如果添加了关联产品,如何从WooCommerce购物车中删除产品 [英] How to delete a product from WooCommerce cart if associated product is added

查看:82
本文介绍了如果添加了关联产品,如何从WooCommerce购物车中删除产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have setup a WooCommerce shop for sell Ticket for a Dancing Workshop. Everthing is ok, but i will delete Tickets from cart if somebody puts 2 Tickets for Workshops that are at the same time.

示例:我已为该课程制作了10张门票.

Example: I have made 10 Tickets for the courses.

  • 5个名称为A1-A5的培训师A
  • 5个名称为B1-B5的培训师B.

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

Now when somebody add A1 to cart he cannot use the same Time Course 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' );
    }
}


有人可以解释如何修改此代码?


Someone who can explain how this code should be modified?

推荐答案

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

Following code works with corresponding products, if one product id is present, then the corresponding product id is removed from cart.

更新:12/20-改进了代码

UPDATE: 12/20 - improved code

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开始回答


Answer from 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 );

这篇关于如果添加了关联产品,如何从WooCommerce购物车中删除产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆