簡體   English   中英

WooCommerce:僅獲取定制產品的購物車商品數量

[英]WooCommerce: Get the cart item quantity of a customized product only

我已經修改了 functions.php 文件以包含使用自定義文本自定義某些產品的可能性。

此功能附帶 1 的額外費用,除非該功能尚未完成,否則將在產品結帳時顯示:

    if (!empty( $cart_item['custom_text'] ) ) {

        $surcharge = 1;
        WC()->cart->add_fee( 'Customization', $surcharge, true, '' );
    }

自然,如果將更多定制產品添加到購物車中,則此 1 應乘以購物車中的產品數量。 為此,我嘗試添加變量 $quantity :

    if (!empty( $cart_item['custom_text'] ) ) {
            
        $quantity = WC()->cart->get_cart_contents_count();

        $surcharge = 1 * $quantity;
        WC()->cart->add_fee( 'Customization', $surcharge, true, '' );
    }

這里的問題是,如果客戶嘗試購買定制商品而不是其他商品,因為此方法會考慮購物車中的總數量,而不管它是否定制。 見圖1:

圖1

那么問題來了:是否可以只獲取定制項目(圖1中綠色圈出)來計算附加費?

要獲取您的定制產品的購物車商品數量,請僅使用以下內容:

if (!empty( $cart_item['custom_text'] ) ) {
    $surcharge = 1;
    $quantity  = $cart_item['quantity'];
    WC()->cart->add_fee( 'Customization', ( $surcharge * $quantity ), true, '' );
}

它應該可以按您的預期工作

暫無
暫無

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

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