2017-06-21 235 views
0

如何在單個產品頁面上顯示當前活動變體的woocommerce變量價格? 我使用的代碼:顯示woocommerce變量產品價格

   <?php 
       global $product; 
       if ($product->is_type('simple')) { ?> 
        <p class="price"><?php echo $product->get_price_html(); ?></p> 
       <?php } ?> 
       <?php 
       if($product->product_type=='variable') { 
        $available_variations = $product->get_available_variations(); 
        $variation_id=$available_variations[0]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation. 
        $variable_product1= new WC_Product_Variation($variation_id); 
        $regular_price = $variable_product1 ->regular_price; 
        $sales_price = $variable_product1 ->sale_price; 
        echo $regular_price+$sales_price; 
        } 
       ?> 

但只顯示最低的價格變化,而不是當前所選擇的變化的價格。 任何想法如何顯示活動變化的當前價格?

回答

1
<?php 
       global $product; 
       if ($product->is_type('simple')) { ?> 
        <p class="price"><?php echo $product->get_price_html(); ?></p> 
       <?php } ?> 
       <?php 
       if($product->product_type=='variable') { 
        $available_variations = $product->get_available_variations(); 
$count = count($available_variations)-1; 
        $variation_id=$available_variations[$count]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation. 
        $variable_product1= new WC_Product_Variation($variation_id); 
        $regular_price = $variable_product1 ->regular_price; 
        $sales_price = $variable_product1 ->sale_price; 
        echo $regular_price+$sales_price; 
        } 
       ?> 

試試這個。這可能會幫助你。

+0

感謝您的回答,但是這個顯示了變化的最高價格。如果我切換到另一個變化,它仍然顯示相同的價格:( –

+0

例如有2個變化:第一個成本7美元,第二個14美元。我的代碼總是顯示最低的價格,而你的 - 最高的,我需要改變動態價格:如果我選擇第一種變體,則顯示價格爲7美元,如果我選擇第二種 - 14美元 –

+0

您是否有該選定變體的唯一編號? –

1
<?php 
       global $product; 
       if ($product->is_type('simple')) { ?> 
        <p class="price"><?php echo $product->get_price_html(); ?></p> 
       <?php } ?> 
       <?php 
       if($product->product_type=='variable') { 
        $available_variations = $product->get_available_variations(); 
foreach($available_variations as $key=>$val){ 
    if(trim($val['variation_id'])==**"your selected variant id"**){ 
        $variation_id=$available_variations[$key]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation. 
    } 
} 
        $variable_product1= new WC_Product_Variation($variation_id); 
        $regular_price = $variable_product1 ->regular_price; 
        $sales_price = $variable_product1 ->sale_price; 
        echo $regular_price+$sales_price; 
        } 
       ?> 
+0

可能這可能會幫助您獲得選定的變體ID '$ variable_product1 = new WC_Product_Variation():: get_variation_id();' –

相關問題