Get the shopping cart item key ID of a specific removed item: WooCommerce Shopping Cart
P粉384244473
P粉384244473 2024-04-04 16:35:18
0
1
284

In WooCommerce, I have a custom cart.php template where I need to check if a deleted product (item) is unique and then further code based on that information.

Is there a way to find this ONE Key id of a deleted item without using a hook, the one that notifies ' that "Item X" has been deleted. Undo? '?

I can't find any solution anywhere.

P粉384244473
P粉384244473

reply all(1)
P粉545956597

You can get deleted shopping cart items in two ways:

  • Used from the WC_Cart object:

    $removed_items = WC()->cart->get_removed_cart_contents();
  • Used from the WC_Session object:

    $removed_items = WC()->session->get('removed_cart_contents');

To locate a specific deleted product and get its key ID (and the revoke URL or delete URL from it) , use:

$targeted_product_id = 25; // Set the product ID to target
$targeted_item_key   = ''; // Initializing

// Get removed cart items
$removed_items = WC()->cart->get_removed_cart_contents();

// Loop through removed cart items
foreach( $removed_items as $item_key => $item ) {
    $product_id   = $item['product_id'];
    $variation_id = $item['variation_id'];

    if( in_array($targeted_product_id, [$product_id, $variation_id]) ) {
        $targeted_item_key = $item_key; 
        break;
    }
}
// get the Undo URL
$undo_url = WC()->cart->get_undo_url( $targeted_item_key );

// Test output Undo URL
echo ''. __("Undo Url") . '';

// get the remove URL
$remove_url = WC()->cart->get_remove_url( $targeted_item_key );

// Test output remove URL
echo ''. __("Remove Url") . '';
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!