woocommercefiltermetadataselecteditemproduct-variations

Get woocommerce selected product variation custom meta_value in javascript


The price data of certain product variations are being sent by ton. I need to convert that data to kg and then multiply it by the product variation weight in kg. I have created a custom meta field called num_sal. When num_sal = 1000 then I must apply this calculation, otherwise I leave the price as is. I need to display this price on the single product page and also send the product price to the cart and checkout. My first idea was to change the meta_value in the database (divide by 1000) but how can I know when it has already been changed to the correct value in kg. If the price changes, they will send it in tons again. So I have been using javascript to show the variation price when selected

$(document).on('found_variation', ...

which works well. Now I need to change the price of those variations when, and only when, my custom metadata 'num_sal' = 1000. Here is my javascript code which displays the selected price correctly, but which doesn't have an if clause which takes into account this meta field. When this works, the selected variation price should be variation.display_price / 1000 * variation.weight

wc_enqueue_js( "     
   $(document).on('found_variation', 'form.cart', function( event, variation ) {
     if(variation.price_html)
     var variationprice = variation.display_price * 1.2;
     console.log(variation.display_price * 1.2);
     const formatter = new Intl.NumberFormat('fr-FR', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,            
      });

     var variationpriceTTC = formatter.format(variation.display_price * 1.2);
     console.log(variationpriceTTC);
     var variationpriceHT = formatter.format(variation.display_price);
     $('p.price').html(variationpriceHT + ' € HT - <span class=\"prixTTC\">' + variationpriceTTC + ' € TTC</span>');
     $('.woocommerce-variation-price').hide();
  });
  $(document).on('hide_variation', 'form.cart', function( event, variation ) {   
     $('p.price').html('" . $price . "');
  });
" );

Thanks in advance for any suggestions.


Solution

  • thanks for the suggestion. Here is the code that I used to make it work. I am using the woocommerce hooks 'woocommerce_product_variation_get_regular_price' and 'woocommerce_product_variation_get_price' thus I am not modifying metadata in the database, but only the product price on the single product level and the subsequent cart and checkout prices (I do not display product variation prices on the product list pages).

    I was deeply inspired by a post I found on StackOverflow: Change product variation prices via a hook in WooCommerce 3.3

    add_filter('woocommerce_product_variation_get_regular_price','custom_price_2', 99, 2 ); add_filter('woocommerce_product_variation_get_price', 'custom_price_2' , 99, 2 );

    // Variations (of a variable product)
    add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
    add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 99, 3 );
    
    
    function custom_price_2( $price, $product ) {
        // Delete product cached price  (if needed)
        wc_delete_product_transients($product->get_id());
        $product_id = $product->get_id(); 
        $weight_1000 = get_post_meta($product_id, 'num_sal', true) ;
        $variation_weight = $product->get_weight();
        if ($weight_1000 = "1000"){
            return $price / 1000 * $variation_weight; 
        }
        else {
            return $price;
        }   
    }
    
    function custom_variation_price( $price, $variation, $product ) {
        // Delete product cached price  (if needed)
        wc_delete_product_transients($variation->get_id());
        $weight_1000 = $variation->get_meta( 'num_sal' ) ;
        $variation_weight = $variation->get_weight();
        if ($weight_1000 = "1000"){
            return (float)$price / 1000 * (float)$variation_weight; 
        }
        else {
            return $price;
        }
    }