gpt4 book ai didi

php - 从 WooCommerce 中的变体产品获取自定义字段值和最高价格

转载 作者:行者123 更新时间:2023-12-05 06:15:35 25 4
gpt4 key购买 nike

我正在尝试显示从 X 到 X 的自定义价格跨度。

  • 最低价格来自(多个)自定义字段值,我需要使用最低值。
  • 最高价应为变体最高价。

如果变体具有 bulk_price 值,我只需要这个,并且只在存档页面中显示它。我需要获取自定义字段值和最高价格。

我的工作地点:

"How can I get Min and Max price of a woocommerce variable product in Custom loop? "

"WooCommerce: Get custom field from product variations and display it as a suffix to variation prices "


这是我的:

function change_product_price_display( $price) {
$bulk_price = get_post_meta([ 'variation_id' ], 'bulk_price', true);
$priceMax = $product->get_variation_price('max'); // Max price

//only show in archives
if (is_product_category()) {

//only if there is a bulk price
if ( $bulk_price ) {
return ' <span class="price-suffix">' . ' From ' . get_woocommerce_currency_symbol() .__( $bulk_price , "woocommerce") . ' - ' . $priceMax . '</span>';
}
}
//don't affect other products
else {
return $price;
}
}
add_filter( 'woocommerce_get_price_html', 'change_product_price_display');
add_filter( 'woocommerce_cart_item_price', 'change_product_price_display');

最佳答案

在产品类别存档中显示最低值(自定义字段)到最高价格。注释并在代码中添加解释

// 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 );

关于php - 从 WooCommerce 中的变体产品获取自定义字段值和最高价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62444714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com