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

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

Woocommerce中特定產(chǎn)品的動(dòng)態(tài)銷售價(jià)格顯示

Woocommerce中特定產(chǎn)品的動(dòng)態(tài)銷售價(jià)格顯示

PHP
達(dá)令說 2021-05-03 10:42:49
我需要在功能文件中開發(fā)一個(gè)腳本,為注冊用戶顯示特定類別的銷售價(jià)格。這段代碼工作正常。我需要添加促銷價(jià)設(shè)計(jì)function thenga_customer_specific_pricing( $price, $product ) {        if ( ! is_user_logged_in() ) {        return $price;    }    $id = $product->get_id();    if( has_term( 'daniel-wellington', 'product_cat' ,$id ) ){        // Give these customers a 20% discount.        return $price * 0.8;    } elseif( has_term( 'giardino-segreto', 'product_cat' ,$id ) ){        return $price * 0.85;    } else {        return $price;    }}add_filter( 'woocommerce_product_get_price', 'thenga_customer_specific_pricing', 10, 2 );我期望這樣的輸出:是:100€現(xiàn)在:80€我試過這個(gè)過濾器:function custom_dynamic_sale_price_html( $price_html, $product ) {    if( $product->is_type('variable') ) return $price_html;    $price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display(  $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();    return $price_html;}add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );但是它適用于所有產(chǎn)品,如何僅在特定類別中才能將此過濾器稱為“過濾器”?
查看完整描述

2 回答

?
慕無忌1623718

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

以下代碼將顯示您的預(yù)期輸出(對于簡單產(chǎn)品):


// Discount prices

add_filter( 'woocommerce_product_get_price', 'specific_discounted_product_prices', 10, 2 );

function specific_discounted_product_prices( $price, $product ) {

    // For logged in customers

    if ( is_user_logged_in() ) {

        if( has_term( 'daniel-wellington', 'product_cat' ,$product->get_id() ) ){

            $price *= 0.8; // 20% discount

        } elseif( has_term( 'giardino-segreto', 'product_cat' ,$product->get_id() ) ){

            $price *= 0.85; // 15% discount

        }

    }

    return $price;

}


// Display the discount

add_filter( 'woocommerce_get_price_html', 'specific_discounted_product_prices_display', 10, 2 );

function specific_discounted_product_prices_display( $price, $product ) {

    // For simple products and logged in customers

    if( $product->is_type('simple') && is_user_logged_in() ){

        $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );

        $sale_price    = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );

        $active_price  = wc_get_price_to_display( $product );


        if( $regular_price != $active_price ) {

            if( $product->is_on_sale() )

                $price = sprintf( 'Was: %s – Now: %s', wc_price($sale_price), wc_price($active_price) );

            else

                $price = sprintf( 'Was: %s – Now: %s', wc_price($regular_price), wc_price($active_price) );

        }

    }

    return $price;

}

代碼進(jìn)入您的活動(dòng)子主題(或活動(dòng)主題)的function.php文件中。經(jīng)過測試和工作。


查看完整回答
反對 回復(fù) 2021-05-21
  • 2 回答
  • 0 關(guān)注
  • 181 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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