在WooCommerce 3中用所选择的变化价格替换可变价格范围在WooCommerce上,我想更改Variable单一产品页面布局。因为,当您拥有可变产品时,您会在“变量产品”页面中获得此有线价格(在产品标题下方),并且它也会显示在商店页面中。对我来说,标准方法是在商店和产品页面显示产品的最低价格,并根据用户选择的变量更改价格。我不敢相信为什么。我可以使用此代码段删除价格范围并显示最低价格。https://businessbloomer.com/disable-variable-product-price-range-woocommerce/但话说回来,最低价格不会根据选择变量而改变。变量产品布局中还有两个价格。这是我当前的变量产品页面布局http://www.preorders.lk/product/beats-solo3-wireless-on-ear-headphones/因此,任何人都可以帮助从可变产品页面中删除价格范围,并仅显示产品的最低价格(在产品标题下)作为默认值。因此,价格应根据产品的变量而变化。并且最低价格也应该在商店页面中显示。希望这很清楚。如果有任何不清楚的地方,请告诉我。有关更多详细信息,请参阅附图。谢谢。
2 回答
噜噜哒
TA贡献1784条经验 获得超7个赞
我知道我在这里复活了一个旧线程,但是我在使用这段代码时发现的一点是,您需要注意使用此代码,因此您可能会停止在某些主题上显示单个非变量价格:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
在所有产品页面上触发,无论它是否为可变产品。
您可以使用以下版本,它只是在运行其余代码之前检查当前产品是否可变。
add_action( 'woocommerce_before_single_product', 'check_if_variable_first' );function check_if_variable_first(){
if ( is_product() ) {
global $post;
$product = wc_get_product( $post->ID );
if ( $product->is_type( 'variable' ) ) {
// removing the price of variable productsremove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );// Change location ofadd_action( 'woocommerce_single_product_summary', 'custom_wc_template_single_price', 10 );function custom_wc_template_single_price(){
global $product;// Variable product onlyif($product->is_type('variable')):
// 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 = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
}
?>
<style>
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;
}
</style>
<script>
jQuery(document).ready(function($) {
$('select').blur( function(){
if( '' != $('input.variation_id').val() ){
$('p.price').html($('div.woocommerce-variation-price > span.price').html()).append('<p class="availability">'+$('div.woocommerce-variation-availability').html()+'</p>');
console.log($('input.variation_id').val());
} else {
$('p.price').html($('div.hidden-variable-price').html());
if($('p.availability'))
$('p.availability').remove();
console.log('NULL');
}
});
});
</script>
<?php
echo '<p class="price">'.$price.'</p>
<div class="hidden-variable-price" >'.$price.'</div>';endif;}
}
}}- 2 回答
- 0 关注
- 409 浏览
添加回答
举报
0/150
提交
取消
