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
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'] ).' ';
$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>