在使用Elementor构建的可变产品页面上,如何用实际变动价格替换“从价格范围”?

问题描述

我已将此代码添加到functions.php中,以在产品价格旁边显示折扣百分比和折扣率(对于可变产品)。

// Always Display the selected variation price for variable products (already working)
add_filter( 'woocommerce_show_variation_price','filter_show_variation_price',10,3 );
function filter_show_variation_price( $condition,$product,$variation ){
    if( $variation->get_price() === "" ) return false;
    else return true;
}

// Remove the displayed price from variable products in single product pages only
add_action( 'woocommerce_single_product_summary','remove_the_displayed_price_from_variable_products',9 );
function remove_the_displayed_price_from_variable_products() {
    global $product;

    // Just for variable products
    if( ! $product->is_type('variable') ) return;

    // Remove the displayed price from variable products
    remove_action( 'woocommerce_single_product_summary','woocommerce_template_single_price',10 );
}

// Display the selected variation discounted price with the discounted percentage for variable products
add_filter( 'woocommerce_format_sale_price','woocommerce_custom_sales_price',3 );
function woocommerce_custom_sales_price( $price,$regular_price,$sale_price ) {

if ( !is_front_page() ) {
    global $product;
    
    // Just for variable products on single product pages
    if( $product->is_type('variable') && is_product() ) {

        // Getting the clean numeric prices (without html and currency)
        $regular_price = floatval( strip_tags($regular_price) );
        $sale_price = floatval( strip_tags($sale_price) );

        // Percentage calculation and text
        $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
        $savings = round( $regular_price - $sale_price );
        $percentage_txt = $percentage.__(' Discount ','woocommerce' );
        $savings_text = __(' Save ','woocommerce' ) . '₹' . $savings;

        return  '<span class="web-price">' . wc_price( $sale_price ) . '</span>' . '<span class="incl-text">' . '(inclusive of all taxes)' . '</span>' . '<br>' . '<del>' . '<span class="regular-price-text">' . wc_price( $regular_price ) . '</del>' . '</span>' . '<span class="discount-percentage">' .  $percentage_txt . '</span>' . '|' . '<span class="savings-text">' .  $savings_text . '</span>' ;
    }
    return $price;
}
}

将价格范围更改为类似“发件人:..”的代码如下:

add_filter( 'woocommerce_variable_price_html','bbloomer_variation_price_format',2 );
 
function bbloomer_variation_price_format( $price,$product ) {
 
// 1. Get min/max regular and sale variation prices
 
$min_var_reg_price = $product->get_variation_regular_price( 'min',true );
$min_var_sale_price = $product->get_variation_sale_price( 'min',true );
$max_var_reg_price = $product->get_variation_regular_price( 'max',true );
$max_var_sale_price = $product->get_variation_sale_price( 'max',true );
 
// 2. New $price,unless all variations have exact same prices
 
if ( ! ( $min_var_reg_price == $max_var_reg_price && $min_var_sale_price == $max_var_sale_price ) ) {   
   if ( $min_var_sale_price < $min_var_reg_price ) {
      $price = sprintf( __( 'From: <del>%1$s</del><ins>%2$s</ins>','woocommerce' ),wc_price( $min_var_reg_price ),wc_price( $min_var_sale_price ) );
   } else {
      $price = sprintf( __( 'From: %1$s',wc_price( $min_var_reg_price ) );
   }
}

// 3. Return $price
 
return $price;
}

现在这里面临的问题如下:

  1. 对于所有变体价格相同的可变产品,它不会显示实际的变体价格,而是显示以下内容:

Variable product with same price for all variations

产品网址:https://staging.websigma.in/sleepyhead/product/sleepyhead-original-3-layered-orthopedic-memory-foam-mattress/

  1. 对于价格随每个变化而有所不同的其他产品页面,将正确显示默认的“从..”,但是从下拉列表中选择任何变化时,价格和折扣将显示在“添加到购物车”按钮下方,如下所示:

Variable product with different prices for each variations & with the from range mentioned 产品网址:https://staging.websigma.in/sleepyhead/product/sleepyhead-flip-dual-sided-high-density-foam-mattress/

现在,我们要达到的目标是默认显示价格/范围内的价格。当任何人从下拉列表中选择变体时,“起始范围”应替换为变体价格和折扣。检查下面的参考设计 The Original Price should be repalced with the actual variation price & discount

有人请查看添加的代码段,看看代码出了什么问题?我想念什么吗?我被困住了,任何帮助将不胜感激!

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...