久久久久久久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久久久久久久 | 国产精品一区二区视频 | 精品久久久久久 | 亚洲欧美日韩在线 | 精品国产高清一区二区三区 | 久久精选 | 国产成人jvid在线播放 | 日韩一区在线观看视频 | 成人日韩 | 欧美午夜一区二区三区免费大片 | 久久国产高清视频 | 一级黄大片| 超碰97人人人人人蜜桃 | 亚洲欧美中文日韩在线v日本 | 日韩一区二区免费视频 | 日韩免费av| 日本一区二区电影 | 国产精品久久国产愉拍 | 尤物在线视频 | 一本一道久久a久久精品综合蜜臀 | 精品视频一区二区三区四区 | 亚洲一页 | 神马久久久久久久久久 | 亚洲精品在线视频 | 在线看片网站 | 一区二区三区四区不卡视频 | 免费看黄视频网站 | 成人午夜毛片 | 99色播| 91精品国产色综合久久不卡98口 | 9999国产精品欧美久久久久久 | 日韩国产一区二区三区 | 日韩免费在线观看视频 | av网站观看 | 亚洲精品国产精品国自产在线 | 午夜精品久久久久久久久久久久 | 色播久久| 美女福利视频一区 | 国产乱码精品一区二区三区五月婷 | 第一色在线 | 国产激情精品 |