복붙노트

[WORDPRESS] 특정 제품 쇼핑 카트에 추가 할 때 어떻게 특정 카트 항목을 제거하려면?

WORDPRESS

특정 제품 쇼핑 카트에 추가 할 때 어떻게 특정 카트 항목을 제거하려면?

해결법


  1. 1.예 woocommerce_add_to_cart 훅, 예를 들어 꺾어 사용자 정의 기능이 가능합니다 :

    예 woocommerce_add_to_cart 훅, 예를 들어 꺾어 사용자 정의 기능이 가능합니다 :

    add_action( 'woocommerce_add_to_cart', 'check_product_added_to_cart', 10, 6 );
    function check_product_added_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    
        // Set HERE your targeted product ID
        $target_product_id = 31;
        // Set HERE the  product ID to remove
        $item_id_to_remove = 37;
    
        // Initialising some variables
        $has_item = false;
        $is_product_id = false;
    
        foreach( WC()->cart->get_cart() as $key => $item ){
            // Check if the item to remove is in cart
            if( $item['product_id'] == $item_id_to_remove ){
                $has_item = true;
                $key_to_remove = $key;
            }
    
            // Check if we add to cart the targeted product ID
            if( $product_id == $target_product_id ){
                $is_product_id = true;
            }
        }
    
        if( $has_item && $is_product_id ){
            WC()->cart->remove_cart_item($key_to_remove);
    
            // Optionaly displaying a notice for the removed item:
            wc_add_notice( __( 'The product "blab bla" has been removed from cart.', 'theme_domain' ), 'notice' );
        }
    }
    

    이 코드는 어떤 플러그인 파일도 function.php의 활성 자식 테마 (또는 테마)의 파일이나 간다.

    이 코드는 테스트 및 작동된다.

  2. from https://stackoverflow.com/questions/41833075/how-to-remove-a-specific-cart-item-when-adding-to-cart-a-specific-product by cc-by-sa and MIT license