哪个挂钩可以更改WooCommerce购物车页面中的数量更新? [英] Which Hook to alter quantity update in WooCommerce cart page?

查看:177
本文介绍了哪个挂钩可以更改WooCommerce购物车页面中的数量更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在购物车中更改产品数量时触发功能。
更具体地说,当客户修改购物车中的金额时,我想运行此功能。

I'm trying to fire a function when the quantity of a product is changed in cart. More specifically I want to run this function when a customer modify the amount in a cart.

我要查找购物车中剩余的金额,然后拦截更新购物车事件

I'm looking to find the amount left in a cart then to intercept the update cart event

当前我正在使用:

add_action( 'woocommerce_remove_cart_item', 'my function');

当我按 update_cart时,它似乎不起作用。
有什么建议吗?
谢谢!

When I press "update_cart", it doesn't seem to work. Any advice? Thank you!

推荐答案

您应该使用 woocommerce_after_cart_item_quantity_update 动作钩子具有 4个自变量强>。但是,当数量更改为零时, 需要使用woocommerce_before_cart_item_quantity_zero 操作挂钩代替(并具有2个参数)

You should use woocommerce_after_cart_item_quantity_update action hook that has 4 arguments. But when quantity is changed to zero, woocommerce_before_cart_item_quantity_zero action hook need to be used instead (and has 2 arguments).

下面是一个将更新数量限制为一定数量并显示自定义通知的工作示例:

Below is a working example that will limit the updated quantity to a certain amount and will display a custom notice:

add_action( 'woocommerce_after_cart_item_quantity_update', 'limit_cart_item_quantity', 20, 4 );
function limit_cart_item_quantity( $cart_item_key, $quantity, $old_quantity, $cart ){
    if( ! is_cart() ) return; // Only on cart page

    // Here the quantity limit
    $limit = 5;

    if( $quantity > $limit ){
        // Change the quantity to the limit allowed
        $cart->cart_contents[ $cart_item_key ]['quantity'] = $limit;
        // Add a custom notice
        wc_add_notice( __('Quantity limit reached for this item'), 'notice' );
    }
}

此代码在function.php文件中进行您的活动子主题(或主题)。经过测试并可以正常工作。

This code goes on function.php file of your active child theme (or theme). Tested and works.


该钩位于 WC_Cart set_quantity()方法无法在挂钩内使用该方法,因为它将抛出错误






在数量设置为零时触发一些动作:


To trigger some action when quantity is set to Zero use:

add_action( 'woocommerce_before_cart_item_quantity_zero', 'action_before_cart_item_quantity_zero', 20, 4 );
function action_before_cart_item_quantity_zero( $cart_item_key, $cart ){
    // Your code goes here
}

这篇关于哪个挂钩可以更改WooCommerce购物车页面中的数量更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆