久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Woocommerce 中為登錄用戶啟用銷售價格,為未登

Enable sale price for logged users and regular price for unlogged users in Woocommerce(在 Woocommerce 中為登錄用戶啟用銷售價格,為未登錄用戶啟用正常價格)
本文介紹了在 Woocommerce 中為登錄用戶啟用銷售價格,為未登錄用戶啟用正常價格的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在 Woocommerce 中,我有一個有很多變化的可變產品,每個變化都有自己的常規價格和銷售價格.

In Woocommerce I have a variable products with many variations and every variation has its own REGULAR price and SALE price.

我想當用戶:

  • 已登錄,銷售價格將是有效價格
  • 未登錄,正常價格將是有效價格.

我在我的子主題 function.php 文件中添加了這個鉤子函數:

I have added this hooked function in my child theme function.php file:

add_filter( 'woocommerce_available_variation', 'my_variation', 10, 3);
function my_variation( $data, $product, $variation ) {
    wc_delete_product_transients($variation->get_id());
     $variation_id = $variation->get_id();
     $variable_product1= new WC_Product_Variation($variation_id);
     if(is_user_logged_in()){
        $regular_price = $variable_product1 ->regular_price;
             $data['price_html'] = woocommerce_price($regular_price);
             return $data;
             }  else { 
              $sale_price = $variable_product1 ->sale_price;
         $data['price_html'] = woocommerce_price($sale_price);
         return $data;

        }

}

顯示的價格有效......但是當我將任何產品添加到購物車時,它只保留購物車中的銷售價格......

The displayed price works… But when I add to cart any product it remains only with the sale price in in cart…

感謝任何幫助

推薦答案

下面的代碼將:

  • 僅在任何地方為未登錄的用戶啟用正常價格
  • 僅在所有地方為登錄用戶啟用促銷價
  • 隱藏促銷標志
  • 刪除銷售價格范圍

代碼:

// Variable and simple product displayed prices (removing sale price range)
add_filter( 'woocommerce_get_price_html', 'custom_get_price_html', 20, 2 );
function custom_get_price_html( $price, $product ) {

    if( $product->is_type('variable') )
    {
        if( is_user_logged_in() ){
            $price_min  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_sale_price('min') ) );
            $price_max  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_sale_price('max') ) );
        } else {
            $price_min  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('min') ) );
            $price_max  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_regular_price('max') ) );
        }

        if( $price_min != $price_max ){
            if( $price_min == 0 && $price_max > 0 )
                $price = wc_price( $price_max );
            elseif( $price_min > 0 && $price_max == 0 )
                $price = wc_price( $price_min );
            else
                $price = wc_format_price_range( $price_min, $price_max );
        } else {
            if( $price_min > 0 )
                $price = wc_price( $price_min);
        }
    }
    elseif( $product->is_type('simple') )
    {
        if( is_user_logged_in() )
            $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
        else
            $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );

        if( $active_price > 0 )
            $price = wc_price($active_price);
    }
    return $price;
}

// Product Variation displayed prices
add_filter( 'woocommerce_available_variation', 'custom_variation_price', 10, 3);
function custom_variation_price( $data, $product, $variation ) {

    $reg_price = wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) );
    $sale_price = wc_get_price_to_display( $variation, array( 'price' => $variation->get_sale_price() ) );

    if( is_user_logged_in() )
        $data['price_html'] = wc_price( $sale_price );
    else
        $data['price_html'] = wc_price( $reg_price );

    return $data;
}

// Set the correct prices in cart
add_action( 'woocommerce_before_calculate_totals', 'set_item_cart_prices', 20, 1 );
function set_item_cart_prices( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ){
        if( ! is_user_logged_in() ){
            $cart_item['data']->set_price( $cart_item['data']->get_regular_price() );
        }
    }
}

// Remove sale badge
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );

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

Code goes in function.php file of your active child theme (or active theme). Tested and works.

更新:為在 WooCommerce 中顯示自定義產品價格的功能啟用后綴

這篇關于在 Woocommerce 中為登錄用戶啟用銷售價格,為未登錄用戶啟用正常價格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 亚洲激情视频在线 | 欧美精品久久久久 | 欧美性网 | 一区二区不卡高清 | 久久久久久亚洲精品 | 日本黄色一级片视频 | 国产乱码精品一区二区三区av | 亚洲va在线va天堂va狼色在线 | 精品国产乱码久久久久久果冻传媒 | 在线黄av| 国产女人第一次做爰毛片 | 国产成人影院 | 欧美成人精品一区 | 国产激情在线 | 97av视频在线 | 自拍偷拍精品 | 中文字幕在线观看 | 亚洲一区二区日韩 | 国产精品免费视频一区 | 免费在线色 | 欧美视频三区 | 欧美99久久精品乱码影视 | 国产中文字幕网 | 中文字幕亚洲区 | 国产不卡一区在线观看 | 污视频在线免费观看 | 男人av网| 国产一区二区三区免费 | 久久精品国产99国产 | 日产久久| 天堂中文字幕av | 干干干操操操 | 国产精品特级毛片一区二区三区 | 在线免费中文字幕 | 久操亚洲 | av网址在线播放 | 久久这里有精品 | 在线国产一区 | 亚洲国产成人精品女人久久久 | 久久久精品视频免费 | 日本精品视频一区二区三区四区 |