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

Get custom field value and max price from variation product in WooCommerce

我正在尝试显示 X-X 的变体自定义价格跨度。

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

我的工作地点:

""

""


这是我的:

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