wordpress 将折扣百分比添加到销售的可变产品

yduiuuwa  于 2022-11-22  发布在  WordPress
关注(0)|答案(1)|浏览(103)

我试图在使用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 . '%' );
  }
}

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

也不是在前端。

h43kikqp

h43kikqp1#

WooCommerce版本3+更新|不建议使用的替代品

  • 将“woocommerce变量销售价格”替换为“woocommerce变量获取价格”
  • 将“woocommerce销售价格”替换为“woocommerce获取价格”
  • 将“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+

相关答案:

  • 在产品销售时为价格添加自定义文本标签
  • 围绕产品销售价格和常规价格有条件定制输出

相关问题