[英]Get The Product Id of An Item If User Changes Quantity to 0 in WooCommerce Cart
我试图找出一种方法来从购物车页面获取数量减少为0的商品的产品ID。
我挂在woocommerce_after_cart_item_quantity_update之后,如果用户将数量更改为0,但它会触发,就触发。
function action_woocommerce_after_cart_item_quantity_update( $cart_item_key, $quantity, $old_quantity ) {
var_error_log([$cart_item_key, $quantity, $old_quantity]);
};
add_action( 'woocommerce_after_cart_item_quantity_update', 'action_woocommerce_after_cart_item_quantity_update', 10, 3 );
function var_error_log( $object=null ){
ob_start(); // start buffer capture
var_dump( $object ); // dump the values
$contents = ob_get_contents(); // put the buffer into a variable
ob_end_clean(); // end capture
error_log( $contents ); // log contents of the result of var_dump( $object )
}
还有另一种方法吗?
编辑:
同样,woocommerce_cart_item_removed挂钩也不好,因为它仅在用户单击删除按钮时运行,而不是在该项目的数量更改为0时运行。
并且woocommerce_cart_item_removed挂钩与woocommerce_cart_item_removed挂钩相似。
对于这种情况,有一个单独的钩子:
woocommerce_before_cart_item_quantity_zero
function woocommerce_before_cart_item_quantity_zero_vvvrbg45kt45($cart_item_key) {
global $woocommerce;
$cart_item = $woocommerce->cart->get_cart_item( $cart_item_key );
$product_id = $cart_item['product_id'];
}
add_action( 'woocommerce_before_cart_item_quantity_zero', 'woocommerce_before_cart_item_quantity_zero_vvvrbg45kt45' );
试试这个钩子
add_action( 'woocommerce_cart_item_removed', 'after_remove_product_from_cart', 10, 2 );
function after_remove_product_from_cart($removed_cart_item_key, $instance) {
$line_item = $instance->removed_cart_contents[ $removed_cart_item_key ];
$product_id = $line_item[ 'product_id' ];
}
从购物车中取出物品时,这会触发。 欲获得更多信息,
希望这对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.