1 回答
TA贡献1883条经验 获得超3个赞
在产品类别存档中显示最低值(自定义字段)到最高价格。注释并在代码中添加解释
// Display on product category archive lowest value to max price
function change_product_price_display( $price, $product ) {
// Returns true when viewing a product category archive.
if ( is_product_category() ) {
// Set array
$bulk_prices = array();
// Loop for variations IDs
foreach( $product->get_children() as $variation_id ) {
// Get post meta
$bulk_price = get_post_meta($variation_id, 'bulk_price', true);
// True
if( $bulk_price ) {
// Push
$bulk_prices[] = $bulk_price;
}
}
// NOT empty
if( sizeof($bulk_prices) > 0 ) {
// Sort: low to high
sort($bulk_prices);
// First value
$lowest_value = reset( $bulk_prices );
// Get max price
$price_max = $product->get_variation_price('max');
// Output
$price = '<span class="price-suffix">From ' . get_woocommerce_currency_symbol() . $lowest_value . ' - ' . wc_price($price_max) . '</span>';
}
}
return $price;
}
add_filter( 'woocommerce_variable_price_html', 'change_product_price_display', 10, 2 );
为清楚起见,创建和保存自定义字段的代码
// Add custom field input product variations
function action_woocommerce_variation_options_pricing( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'bulk_price[' . $loop . ']',
'desc_tip' => 'true',
'description' => __( 'Enter the Bulk price here.', 'woocommerce' ),
'label' => __( 'Custom Field', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'bulk_price', true )
));
}
add_action( 'woocommerce_variation_options_pricing', 'action_woocommerce_variation_options_pricing', 10, 3 );
// Save custom field on product variation save
function action_woocommerce_save_product_variation( $variation_id, $i ) {
$bulk_price = $_POST['bulk_price'][$i];
if ( isset( $bulk_price ) ) {
update_post_meta( $variation_id, 'bulk_price', esc_attr( $bulk_price ) );
}
}
add_action( 'woocommerce_save_product_variation', 'action_woocommerce_save_product_variation', 10, 2 );
- 1 回答
- 0 关注
- 161 浏览
添加回答
举报