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

將折扣百分比添加到銷售的可變產(chǎn)品中

Adding the discount percentage to variable products on sale(將折扣百分比添加到銷售的可變產(chǎn)品中)
本文介紹了將折扣百分比添加到銷售的可變產(chǎn)品中的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我正在嘗試在使用 WooCommerce 的網(wǎng)站中添加折扣百分比價(jià)格.

I’m trying to add a discount percentage aside price in a site that uses WooCommerce.

我已將此腳本應(yīng)用于標(biāo)準(zhǔn)價(jià)格和銷售價(jià)格:

I’ve applied this script for the standard price and the sale price:

// Add save percentage next to sale item prices.
add_filter( 'woocommerce_get_price_html', 'adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){
  $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
  return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}

上面的腳本有效.

在前端,我有價(jià)格百分比.

In front-end, I have the price percentage.

現(xiàn)在我想對(duì)產(chǎn)品變化價(jià)格應(yīng)用相同的腳本.

Now I want apply the same script to the product variation price.

我檢查了產(chǎn)品變體選項(xiàng)并嘗試了以下操作:

I’ve checked the product variation option and tried something like this:

// Add save percentage next to sale item prices.
add_filter( 'woocommerce_get_price_html', 'adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){
  if( $product->is_type( 'variable' ) ) {
    $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
    return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
  }else{
    $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
    return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
  }
}

但它不起作用,百分比不適用于價(jià)格.

But it does not work, the percentage is not applied to the price.

前端也不行.

推薦答案

針對(duì) WooCommerce 版本 3+ 進(jìn)行了更新 |已棄用的替代品

  • 將woocommerce_variable_sale_price_html"替換為woocommerce_variable_get_price_html"
  • 將woocommerce_sale_price_html"替換為woocommerce_get_price_html"
  • 將woocommerce_price()"替換為wc_price()"
  • WC_Product 價(jià)格屬性替換為 WC_Product 價(jià)格方法
  • Replaced 'woocommerce_variable_sale_price_html' by 'woocommerce_variable_get_price_html'
  • Replaced 'woocommerce_sale_price_html' by 'woocommerce_get_price_html'
  • Replaced 'woocommerce_price()' by 'wc_price()'
  • Replaced WC_Product price properties by WC_Product price methods


可變產(chǎn)品更復(fù)雜,因?yàn)槟?2 個(gè)不同的價(jià)格位置,第一個(gè)顯示最小和最大價(jià)格(當(dāng)您有多個(gè)變體時(shí)),第二個(gè)顯示所選變體的價(jià)格.我對(duì)你的原始代碼做了很多更改.


For variable products is more complicated as you have 2 different locations with prices, the first one displays the minimal and maximal price (when you have multiple variations) and the second one displays the price from the selected variations. I have changed a lot your original code.

這是顯示折扣百分比周圍的自定義動(dòng)態(tài)標(biāo)簽的正確代碼:

Here the correct code to display that custom dynamic labels arround discounted percentages:

add_filter('woocommerce_variable_get_price_html','adventure_tours_sales_price', 10, 2 );
add_filter('woocommerce_get_price_html','adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){

    // Variables initialisation
    $regular_price = $product->get_regular_price();
    $sale_price    = $product->get_sale_price();
    $save_text     = __('Save', 'woocommerce') . ' ';

    if(!empty($sale_price)) {
        // Percentage calculation
        $percentage = '<span class="save-percent"> ' .$save_text . round( ( ( $regular_price -  $sale_price ) / $regular_price ) * 100 ) . '%</span>';

        $price = '<del class="strike">' . wc_price( $regular_price ) . '</del>
        <ins class="highlight">' . wc_price( $sale_price )  . $percentage . '</ins>';
    } else {
        $price = '<ins class="highlight">'.wc_price( $regular_price ).'</ins>';
    }
    return $price;
}

add_filter('woocommerce_variable_get_price_html', 'adventure_tours_sales_min_max_prices', 20, 2);
function adventure_tours_sales_min_max_prices( $price, $product) {

    // Variables initialisation
    $variation_min_reg_price = $product->get_variation_regular_price('min', true);
    $variation_max_reg_price = $product->get_variation_regular_price('max', true);
    $variation_min_sale_price = $product->get_variation_sale_price('min', true);
    $variation_max_sale_price = $product->get_variation_sale_price('max', true);
    $percentage_min = '';
    $percentage_max = '';
    $save_text     = __('Save', 'woocommerce') . ' ';

    // Percentage calculations
    if($variation_min_reg_price != $variation_min_sale_price)
        $percentage_min = '<span class="save-percent-min"> (' .$save_text . round( ( ( $variation_min_reg_price -  $variation_min_sale_price ) / $variation_min_reg_price ) * 100 ) . '%)</span>';
    if($variation_max_reg_price != $variation_max_sale_price)
        $percentage_max = '<span class="save-percent-max"> (' .$save_text . round( ( ( $variation_max_reg_price -  $variation_max_sale_price ) / $variation_max_reg_price ) * 100 ) . '%)</span>';

    if (($variation_min_reg_price != $variation_min_sale_price) || ($variation_max_reg_price != $variation_max_sale_price)) {
        $price = '<del class="strike">' . wc_price($variation_min_reg_price) . '-' . wc_price($variation_max_reg_price) .  '</del>
        <ins class="highlight">' . wc_price($variation_min_sale_price) . $percentage_min . ' - ' . wc_price($variation_max_sale_price) . $percentage_max . '</ins>';
    }
    return $price;
}

代碼位于活動(dòng)子主題(或主題)的functions.php 文件或任何插件文件中.

在 Woocommerce 版本 3+ 上測(cè)試并運(yùn)行

Tested and works on Woocommerce version 3+

相關(guān)答案:

  • 添加產(chǎn)品打折時(shí)的價(jià)格自定義文本標(biāo)簽
  • 圍繞產(chǎn)品的條件自定義輸出銷售價(jià)格和正常價(jià)格

這篇關(guān)于將折扣百分比添加到銷售的可變產(chǎn)品中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗(yàn)證問題中添加自定義注冊(cè)字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 尤物视频在线免费观看 | 国产一区成人 | 91网在线播放| 国产一级毛片视频 | 精品一区二区久久久久久久网站 | 欧美日日 | 久久国产综合 | 免费毛片网 | 久久国产视频一区 | 亚洲欧美日韩在线不卡 | 日本三级黄视频 | 国产在线小视频 | 婷婷激情在线 | 精品国产一区久久 | 国产成人一区二区 | 操久久 | 日韩网站在线观看 | 久久久精品日本 | 一级二级三级在线观看 | 成人在线观看免费 | 一区精品国产欧美在线 | 中文字幕日韩一区 | 亚洲欧美日韩在线 | 性高湖久久久久久久久3小时 | 香蕉久久av | 天天色综| 国产免费自拍 | 日韩电影中文字幕在线观看 | a级片播放| 欧美成人免费在线视频 | 日本人做爰大片免费观看一老师 | 国产成人免费 | 在线国产视频 | 在线观看黄视频 | 亚洲第一视频网站 | 国产精品爱久久久久久久 | 久久久久国色av免费观看性色 | hsck成人网 | 毛片在线视频 | 99av成人精品国语自产拍 | 亚洲精品自拍视频 |