0

variation Product

Update : I am using an custom plug-in to set price as per user role and from product page i want to update taht price as based on my input fields.

I want to update product price on user input and selection on two pre defined variations as shown in the image

bug

Update 2: As per suggestion and code link i am able to get the price on cart page but if i enable the plugin as mentioned i am not able to update the total price of each product.

 add_action( 'woocommerce_before_variations_form', 'addCustomDeimensionAction', 5 );

    function addCustomDeimensionAction() { ?>
    <input type="number" name="dimensionLength" class="dimensionLength" placeholder="L">
    <input type="number" name="dimensionWidth" class="dimensionWidth" placeholder="W">
    <input type="hidden" name="customTotalCharge" class="customTotalCharge" value="">
    <?php 
    }

    // Get custom field value, calculate new item price, save it as custom cart item data
    add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 2 );
    function add_custom_field_data( $cart_item_data, $product_id ){
        $customTotalCharge = (float) sanitize_text_field( $_POST['customTotalCharge'] );
        $product = wc_get_product($product_id); // The WC_Product Object
        if($product->product_type=='variable') {
        $new_price =  $customTotalCharge;

        // Set the custom amount in cart object
        $cart_item_data['custom_data']['extra_charge'] = (float) $customTotalCharge;
        $cart_item_data['custom_data']['new_price'] = (float) $new_price;
        $cart_item_data['custom_data']['unique_key'] = md5( microtime() . rand() ); // Make each item unique
    }
    return $cart_item_data;
    }

Note :below filter is not working as in above plug in woocommerce_product_variation_get_price is used

// Set the new calculated cart item price 
add_action( 'woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 9999, 1 );
function extra_price_add_custom_price( $cart ) {
    if ( is_admin() && !defined('DOING_AJAX') )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cart_item['custom_data']['new_price']) ){
            echo $cart_item['custom_data']['new_price'];
            $cart_item['data']->set_price( floatval($cart_item['custom_data']['new_price']) );
        }
    }
}

so i am trying to update the price with same filer i.e woocommerce_product_variation_get_price but not able to get custom_data in this filter

// Display cart item custom price details
add_filter('woocommerce_cart_item_price', 'display_cart_items_custom_price_details', 999, 3 );
function display_cart_items_custom_price_details( $product_price, $cart_item, $cart_item_key ){
    if( isset($cart_item['custom_data']['extra_charge']) ) {
        $product = $cart_item['data'];
        $product_price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
        $product_price .= '<br>' . wc_price( $cart_item['custom_data']['extra_charge'] ).'&nbsp;';
        $product_price .= __("Extra Charge", "woocommerce" );
    }
    return $product_price;
}

add_action('wp_footer', 'get_variation_dump');
function get_variation_dump() {
?>
<script type="text/javascript">
    jQuery( document ).ready( function($) {
    jQuery( '.variations_form' ).each( function() {
        // when variation is found, do something
        jQuery(this).on( 'found_variation', function( event, variation ) {
            let dimensionLengthVal = jQuery('.dimensionLength').val();
            let dimensionWidthVal = jQuery('.dimensionWidth').val();
            let variationPrice = variation.display_price;
            const totalPrice = (dimensionWidthVal * dimensionLengthVal ) * variationPrice;
            jQuery('.customTotalCharge').val(totalPrice);
        });

    });

} );
</script>
inrsaurabh
  • 493
  • 7
  • 16
  • The only way to do that is to use jQuery + some additional other code (a hidden imput field) and cart manipulations. – LoicTheAztec Jul 19 '18 at 09:33
  • @LoicTheAztec i can get input fields values using js but not able to manipulate the cart, can you share any link or post related to this. – inrsaurabh Jul 19 '18 at 10:20
  • You need to set the calculated cost in the hidden field (with jQuery). This hidden field needs to be inside the add-to-cart form. Once done you can use something like in [**this answer code**](https://stackoverflow.com/questions/50450056/add-a-custom-product-calculated-price-to-woocommerce-cart/50454235#50454235), which will get the hidden field calculated cost and will set it in cart as custom data, to be able to change the product price with this custom cost. – LoicTheAztec Jul 19 '18 at 10:28
  • @LoicTheAztec thank you let me try this way, as your cuurent code is not working may due to https://wordpress.org/plugins/woocommerce-role-based-price/ this plugin which i am using for role based price system – inrsaurabh Jul 19 '18 at 10:58
  • As you are using a third party plugin for pricing, you get the limitations of this plugin… You should mention all that kind of details in your question, as nobody can guess that by magic. – LoicTheAztec Jul 19 '18 at 11:47
  • I am really sorry for that. – inrsaurabh Jul 19 '18 at 12:39

0 Answers0