1
votes

I'm trying to show variation custom price-span from X-X.

  • The lowest price comes from (multiple) custom field values, I need to use the lowest value.
  • The highest price should be variation max price.

I only want this if the variation has the bulk_price value, and only show it in archives pages. I need to get the custom field value, and the price max.

I'm working from:

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

and

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


This is what I have:

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');
1
Before I answer your question. It may be more convenient for the full overview that you put your full code here for both creating the field and building on it. Now it has become a bit of a puzzle to compare the values ​​used for the custom field (from your previous question / answer) with those from this question, because you apparently renamed it? $add_field vs $bulk_price.7uc1f3r
I've updated the code. appreciate the answer.tiny
Okay thanks. So for clarity, you only want to show the lowest value of all custom fields for that specific variable product? for example, you have 3 custom fields 10, 20 & 30 and then display from 10 to max price?7uc1f3r
yes, the lowest possible price to the highest.tiny

1 Answers

1
votes

Display on product category archive lowest value (custom field) to max price. Comment with explanation added to the code

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

For clarity, the code for creating and saving the custom fields

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