第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

基于 WooCommerce 購(gòu)物車(chē)中某個(gè)類(lèi)別的商品數(shù)量計(jì)數(shù)的折扣

基于 WooCommerce 購(gòu)物車(chē)中某個(gè)類(lèi)別的商品數(shù)量計(jì)數(shù)的折扣

PHP
皈依舞 2023-06-18 16:28:41
我有一類(lèi)產(chǎn)品的價(jià)格都是 15 美元。當(dāng)用戶(hù)從該類(lèi)別購(gòu)買(mǎi) 10 到 20 件產(chǎn)品時(shí),他們應(yīng)該獲得 10 美元的折扣價(jià)。當(dāng)用戶(hù)購(gòu)買(mǎi) 20+ 時(shí),價(jià)格再次變?yōu)?5 美元。不能為用戶(hù)分配自定義角色(如批發(fā)商)。我根據(jù)另一個(gè)問(wèn)題的 LoicTheAztec 代碼松散地創(chuàng)建了代碼,并添加了我自己的修改和代碼??雌饋?lái)應(yīng)該可以。我沒(méi)有收到任何錯(cuò)誤,但它不起作用。add_action('woocommerce_before_cart', 'check_product_category_in_cart');function check_product_category_in_cart() {    // HERE set your product categories in the array (can be IDs, slugs or names)    $categories = array('surfacing-adhesives');    $found      = false; // Initializing    $count = 0;    // Loop through cart items          foreach ( WC()->cart->get_cart() as $cart_item ) {        // If product categories is found        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {            $count++;        }    }    if (!current_user_can('wholesaler')) {        // Discounts        if ($count > 10 && $count < 20) {            // Drop the per item price            $price = 10;        } else if ($count > 20) {            // Drop the per item price            $price = 5;        } else {            // Did not qualify for volume discount        }    }}
查看完整描述

1 回答

?
楊__羊羊

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊

您沒(méi)有使用正確的鉤子,并且缺少一些東西。嘗試以下操作:


add_action( 'woocommerce_before_calculate_totals', 'discounted_cart_item_price', 20, 1 );

function discounted_cart_item_price( $cart ){

    // Not for wholesaler user role

    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || current_user_can('wholesaler') )

        return;


    // Required since Woocommerce version 3.2 for cart items properties changes

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )

        return;


    // HERE set your product categories in the array (can be IDs, slugs or names)

    $categories = array('surfacing-adhesives');

    $categories = array('t-shirts');


    // Initializing

    $found = false;

    $count = 0;


    // 1st Loop: get category items count  

    foreach ( WC()->cart->get_cart() as $cart_item ) {

        // If product categories is found

        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {

            $count += $cart_item['quantity'];

        }

    }


    // Applying discount

    if ( $count >= 10 ) {

        // Discount calculation (Drop the per item qty price)

        $price = $count >= 20 ? 5 : 10;


        // 2nd Loop: Set discounted price  

        foreach ( WC()->cart->get_cart() as $cart_item ) {

            // If product categories is found

            if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {

                $cart_item['data']->set_price( $price );

            }

        }

    }

}

代碼進(jìn)入您的活動(dòng)子主題(或活動(dòng)主題)的 functions.php 文件。測(cè)試和工作。


查看完整回答
反對(duì) 回復(fù) 2023-06-18
  • 1 回答
  • 0 關(guān)注
  • 171 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)