簡體   English   中英

哪個掛鈎可以更改WooCommerce購物車頁面中的數量更新?

[英]Which Hook to alter quantity update in WooCommerce cart page?

當購物車中的產品數量更改時,我正在嘗試觸發一個函數。 更具體地說,當客戶修改購物車中的金額時,我想運行此功能。

我正在尋找購物車中剩余的金額,然后攔截更新購物車事件

目前,我正在使用:

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

當我按“ update_cart”時,它似乎不起作用。 有什么建議嗎? 謝謝!

您應該使用具有4個參數的 woocommerce_after_cart_item_quantity_update操作掛鈎 但是,當數量更改為零時,需要改為使用woocommerce_before_cart_item_quantity_zero操作鈎子 (並且具有2個參數)

下面是一個工作示例,該示例將更新數量限制為一定數量,並顯示自定義通知:

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文件上 經過測試和工作。

由於此掛鈎位於WC_Cart set_quantity()方法中因此無法在掛鈎內使用該方法,因為它將拋出錯誤


要在數量設置為零時觸發某些操作,請使用:

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
}

也許這個鈎子? do_action( 'woocommerce_after_cart_item_quantity_update', $cart_item_key, $quantity, $old_quantity );

http://hookr.io/actions/woocommerce_after_cart_item_quantity_update/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM