如果添加了关联产品,如何从 WooCommerce 购物车中删除产品

How to delete a product from WooCommerce cart if associated product is added

我已经建立了一个 WooCommerce 商店来出售舞蹈工作坊的门票。一切都很好,但如果有人同时放入 2 张工作坊门票,我会从购物车中删除门票。

示例:我已经制作了 10 张课程门票。

现在当有人将 A1 添加到购物车时,他不能使用相同的时间进程 B1。

我使用以下 代码,这仅 有效 1 产品

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' );
    }
}

谁能解释一下应该如何修改这段代码?

以下代码适用于相应的产品,如果存在一个产品 ID,则会从购物车中删除相应的产品 ID。


更新: 12/20 - 改进代码

function action_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    // (USE: PRODUCT_IDs) - the corresponding product id's
    // Example: if product with ID 30 is added and product with ID 32 is in cart, product with ID 32 will be removed from cart, this also works the opposite    
    $associated_product_ids_arrays = array(
        array(
            'product_id_1' => '30',
            'product_id_2' => '32',
        ), 
        array(
            'product_id_1' => '817',
            'product_id_2' => '819',
        ), 
        array(
            'product_id_1' => '827',
            'product_id_2' => '843',
        ), 
    );
    
    // check
    $found = false;
    
    // Loop trough arrays
    foreach ( $associated_product_ids_arrays as $key_1 => $associated_product_ids_array ) {
        foreach ( $associated_product_ids_array as $key_2 => $associated_product_ids ) {
            // Compare
            if ( $associated_product_ids == $product_id ) {
                // Unset
                unset( $associated_product_ids_array[$key_2] );
                
                // Convert last value in array to integer
                $associated_ticket_id = (int) implode('', $associated_product_ids_array );
                
                // Found = true, break 2 loops
                $found = true;
                break 2;
            }
        }
    }
    
    // True
    if ( $found ) {
        // Generate a unique ID for the cart item
        $product_cart_id = WC()->cart->generate_cart_id( $associated_ticket_id );
        
        // Check if product is in the cart and return cart item key
        $item_key = WC()->cart->find_product_in_cart( $product_cart_id );
        
        // if item_key true
        if ( $item_key ) {
            // remove product from cart
            WC()->cart->remove_cart_item( $item_key );
            
            // Optionaly displaying a notice
            wc_add_notice( sprintf( __( 'The product %d has been removed from cart because product %d has been added.', 'woocommerce' ), $associated_ticket_id, $product_id ), 'notice' );
        }
    }
}
add_action( 'woocommerce_add_to_cart', 'action_woocommerce_add_to_cart', 10, 6 );

03/20 的回答

function product_to_remove( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    // (USE: PRODUCT_IDs) - the corresponding ticket id's
    $associated_ticket_ids_1 = array( 30, 32 );
    $associated_ticket_ids_2 = array( 817, 819 );
    $associated_ticket_ids_3 = array( 827, 843 );
    // etc...
    
    // total of associated ticket ids arrays, total = number of different $associated_ticket_ids arrays, in this example: 3
    $total = 3;
    
     // check
    $found = false;
    
     // loop through the arrays
    for ($i = 1; $i <= $total; $i++) {
        $array_name = ${'associated_ticket_ids_' . $i};
        
        foreach ($array_name as $ticket_id ) {
            if ( $ticket_id == $product_id ) {
                /* Get the associated ticket id */
                // Search value and unset
                unset( $array_name[array_search( $ticket_id, $array_name )] );
                
                // Convert last value in array to integer
                $associated_ticket_id = (int) implode('', $array_name);
                
                // if found, break loops
                $found = true;
                break 2;
                
                 /* uncomment line below for debug purposes */
                //echo 'Product id: ' . $product_id . ' | Ticket id: ' . $ticket_id . ' | Associated ticket id: ' . $associated_ticket_id;
            }
        }
    }
    
    // if found true
    if ( $found ) {
        // Generate a unique ID for the cart item
        $product_cart_id = WC()->cart->generate_cart_id( $associated_ticket_id );
        
        // Check if product is in the cart and return cart item key
        $item_key = WC()->cart->find_product_in_cart( $product_cart_id );
        
        // if item_key true
        if ( $item_key ) {
            // remove product from cart
            WC()->cart->remove_cart_item( $item_key );
            
            // Optionaly displaying a notice
            wc_add_notice( __( 'The product ' . $associated_ticket_id . ' has been removed from cart because product ' . $product_id . ' has been added.', 'woocommerce' ), 'notice' );
        }
    }
}
add_action( 'woocommerce_add_to_cart', 'product_to_remove', 10, 6 );