PHP代码怎么实现价格区间,php – 在Woocommerce 3中更换价格区间处理默认变化显示价格...

在我们的woocommerce网站上,我试图根据客户从下拉菜单中选择的变化来更新显示的价格,如下所示:

rT02r.png

add_action( 'woocommerce_before_single_product', 'move_variations_single_price', 1 );

function move_variations_single_price(){

global $product, $post;

if ( $product->is_type( 'variable' ) ) {

// removing the variations price for variable products

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

// Change location and inserting back the variations price

add_action( 'woocommerce_single_product_summary', 'replace_variation_single_price', 10 );

}

}

function replace_variation_single_price(){

global $product;

// Main Price

$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );

$price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

// Sale Price

$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );

sort( $prices );

$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

if ( $price !== $saleprice && $product->is_on_sale() ) {

$price = '' . $saleprice . $product->get_price_suffix() . ' ' . $price . $product->get_price_suffix() . '';

}

?>

div.woocommerce-variation-price,

div.woocommerce-variation-availability,

div.hidden-variable-price {

height: 0px !important;

overflow:hidden;

position:relative;

line-height: 0px !important;

font-size: 0% !important;

}

jQuery(document).ready(function($) {

$('select').blur( function(){

if( '' != $('input.variation_id').val() ){

if($('p.availability'))

$('p.availability').remove();

$('p.price').html($('div.woocommerce-variation-price > span.price').html()).append('

'+$('div.woocommerce-variation-availability').html()+'

');

console.log($('input.variation_id').val());

} else {

$('p.price').html($('div.hidden-variable-price').html());

if($('p.availability'))

$('p.availability').remove();

console.log('NULL');

}

});

});

echo '

'.$price.'

'.$price.'
';

}

但是,当选择变体时,它会在From:$price下面显示undefined,如下所示:

未定义错误的示例:

pkLfm.png

因此,如果有人可以帮我确定此错误的原因,以便现在根据已选择的变体显示和更新价格,我们将不胜感激.

解决方法:

更新3

我做了一些重大改进和改进……这个代码版本也解决了默认情况下为变量产品选择变体(摆脱那个“未定义”错误……)并在特定情况下解决了一些其他错误的情况.

Note – The code might not work in some cases: For some themes or some plugins (like German Market for example) that make their own changes, using those hooks or changing the default html structure. In some cases your own customizations can be guilty.

这是新代码:

// Utility function to get the default variation (if it exist)

function get_default_variation( $product ){

$attributes_count = count($product->get_variation_attributes());

$default_attributes = $product->get_default_attributes();

// If no default variation exist we exit

if( $attributes_count != count($default_attributes) )

return false;

// Loop through available variations

foreach( $product->get_available_variations() as $variation ){

$found = true;

// Loop through variation attributes

foreach( $variation['attributes'] as $key => $value ){

$taxonomy = str_replace( 'attribute_', '', $key );

// Searching for a matching variation as default

if( isset($default_attributes[$taxonomy]) && $default_attributes[$taxonomy] != $value ){

$found = false;

break;

}

}

// If we get the default variation

if( $found ) {

$default_variaton = $variation;

break;

}

// If not we continue

else {

continue;

}

}

return isset($default_variaton) ? $default_variaton : false;

}

add_action( 'woocommerce_before_single_product', 'move_variations_single_price', 1 );

function move_variations_single_price(){

global $product, $post;

if ( $product->is_type( 'variable' ) ) {

// removing the variations price for variable products

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

// Change location and inserting back the variations price

add_action( 'woocommerce_single_product_summary', 'replace_variation_single_price', 10 );

}

}

function replace_variation_single_price(){

global $product;

// Main Price

$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );

$active_price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

// Sale Price

$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );

sort( $prices );

$regular_price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

if ( $active_price !== $regular_price && $product->is_on_sale() ) {

$price = '' . $regular_price . $product->get_price_suffix() . ' ' . $active_price . $product->get_price_suffix() . '';

} else {

$price = $regular_price;

}

// When a default variation is set for the variable product

if( get_default_variation( $product ) ) {

$default_variaton = get_default_variation( $product );

if( ! empty($default_variaton['price_html']) ){

$price_html = $default_variaton['price_html'];

} else {

if ( ! $product->is_on_sale() )

$price_html = $price = wc_price($default_variaton['display_price']);

else

$price_html = $price;

}

$availiability = $default_variaton['availability_html'];

} else {

$price_html = $price;

$availiability = '';

}

// Styles ?>

div.woocommerce-variation-price,

div.woocommerce-variation-availability,

div.hidden-variable-price {

height: 0px !important;

overflow:hidden;

position:relative;

line-height: 0px !important;

font-size: 0% !important;

}

jQuery(document).ready(function($) {

var a = 'div.wc-availability', p = 'p.price';

$('select').blur( function(){

if( '' != $('input.variation_id').val() ){

if($(a).html() != '' ) $(a).html('');

$(p).html($('div.woocommerce-variation-price > span.price').html());

$(a).html($('div.woocommerce-variation-availability').html());

} else {

if($(a).html() != '' ) $(a).html('');

$(p).html($('div.hidden-variable-price').html());

}

});

});

echo '

'.$price_html.'

'.$availiability.'
'.$price.'
';

}

代码位于活动子主题(或活动主题)的function.php文件中.经过测试和工作.

Tested in Woocommerce versions 3.2.x, 3.3.x and 3.4.x too…

笔记:

在以下情况下,代码处理这些特定情况:

>定义默认变体

>所有变化都具有相同的正常价格

>所有变化都具有相同的常规和销售价格

该代码还解决(在某些情况下)或增强:

>更好的可用性html结构和显示(如默认情况下)

>负载上不再显示延迟显示

>没有更多的可用性重复错误

>没有更多可用性“未定义”显示

标签:php,wordpress,woocommerce,price,variations

来源: https://codeday.me/bug/20190925/1816415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值