I'm wondering how I can display the product price on the shop page. Right now my variable products are shown with their price range.
But these are both products that have been setup with default variable parameters:
When you click on a product, it's being shown as followed:
正如您所看到的,该特定选择的价格是€300
,我想知道如何显示它€300
在商店页面上而不是€150-€2.003
可以获取在可变产品中设置的默认变化价格并将其显示在商店和档案页面中:
add_filter( 'woocommerce_variable_price_html', 'custom_variable_displayed_price', 10, 2 );
function custom_variable_displayed_price( $price_html, $product ) {
// Only for archives pages
if ( ! ( is_shop() || is_product_category() || is_product_tag() ) )
return $price_html;
// Searching for the default variation
$default_attributes = $product->get_default_attributes();
// Loop through available variations
foreach($product->get_available_variations() as $variation){
$found = true; // Initializing
// 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;
}
}
// When it's found we set it and we stop the main loop
if( $found ) {
$default_variaton = $variation;
break;
} // If not we continue
else {
continue;
}
}
// If no default variation is found we exit.
if( ! isset($default_variaton) )
$price_html;
// Formatting the price
if ( $default_variaton['display_price'] !== $default_variaton['display_regular_price'] && $product->is_on_sale()) {
$price_html = '<del>' . wc_price($default_variaton['display_regular_price']) . '</del> <ins>' . wc_price($default_variaton['display_price']) . '</ins>';
} else {
$price_html = wc_price($default_variaton['display_price']);
}
return $price_html;
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
相关回答:显示 Woocommerce 3 上的默认变化价格和节省金额 https://stackoverflow.com/questions/50234781/display-the-default-variation-price-and-savings-amount-on-woocommerce-3/50239940#50239940
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)