【问题标题】:Adding the discount percentage to variable products on sale将折扣百分比添加到销售的可变产品
【发布时间】:2017-02-14 15:28:34
【问题描述】:

我正在尝试在使用 WooCommerce 的网站中添加折扣百分比。

我已将此脚本应用于标准价格和销售价格:

// 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 . '%' );
}

上面的脚本有效。

在前端,我有价格百分比。

现在我想将相同的脚本应用于产品变化价格。

我检查了产品变体选项并尝试了以下方法:

// 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 . '%' );
  }
}

但它不起作用,百分比不适用于价格。

也不在前端。

【问题讨论】:

  • 这里没有人会读心术。在问题的正文中描述“仍然无效”的含义。错误信息?日志?有什么事吗?

标签: php wordpress woocommerce variations


【解决方案1】:

更新了 WooCommerce 版本 3+ |已弃用的替代品

  • 将“woocommerce_variable_sale_price_html”替换为“woocommerce_variable_get_price_html”
  • 将“woocommerce_sale_price_html”替换为“woocommerce_get_price_html”
  • 将“woocommerce_price()”替换为“wc_price()”
  • WC_Product 价格属性替换为 WC_Product 价格方法

对于可变产品更为复杂,因为您有 2 个不同位置的价格,第一个显示最低和最高价格(当您有多个变体时),第二个显示所选变体的价格。我对你原来的代码做了很多改动。

这里是显示自定义动态标签围绕折扣百分比的正确代码:

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;
}

代码进入您的活动子主题(或主题)的 functions.php 文件或任何插件文件中。

在 Woocommerce 版本 3+ 上测试并运行


相关答案:

【讨论】:

  • 嗨,我把这段代码放到了我的functions.php中——但是什么也没显示?我错过了什么吗...谢谢
  • 在较新版本的 woocommerce 中替换以下文本:将“woocommerce_variable_sale_price_html”替换为“woocommerce_variable_get_price_html”并将“woocommerce_sale_price_html”替换为“woocommerce_get_price_html”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 1970-01-01
相关资源
最近更新 更多