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

用 Woocommerce 3 中的自定義文本替換產品零顯示價

Replace product zero displayed price with a custom text in Woocommerce 3(用 Woocommerce 3 中的自定義文本替換產品零顯示價格)
本文介紹了用 Woocommerce 3 中的自定義文本替換產品零顯示價格的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在尋找一種方法來用文本替換 WooCommerce(版本 3.3.x)產品的 €0,00 價格,例如基于合同".最好在網上商店和電子郵件中可見.由于 WooCommerce 最近的變化,現有的代碼段不再起作用,例如:

I am looking for a way to replace €0,00 price for a WooCommerce (version 3.3.x) product with text, like 'Based on contract'. Preferably visible in the webshop and emails. Because of recent changes in WooCommerce the existing snippets aren't working anymore, like:

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

function changeFreePriceNotice($price, $product) {
    if ( $price == wc_price( 0.00 ) )
        return 'FREE';
    else
        return $price;
}

<?php
// Do not copy the opening <?php tag above
// Customize the Free! price output
add_filter( 'woocommerce_variable_free_price_html','wsm_custom_free_price' );
add_filter( 'woocommerce_free_price_html', 'wsm_custom_free_price' );
add_filter( 'woocommerce_variation_free_price_html', 'wsm_custom_free_price' );
function wsm_custom_free_price( $price ) {
    // Edit content between single quotes below to desired output
    return 'Prepaid Inventory';
}

function my_wc_custom_get_price_html( $price, $product ) {
    if ( $product->get_price() == 0 ) {
        if ( $product->is_on_sale() && $product->get_regular_price() ) {
            $regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) );

            $price = wc_format_price_range( $regular_price, __( 'Free!', 'woocommerce' ) );
        } else {
            $price = '<span class="amount">' . __( 'Free!', 'woocommerce' ) . '</span>';
        }
    }

    return $price;
}

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

推薦答案

以下是在 WooCommerce 3+ 上執行此操作的正確方法(也在 WooCommerce 3.3.x 和 3.5+ 上測試):

Here is the correct way to do it on Woocommerce 3+ (tested on WooCommerce 3.3.x and 3.5+ too):

// Product displayed prices
add_filter( 'woocommerce_get_price_html', 'free_price_custom_label', 20, 2 );
function free_price_custom_label( $price, $product ) {
    if ( is_shop() || is_product_category() || is_product_tag() || is_product() ) {
        // HERE your custom free price label
        $free_label = '<span class="amount">' . __('Based on contract') . '</span>';

        if( $product->is_type('variable') )
        {
            $price_min  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price('min') ) );
            $price_max  = wc_get_price_to_display( $product, array( 'price' => $product->get_variation_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);
                else
                    $price = $free_label;
            }
        }
        elseif( $product->is_on_sale() )
        {
            $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() ) );
            if( $sale_price > 0 )
                $price = wc_format_sale_price( $regular_price, $sale_price );
            else
                $price = $free_label;
        }
        else
        {
            $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) );
            if( $active_price > 0 )
                $price = wc_price($active_price);
            else
                $price = $free_label;
        }
    }
    return $price;
}

// Product variation displayed prices
add_filter( 'woocommerce_variable_price_html', 'free_variation_price_custom_label', 20, 2 );
function free_variation_price_custom_label( $price, $product ) {
    // HERE your custom free price label
    $free_label = '<span class="amount">' . __('Based on contract') . '</span>';

    if( $product->is_on_sale() )
    {
        $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() ) );
        if( $sale_price > 0 )
            $price = wc_format_sale_price( $regular_price, $sale_price );
        else
            $price = $free_label;
    }
    else
    {
        $active_price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) );
        if( $active_price > 0 )
            $price = wc_price($active_price);
        else
            $price = $free_label;
    }
    return $price;
}

// Cart items displayed prices and line item subtotal
add_filter( 'woocommerce_cart_item_subtotal', 'free_cart_item_price_custom_label', 20, 3 );
add_filter( 'woocommerce_cart_item_price', 'free_cart_item_price_custom_label', 20, 3 );
function free_cart_item_price_custom_label( $price, $cart_item, $cart_item_key ) {
    // HERE your custom free price label
    $free_label = '<span class="amount">' . __('Based on contract') . '</span>';

    if( $cart_item['data']->get_price() > 0 )
        return $price;
    else
        return $free_label;
}

// Order items displayed prices (and also email notifications)
add_filter( 'woocommerce_order_formatted_line_subtotal', 'free_order_item_price_custom_label', 20, 3 );
function free_order_item_price_custom_label( $subtotal, $item, $order ) {
    // HERE your custom free price label
    $free_label = '<span class="amount">' . __('Based on contract') . '</span>';

    if( $order->get_line_subtotal( $item ) > 0 )
        return $subtotal;
    else
        return $free_label;
}

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

這篇關于用 Woocommerce 3 中的自定義文本替換產品零顯示價格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
主站蜘蛛池模板: 欧美黄色大片在线观看 | 国产在线观看一区二区 | 国产精品伦理一区 | 欧美精品一区二区三区蜜臀 | 亚洲精品一区二区 | 亚洲精品乱码久久久久久按摩观 | 一区在线视频 | 91九色porny首页最多播放 | 国产做a爱免费视频 | 亚洲久草视频 | 男人的天堂亚洲 | www.天天操 | 午夜精品久久久久久久久久久久久 | 久久久久久久国产精品 | 欧美激情免费在线 | 成人精品久久久 | www免费视频 | 免费在线播放黄色 | 91精品久久久久久久 | 天堂中文在线播放 | 国产精品1区2区3区 欧美 中文字幕 | 亚洲国产精品99久久久久久久久 | 香蕉二区| 国产免费一区二区 | 亚洲一二三区精品 | 国产精品久久片 | 久久久久久久国产 | 日本精品视频一区二区 | av在线一区二区三区 | 亚洲精品乱码久久久久久蜜桃 | 久久久久久久综合 | 欧美日韩国产一区 | 国产伊人精品 | 亚洲精品麻豆 | 黄色网址免费看 | 成人av网站在线观看 | 精品日韩一区二区 | 新91 | 亚洲欧美在线一区 | 九九综合| 精品日韩一区 |