【问题标题】:Get custom field value and max price from variation product in WooCommerce从 WooCommerce 中的变体产品中获取自定义字段值和最高价格
【发布时间】:2020-06-18 17:46:38
【问题描述】:

我正在尝试显示 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');

【问题讨论】:

  • 在我回答你的问题之前。将完整代码放在此处以创建字段并在其上构建可能更方便获得完整概述。现在将用于自定义字段的值(来自您之前的问题/答案)与来自此问题的值进行比较变得有点困惑,因为您显然已重命名它? $add_field$bulk_price.
  • 我已经更新了代码。感谢您的回答。
  • 好的,谢谢。因此,为了清楚起见,您只想显示该特定变量产品的所有自定义字段的最低值?例如,您有 3 个自定义字段 10、20 和 30,然后显示从 10 到最高价格?
  • 是的,从最低价到最高价。

标签: php wordpress woocommerce product custom-fields


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2019-05-09
    • 1970-01-01
    • 2019-09-19
    相关资源
    最近更新 更多