【问题标题】:How to delete a product from WooCommerce cart if associated product is added [duplicate]如果添加了相关产品,如何从 WooCommerce 购物车中删除产品[重复]
【发布时间】:2020-02-19 23:46:27
【问题描述】:

我已经设置了一个 WooCommerce 商店来出售舞蹈工作室的门票。一切正常,但如果有人同时放置 2 张工作坊门票,我会从购物车中删除门票。

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

  • 5 用于名称为 A1 - A5 的培训师 A
  • 5 用于名称为 B1 - B5 的培训师 B。

现在,当有人将 A1 添加到购物车时,他无法使用相同的 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' );
    }
}

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

【问题讨论】:

    标签: php wordpress woocommerce product hook-woocommerce


    【解决方案1】:

    以下代码适用于相应的产品,如果存在一个产品 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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-04
      • 2014-02-06
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多