2016-09-20 38 views
0

我正在研究一些代碼以獲得所選變化價格*數量與總價格。獲取數量*變化價格

不知怎的,我不設法得到它的工作:

add_action('woocommerce_single_product_summary', 'woocommerce_total_product_price', 31); 
function woocommerce_total_product_price() { 
global $woocommerce, $product; 
// let's setup our divs 
echo sprintf('<div id="product_total_price" style="margin-bottom:20px; display: block;">%s %s</div>',__('Totaal Prijs:','woocommerce'),'<span class="price">'.$product->get_price().'</span>'); 
?> 
    <script> 
     jQuery(function($){ 
      var price = <?php get_post_meta(get_the_ID(), '_regular_price', true);?> 
      console.log(price); 
      var currency = '<?php echo get_woocommerce_currency_symbol(); ?>'; 

      $('[name=quantity]').change(function(){ 
        var product_total = parseFloat(price * this.value); 
        $('#product_total_price .price').html(currency + product_total.toFixed(2)); 


      }); 
     }); 
    </script> 
<?php } 

在這一個有什麼建議?

+0

不要你應該使用'$ this.val()''而不是this.value'? –

+0

你是對的,做到了這一點,但似乎它採用了錯誤的對象。當我添加parseint時,它不是一個數字。 – Pjelp

+0

'$(this).text();'或'$(this).attr(「value」)'也許,取決於你的jQuery版本 –

回答

0

解決了一些線索和錯誤後。可能更好,但它是在我身邊工作。 注意我在荷蘭我們使用的是,而不是。 (woocommerce也配置爲十進制..)。

總價基於變動@主題/ functions.php中

// hook this on priority 31, display below add to cart button. 
add_action('woocommerce_single_product_summary', 'woocommerce_total_product_variation_price', 31); 
function woocommerce_total_product_variation_price() { 
global $woocommerce, $product; 
// setup base html... We are going to rewrite this with the standard selected variation 
echo sprintf('<div id="product_total_price" style="margin-bottom:20px; display: block;">%s %s</div>',__('Totaal Prijs:','woocommerce'),'<span class="price">'.$product->get_price().'</span>'); 
?> 
    <script> 
     jQuery(function($){ 

     var currency = currency = ' <?php echo get_woocommerce_currency_symbol(); ?>'; 

      function priceformat() { 
       var product_total = parseFloat(jQuery('.woocommerce-variation-price .amount').text().replace(/ /g ,'').replace(/€/g ,'').replace(/,/g ,'.')) * parseFloat(jQuery('.qty').val()); 
       var product_total2 = product_total.toFixed(2); 
       var product_total3 = product_total2.toString().replace(/\./g, ','); 

       jQuery('#product_total_price .price').html(currency + ' ' + product_total3); 
      } 

      jQuery('[name=quantity]').change(function(){ 
       priceformat(); 
      }); 

      jQuery('body').on('change','.variations select',function(){ 
       priceformat();    
      }); 

      priceformat(); 

     }); 
    </script> 
<?php }