Я сделал код для удаления товара из корзины в хуке действия woocommerce_cart_item_removed, но когда я добавляю новый продукт в корзину, используя метод add_to_cart, он уменьшает количество товара в корзине, а когда я комментирую код действия woocommerce_cart_item_removed, он отлично работает
Вот мой код функции add_to_cart
add_action( 'wp_ajax_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart');
add_action( 'wp_ajax_nopriv_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart');
function woocommerce_ajax_add_to_cart(){
$product_id = $_POST['product_id'];
$variation_id = $_POST['variation_id'];
$qty = $_POST['quantity_without_attr'];
$dataArr = $_POST['dataArray'];
$term_taxonomy_ids = '';
$cart_item_data = array();
if(isset($_POST['dataArray'])){
foreach ($dataArr as $key => $value) {
$quantity = $value['quantity'];
unset($value['quantity']);
if(isset($value['pa_custom-textbox'])){
$term_taxonomy_ids = wp_set_object_terms($product_id, trim($value['pa_custom-textbox']), "pa_custom-textbox", true);
$data = array(
"pa_".$attribute_name => array(
'name' => "pa_custom-textbox",
'value' => '',
'is_visible' => '1',
'is_variation' => '0',
'is_taxonomy' => '1'
)
);
$_product_attributes = get_post_meta($product_id, '_product_attributes', TRUE);
update_post_meta($product_id, '_product_attributes', array_merge($_product_attributes, $data));
}
if(isset($term_taxonomy_ids)){
$value['pa_custom-textbox'] = $term_taxonomy_ids[0];
}
/*Add to cart when attributes are available*/
/* WC()->cart->add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() );*/
if($quantity>0){
WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $value);
}
}
}else{
/*Add to cart when attributes are not available*/
WC()->cart->add_to_cart( $product_id, $qty, $variation_id, $cart_item_data);
}
/* Insert clone product to cart */
$get_categories = get_the_terms($product_id, "product_cat");
if($get_categories[0]->term_id == 26 || $get_categories[0]->term_id == 42 ){
$found_post = post_exists( 'Art charge/setup fee for '.get_the_title($product_id),'','','product');
echo $found_post;
$post_id = '';
if($found_post == 0){
$post_id = wp_insert_post( array(
'post_title' => 'Art charge/setup fee for '.get_the_title($product_id),
'post_status' => 'publish',
'post_type' => "product",
) );
wp_set_object_terms($post_id, 0 ,'product_cat');
wp_set_object_terms($post_id, 'simple','product_type' );
update_post_meta($post_id, '_regular_price','36.00');
update_post_meta($post_id, '_price','35.00');
update_post_meta($post_id, '_sale_price','35.00');
update_post_meta($post_id, '_tax_status','taxable');
update_post_meta($post_id, '_tax_class','zero-rate');
update_post_meta($post_id, '_manage_stock','no');
update_post_meta($post_id, '_virtual','no');
update_post_meta($post_id, '_downloadable','no');
}else{
$post_id = $found_post;
}
/* Art charge/ set up fees */
WC()->cart->add_to_cart( $post_id,1, $variation_id, $value);
}
}
Вот мой ответ $ _POST
введите описание изображения здесь
Ниже приведен код удаления
/* Auto delete cloned product */
add_action( 'woocommerce_cart_item_removed', 'after_remove_product_from_cart', 10, 2 );
function after_remove_product_from_cart($removed_cart_item_key, $cart) {
$line_item = $cart->removed_cart_contents[ $removed_cart_item_key ];
$product_id = $line_item[ 'product_id' ];
$removed_product_name = get_the_title( $product_id );
$cart_contents = $cart->get_cart_contents();
foreach($cart_contents as $cart_item){
$pro_id = $cart_item['product_id'];
$pro_name = $cart_item['data']->get_name();
if(strpos($pro_name, $removed_product_name) != 0){
if($cart_item['quantity'] > 1){
$new_qty = $cart_item['quantity'] - 1;
$cart->set_quantity($cart_item['key'],$new_qty,true );
}else{
$cart->remove_cart_item($cart_item['key']);
}
}
}
// return $cart;
}