Здравствуйте.
Возникла такая проблема: в товаре у меня есть 25 чекбоксов, которые при выборе добавлют к цене свое значение, но проблема в том - что если вы добавляете один и тот же товар, с одними и теми же параметрами, то товары дублируются.
Но если в карточке товара изменить quantity на 2 - дубляжа не происходит.
Не знаю как решить эту проблему, код:
add_filter('woocommerce_add_cart_item_data', 'add_custom_product_data', 10, 3);
function add_custom_product_data( $cart_item_data, $product_id, $variation_id ) {
global $woocommerce; $product;
$cart_item_data['first_price'] = (float) $_POST['first_price'];
$cart_item_data['second_price'] = (float) $_POST['second_price'];
$cart_item_data['third_price'] = (float) $_POST['third_price'];
$cart_item_data['fourth_price'] = (float) $_POST['fourth_price'];
$cart_item_data['fiveth_price'] = (float) $_POST['fiveth_price'];
$cart_item_data['sixth_price'] = (float) $_POST['sixth_price'];
$cart_item_data['seventh_price'] = (float) $_POST['seventh_price'];
$cart_item_data['eighth_price'] = (float) $_POST['eighth_price'];
$cart_item_data['nineth_price'] = (float) $_POST['nineth_price'];
$cart_item_data['tenth_price'] = (float) $_POST['tenth_price'];
$cart_item_data['eleventh_price'] = (float) $_POST['eleventh_price'];
$cart_item_data['twentyth_price'] = (float) $_POST['twentyth_price'];
$cart_item_data['thirteenth_price'] = (float) $_POST['thirteenth_price'];
$cart_item_data['fourteenth_price'] = (float) $_POST['fourteenth_price'];
$cart_item_data['fiveteenth_price'] = (float) $_POST['fiveteenth_price'];
$cart_item_data['sixteenth_price'] = (float) $_POST['sixteenth_price'];
$cart_item_data['seventeenth_price'] = (float) $_POST['seventeenth_price'];
$cart_item_data['eighteenth_price'] = (float) $_POST['eighteenth_price'];
$cart_item_data['nineteenth_price'] = (float) $_POST['nineteenth_price'];
$cart_item_data['twelwieth_price'] = (float) $_POST['twelwieth_price'];
$cart_item_data['twfirst_price'] = (float) $_POST['twfirst_price'];
$cart_item_data['twsecond_price'] = (float) $_POST['twsecond_price'];
$cart_item_data['twthird_price'] = (float) $_POST['twthird_price'];
$cart_item_data['twfourth_price'] = (float) $_POST['twfourth_price'];
$cart_item_data['active_price'] = (float) $_POST['active_price'];
$cart_item_data['unique_key'] = md5(microtime().rand());
$i = 0;
while($i = 1){
$razmers_id = $product_id;
if(isset($_POST['radio_razmers'.$razmers_id])){
$_POST['active_price'] = $_POST['radio_razmers'.$razmers_id];
$new_price_perm = $_POST['active_price'];
} else {
$new_price_perm = $_POST['active_price'];
}
if (isset($_POST['first_option'])) {
$new_price_perm += $_POST['first_price'];
}
if (isset($_POST['second_option'])) {
$new_price_perm += $_POST['second_price'];
}
if (isset($_POST['third_option'])) {
$new_price_perm += $_POST['third_price'];
}
if (isset($_POST['fourth_option'])) {
$new_price_perm += $_POST['fourth_price'];
}
if (isset($_POST['fiveth_option'])) {
$new_price_perm += $_POST['fiveth_price'];
}
if (isset($_POST['sixth_option'])) {
$new_price_perm += $_POST['sixth_price'];
}
if (isset($_POST['seventh_option'])) {
$new_price_perm += $_POST['seventh_price'];
}
if (isset($_POST['eighth_option'])) {
$new_price_perm += $_POST['eighth_price'];
}
if (isset($_POST['nineth_option'])) {
$new_price_perm += $_POST['nineth_price'];
}
if (isset($_POST['tenth_option'])) {
$new_price_perm += $_POST['tenth_price'];
}
if (isset($_POST['eleventh_option'])) {
$new_price_perm += $_POST['eleventh_price'];
}
if (isset($_POST['twentyth_option'])) {
$new_price_perm += $_POST['twentyth_price'];
}
if (isset($_POST['thirteenth_option'])) {
$new_price_perm += $_POST['thirteenth_price'];
}
if (isset($_POST['fourteenth_option'])) {
$new_price_perm += $_POST['fourteenth_price'];
}
if (isset($_POST['fiveteenth_option'])) {
$new_price_perm += $_POST['fiveteenth_price'];
}
if (isset($_POST['sixteenth_option'])) {
$new_price_perm += $_POST['sixteenth_price'];
}
if (isset($_POST['seventeenth_option'])) {
$new_price_perm += $_POST['seventeenth_price'];
}
if (isset($_POST['eighteenth_option'])) {
$new_price_perm += $_POST['eighteenth_price'];
}
if (isset($_POST['nineteenth_option'])) {
$new_price_perm += $_POST['nineteenth_price'];
}
if (isset($_POST['twelwieth_option'])) {
$new_price_perm += $_POST['twelwieth_price'];
}
if (isset($_POST['twfirst_option'])) {
$new_price_perm += $_POST['twfirst_price'];
}
if (isset($_POST['twsecond_option'])) {
$new_price_perm += $_POST['twsecond_price'];
}
if (isset($_POST['twthird_option'])) {
$new_price_perm += $_POST['twthird_price'];
}
if (isset($_POST['twfourth_option'])) {
$new_price_perm += $_POST['twfourth_price'];
}
if(isset($_POST['radio_dobav'])){
$radio_dobav_perm = $_POST["radio_dobav"];
$new_price_perm += $radio_dobav_perm;
}
$cart_item_data['new_price'] = $new_price_perm;
break;
}
return $cart_item_data;
}
add_action('woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 20, 1);
function extra_price_add_custom_price($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach($cart->get_cart() as $cart_item) {
if (isset($cart_item['new_price']))
$cart_item['data']->set_price((float) $cart_item['new_price']);
}
}