dpi10335
dpi10335
2016-04-21 05:46

如何从“woocommerce_cart_item_removed”钩子中找出产品ID?

已采纳

I have code

add_action( 'woocommerce_cart_item_removed', 'after_remove_product_from_cart' );
function after_remove_product_from_cart($removed_cart_item_key, $instance) {
    $product_id = $removed_cart_item_key['product_id'];
}

I want to find out a way to get product id or actual product object itself using $removed_cart_item_key. How do you do it? I cannot find any references, thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dougu3290 dougu3290 5年前

    should be something like this...

    add_action( 'woocommerce_cart_item_removed', 'after_remove_product_from_cart', 10, 2 );
    function after_remove_product_from_cart($removed_cart_item_key, $cart) {
        $line_item = $cart->removed_cart_contents[ $removed_cart_item_key ];
        $product_id = $line_item[ 'product_id' ];
    }
    
    点赞 评论 复制链接分享
  • douchao5864 douchao5864 5年前

    Because it happen before the cart item is removed, you need to use woocommerce_remove_cart_item instead of woocommerce_cart_item_removed, to retrieve this product item.

    add_action( 'woocommerce_remove_cart_item', 'after_remove_product_from_cart', 10, 2 );
    function after_remove_product_from_cart($removed_cart_item_key, $cart) {
        $product_id = $cart->cart_contents[ $removed_cart_item_key ]['product_id'];
    }
    

    See this source from helgatheviking

    点赞 评论 复制链接分享

相关推荐