【发布时间】:2022-07-07 18:13:25
【问题描述】:
我有一个由 5 个产品组成的产品包。所有这些产品都是可选的,但默认选中。因此,主产品显示为“From 900 kr”。而不是显示所选商品的价格。我正在使用 WooCommerces 自己的名为“WooCommerce 产品包”的插件。
总价已显示在捆绑选项下方,但我希望将其显示为主要价格区域。
我已经在可变产品类型上取得了成功,并认为我可以使用相同的代码并进行一些更改。可惜我没能成功。
我用于变量产品的代码:
// Show selected variation price in main price area
add_action('woocommerce_before_add_to_cart_form', 'selected_variation_price_replace_variable_price_range');
function selected_variation_price_replace_variable_price_range(){
global $product;
if( $product->is_type('variable') ):
?><style> .woocommerce-variation-price {display:none;} </style>
<script>
jQuery(function($) {
var p = 'p.price'
q = $(p).html();
$('form.cart').on('show_variation', function( event, data ) {
if ( data.price_html ) {
$(p).html(data.price_html);
}
}).on('hide_variation', function( event ) {
$(p).html(q);
});
});
</script>
<?php
endif;
}
有谁知道我如何做到这一点?
编辑:
我在产品页面上隐藏了主要价格,并将捆绑价格设置为看起来像主要价格。问题现在出在类别页面上,其中价格不显示捆绑包的默认选择变体的价格。它显示产品的最高可能价格,而不是默认捆绑选择价格。
这是来自捆绑插件的代码,它会导致最高价格显示:
/**
* 'woocommerce_bundle_force_old_style_price_html' filter.
*
* Used to suppress the range-style display of bundle price html strings.
*
* @param boolean $force_suppress_range_format
* @param WC_Product_Bundle $this
*/
if ( $suppress_range_price_html || apply_filters( 'woocommerce_bundle_force_old_style_price_html', false, $this ) ) {
$price = wc_price( $price_max );
$regular_price_min = $this->get_bundle_regular_price( 'max', true );
if ( $regular_price_min !== $price_max ) {
$regular_price = wc_price( $regular_price_max );
if ( $price_min !== $price_max ) {
$price = sprintf( _x( '%1$s%2$s', 'Price range: from', 'woocommerce-product-bundles' ), wc_get_price_html_from_text(), wc_format_sale_price( $regular_price, $price ) . $this->get_price_suffix() );
} else {
$price = wc_format_sale_price( $regular_price, $price ) . $this->get_price_suffix();
}
由于价格显示并有效地随着产品页面内的变化而变化,因此代码也必须可用于商店循环,我似乎找不到它。
【问题讨论】:
-
我刚刚测试了这段代码,对我来说效果很好。可以分享一下网址吗?
-
@Bhautik 该代码适用于可变产品类型,但不适用于捆绑产品类型。这是呃网址:staging.coolenergi.dk/produkt/…
标签: php wordpress woocommerce