获取特定移除商品的购物车商品键ID:WooCommerce购物车
P粉384244473
P粉384244473 2024-04-04 16:35:18
0
1
273

在 WooCommerce 中,我有一个自定义 cart.php 模板,我需要在其中检查删除的产品(项目)是否是独一无二的,然后根据该信息进一步编码。

有没有一种方法可以在不使用钩子的情况下找到已删除项目的这个 ONE Key id,即通知 '“Item X”已删除的那个。撤消?'

我在任何地方都找不到任何解决方案。

P粉384244473
P粉384244473

全部回复(1)
P粉545956597

您可以通过两种方式获取已删除的购物车商品:

  • 从 WC_Cart 对象中使用:

    $removed_items = WC()->cart->get_removed_cart_contents();
  • 从 WC_Session 对象中使用:

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

要定位特定的已删除产品并获取其密钥 ID(以及撤消 URL 或从中删除 URL),请使用:

$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") . '';
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!