簡體   English   中英

如果用戶在WooCommerce購物車中將數量更改為0,則獲取項目的產品ID

[英]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.

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