1

I do have a rather complex problem with price calculations in WooCommerce using the plugin GravityForms.

I do sell mostly customized products offering engraving options. Thats why I need GravityForms for.

I wanted to replace the standard woocommerce price by the chosen variable price below the heading. For that I used the very good solution made by

@Loictheaztec Replace the variable price range by the chosen variation price

Here is his code I'am using in my functions.php

// Utility function to get the default variation (if it exist)
function get_default_variation( $product ){
$attributes_count = count($product->get_variation_attributes());
$default_attributes = $product->get_default_attributes();
// If no default variation exist we exit
if( $attributes_count != count($default_attributes) )
    return false;

// Loop through available variations
foreach( $product->get_available_variations() as $variation ){
    $found = true;
    // Loop through variation attributes
    foreach( $variation['attributes'] as $key => $value ){
        $taxonomy = str_replace( 'attribute_', '', $key );
        // Searching for a matching variation as default
        if( isset($default_attributes[$taxonomy]) && 
$default_attributes[$taxonomy] != $value ){
            $found = false;
            break;
        }
    }
    // If we get the default variation
    if( $found ) {
        $default_variaton = $variation;
        break;
    }
    // If not we continue
    else {
        continue;
    }
}
return isset($default_variaton) ? $default_variaton : false;
}

add_action( 'woocommerce_before_single_product', 
'move_variations_single_price', 1 );
function move_variations_single_price(){
global $product, $post;

if ( $product->is_type( 'variable' ) ) {
    // removing the variations price for variable products
    remove_action( 'woocommerce_single_product_summary', 
'woocommerce_template_single_price', 10 );

    // Change location and inserting back the variations price
    add_action( 'woocommerce_single_product_summary', 
'replace_variation_single_price', 10 );
}
}

function replace_variation_single_price(){
global $product;

// Main Price
$prices = array( $product->get_variation_price( 'min', true ), 
$product->get_variation_price( 'max', true ) );
$active_price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 
'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), 
$product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$regular_price = $prices[0] !== $prices[1] ? sprintf( __( 'From: 
%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( 
$prices[0] );

if ( $active_price !== $regular_price && $product->is_on_sale() ) {
    $price = '<del>' . $regular_price . $product->get_price_suffix() . 
'</del> <ins>' . $active_price . $product->get_price_suffix() . 
'</ins>';
} else {
    $price = $regular_price;
}

// When a default variation is set for the variable product
if( get_default_variation( $product ) ) {
    $default_variaton = get_default_variation( $product );
    if( ! empty($default_variaton['price_html']) ){
        $price_html = $default_variaton['price_html'];
    } else {
        if ( ! $product->is_on_sale() )
            $price_html = $price = 
wc_price($default_variaton['display_price']);
        else
            $price_html = $price;
    }
    $availiability = $default_variaton['availability_html'];
} else {
    $price_html = $price;
    $availiability = '';
}
// Styles ?>
<style>
    div.woocommerce-variation-price,
    div.woocommerce-variation-availability,
    div.hidden-variable-price {
        height: 0px !important;
        overflow:hidden;
        position:relative;
        line-height: 0px !important;
        font-size: 0% !important;
    }
</style>
<?php // Jquery ?>
<script>
jQuery(document).ready(function($) {
    var a = 'div.wc-availability', p = 'p.price';

    $('select').blur( function(){
        if( '' != $('input.variation_id').val() ){
            if($(a).html() != '' ) $(a).html('');
            $(p).html($('div.woocommerce-variation-price > 
span.price').html());
            $(a).html($('div.woocommerce-variation- 
availability').html());
        } else {
            if($(a).html() != '' ) $(a).html('');
            $(p).html($('div.hidden-variable-price').html());
        }
    });
});
</script>
<?php

echo '<p class="price">'.$price_html.'</p>
<div class="wc-availability">'.$availiability.'</div>
<div class="hidden-variable-price" >'.$price.'</div>';
}

Lets clarify a bit the things. Please see the screenshot to know where the woocommerce variations and the GravityForm fields are located.

Woocommerce variable price & GravityForms variable price

The css class of the woocommerce price is:

`<p class="price">
<span class="woocommerce-Price-amount amount">45&nbsp;</span>
<span class="woocommerce-Price-currencySymbol">€</span>
<span class="mwst"> inkl. MwSt.</span>
</p>`

And the css class of the GravityForms price is:

<li class="gfield">
  <label class="gfield_label">Total</label>
    <div class="ginput_container">
        <span class="formattedTotalPrice ginput_total">45,00&nbsp €</span>
    </div>
</li>

Now lets select a price variation from the woocommerce fields.

Woocommerce variable price selected

This outputs very nicely the selected variation price of woocommerce at top as well as on the bottom.

So now lets start the problem and select a variation price out of a GravityForms field.

Price variation selected by GravityForms

As you can see the price on top won't recognize the price change. But the price displayed at the lower end shows it correctly.

The question is now, is it possible to modify the code which replaces the variable prices by chosen variation price in a way that it will recognize price changes made via GravityForms too?

Update

I've searched through the documentation of gravity forms to find a filter hook for the price label. And I have found this one:

GravityForms Documentation - gform_product_price Filter

Then I checked what happens if I use this function in the functions.php file of my child theme.

add_filter( 'gform_product_price', 'set_price_label', 10, 2 );
function set_price_label( ) {
echo 'Some output somewhere?';   
}

or this function

add_filter( 'gform_product_price', 'set_price_label_test', 10, 2 );
function set_price_label_test( $sublabel, $form_id ) {
return 'Cost';
}

unfortunately nothing happened.

Here is an image of the result. Result of using GravityForms set_price_label filter

Max
  • 173
  • 5
  • 18

0 Answers0