简体   繁体   中英

WooCommerce cart product delete/update

I have changed my add to cart function to include two products from one product-page. If two products are added they will both get a bundle_id, and when I delete from cart then both products gets removed, but when I undo, only one product returns to the cart. My remove function:

add_action( 'woocommerce_cart_item_removed', 'cart_remove_func', 10, 2 );
function cart_remove_func($removed_cart_item_key, $cart) {
    $line_item = $cart->removed_cart_contents[ $removed_cart_item_key ];
    $bundle_id = $line_item[ 'bundle_id' ];
            
    foreach( WC()->cart->get_cart() as $key => $item ){
        // Check if the item to be removed 1 is in cart
        if( $item['bundle_id'] == $bundle_id ){
            WC()->cart->remove_cart_item($key);
        }
    }
}

I have tried to add the newly deleted item to the removed_cart_contents array, but this didn't seem to work.

Can I add the extra deleted item to the removed_cart_contents array so the "undo" add both products back to the cart?


As for the cart-update: When I update the quantity of an item with bundle_id then both products should be set to the same amount. I have tried to use woocommerce_update_cart_action_cart_updated hook, but all I managed to do here was to show a blanc page and also deleting all products in the cart (not the intention, obviously).

How can I update the quantity of a product with the same bundle_id as the updated product?

The following might work for the undo, although I haven't tested it fully

add_filter('woocommerce_get_undo_url', 'undo_item_link_for_bundle', 10, 2);

function undo_item_link_for_bundle($link, $key){
    $cart_page_url = wc_get_cart_url();
    $line_item = WC()->cart->removed_cart_contents[ $key ];
    $bundle_id = $line_item[ 'bundle_id' ] ?? false;
    if(!$bundle_id){
        return $link;
    }
    foreach(WC()->cart->removed_cart_contents as $k => $v){
        if($v['bundle_id'] == $bundle_id){
            $keys[] = $k;
        }
    }
    $query_args = array(
        'undo_items' => implode(',', $keys),
    );
    return $cart_page_url ? wp_nonce_url( add_query_arg( $query_args, $cart_page_url ), 'woocommerce-cart' ) : '';
}

add_action( 'wp_loaded', 'undo_multiple_items', 20 );

function undo_multiple_items(){
    if ( !isset( $_REQUEST['undo_items'] ) ) {
        return;
    }

    wc_nocache_headers();

    $nonce_value = wc_get_var( $_REQUEST['woocommerce-cart-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) );
    if ( ! empty( $_GET['undo_items'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) {

        // Undo Cart Items.
        $items = explode(',', $_GET['undo_items']);
        foreach($items as $item){
            $cart_item_key = sanitize_text_field( wp_unslash( $item ) );
            WC()->cart->restore_cart_item( $cart_item_key );
        }
        $referer = wp_get_referer() ? remove_query_arg( array( 'undo_items', '_wpnonce' ), wp_get_referer() ) : wc_get_cart_url();
        wp_safe_redirect( $referer );
        exit;

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM