dpi10335
2016-04-21 05:46
浏览 69
已采纳

如何从“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.

图片转代码服务由CSDN问答提供 功能建议

我有代码

  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']; 
} 
   
 
 

我想要 找出一种使用$ removed_cart_item_key获取产品ID或实际产品对象的方法。 你怎么做呢? 我找不到任何参考文献,谢谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dougu3290 2016-04-21 05:55
    已采纳

    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 2016-04-21 06:08

    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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题