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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

登錄用戶的 WooCommerce 產品定制折扣價

登錄用戶的 WooCommerce 產品定制折扣價

PHP
犯罪嫌疑人X 2023-08-11 16:11:42
我有關于在 WooCommerce 中管理價格的問題。我有一家商店,只出售簡單的產品。假設對于所有訂戶和客戶,每種產品的正常價格都有 10% 的折扣。這很簡單:    function custom_price( $price, $product ) {    global $post, $blog_id;    $post_id = $post->ID;    get_post_meta($post->ID, '_regular_price');        if ( is_user_logged_in() ) {            return $price = ($price * 0.9);        } else{            return $price;              }   }   add_filter( 'woocommerce_get_price', 'custom_price', 10, 2);對于已經有促銷價的產品,我希望 woocommerce 按正常價格計算登錄用戶的折扣,并且客戶可以看到促銷價和折扣價之間的最低價格。所以:場景1正常價格:100登錄用戶專用價格:90(比正常價格優(yōu)惠 10%)產品售價:85登錄用戶的價格必須是:85場景2正常價格:100登錄用戶專用價格:90(比正常價格優(yōu)惠 10%)產品售價:95登錄用戶的價格必須是:90Woocommerce 使用上面的代碼片段,計算登錄用戶在銷售價格上的 10% 折扣,返回:場景1登錄用戶的產品價格:76.5(促銷價 10% 折扣,85)場景2登錄用戶的產品價格:85.5(促銷價 95 折 10%)我該如何解決?感謝您的幫助
查看完整描述

1 回答

?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

您的問題代碼自 WooCommerce 3 起已過時且已棄用......而是使用以下應與您的場景相匹配的代碼:


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

add_filter( 'woocommerce_product_variation_get_price', 'custom_discount_price', 10, 2 );

function custom_discount_price( $price, $product ) {

    // For logged in users

    if ( is_user_logged_in() ) {

        $discount_rate = 0.9; // 10% of discount

        

        // Product is on sale

        if ( $product->is_on_sale() ) { 

            // return the smallest price value between on sale price and custom discounted price

            return min( $price, ( $product->get_regular_price() * $discount_rate ) );

        }

        // Product is not on sale

        else {

            // Returns the custom discounted price

            return $price * $discount_rate;

        }

    }

    return $price;

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。


查看完整回答
反對 回復 2023-08-11
  • 1 回答
  • 0 關注
  • 108 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號