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

断了今生、忘了曾经 提交于 2021-01-20 13:54:27

问题


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.

Example: I have made 10 Tickets for the courses.

  • 5 for Trainer A with Name A1 - A5
  • 5 for Trainer B with Name B1 - B5.

Now when somebody add A1 to cart he cannot use the same Time Course B1.

I use the following code, this only works for 1 product

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?


回答1:


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


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

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


来源:https://stackoverflow.com/questions/60310968/how-to-delete-a-product-from-woocommerce-cart-if-associated-product-is-added

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!