添加挂钩以删除 woocommerce 购物车中的项目

Mar*_*iaZ 3 php wordpress woocommerce

在 woocommerce 购物车中,当用户按下购物车上的 REMOVE ITEM 按钮时,我试图从购物车中的项目中检索一些帖子元数据。就像是:

$removed_stock = get_post_meta( $product_id, 'more_info_data', 'x' );
Run Code Online (Sandbox Code Playgroud)

为此,我添加了一个操作:

function ss_cart_updated( $item_id ) {

    print "<pre>";
    print_r (WC()->cart->get_cart());
    print "</pre>";
    exit;

};

// add the action
add_action( 'woocommerce_cart_item_removed', 'ss_cart_updated' );
Run Code Online (Sandbox Code Playgroud)

不幸的是,这只列出了购物车中尚未删除的所有产品。尚未删除的项目不再列出。

我尝试了“woocommerce_get_remove_url”和“woocommerce_cart_item_remove_link”,它们似乎对我没有任何作用。

非常感谢!

hel*_*ing 9

我认为您需要使用在woocommerce_remove_cart_item商品从购物车内容数组中实际取消设置之前运行的 。删除项目后woocommerce_cart_item_removed发生,因此您发现无法获取有关该产品的任何信息。未经测试,但试试这个:

function ss_cart_updated( $cart_item_key, $cart ) {

    print "<pre>";
    $product_id = $cart->cart_contents[ $cart_item_key ]['product_id']; 
    print_r(get_post_meta( $product_id, 'more_info_data', true ));
    print "</pre>";
    exit;

};
add_action( 'woocommerce_remove_cart_item', 'ss_cart_updated', 10, 2 );
Run Code Online (Sandbox Code Playgroud)

  • WC 2.5 中可能会出现上述钩子,这就是为什么您还不能使用它的原因。我经常使用最前沿的 github 版本,因此我可以针对我维护的高级扩展对其进行测试。这有时会让我在回答问题时遇到麻烦 b/c 我的源代码不一样。:) (2认同)