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粉5459565972024-04-05 10:12:26
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") . '';