久久久久久久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)
主站蜘蛛池模板: 久久com | 日韩中文字幕免费 | 欧美1页| 亚洲精品乱码久久久久久久久久 | 黄色在线播放视频 | 亚洲情综合五月天 | 亚洲精品久久久久久久久久久久久 | 久草院线 | 激情婷婷成人 | 玖玖视频 | 国产一区 在线视频 | 91精品国产综合久久久久久首页 | 欧美中文字幕一区二区三区 | 亚州成人| 亚洲精品黄 | 天天干天天操天天看 | 毛片网站免费观看 | 久久国产日韩 | 国产在线网站 | 国产一级精品毛片 | 五月婷婷色 | 欧美精品在线一区二区三区 | 免费观看羞羞视频网站 | 国内自拍偷拍一区 | 国产一区二区精品在线观看 | 国产91九色 | 国产精品伦一区二区三级视频 | 久久高清 | 国产乱码精品一区二区三区中文 | 亚洲一区二区三区免费在线观看 | 日韩三级在线观看 | 国产免费色 | 亚洲人久久 | 日韩国产中文字幕 | 国产成人久久精品一区二区三区 | 人人做人人澡人人爽欧美 | 国产精品免费大片 | 日本在线免费 | 日本网站在线看 | 国产一区二区精品在线 | 欧美精品一区二区三区在线 |