【问题标题】:How to get product price from variable product in WooCommerce?如何从 WooCommerce 中的可变产品中获取产品价格?
【发布时间】:2020-10-07 04:26:49
【问题描述】:

我做了简单的弹出窗口,您可以在其中查看分期付款产品的价格。它工作正常。但在可变产品上,它只给出产品的最低价格。当然,我想获得我选择的产品价格。请帮忙!

    <?php
       $product = wc_get_product( get_the_id());
       if(!empty($product)){
       $price = $product->get_price();

        $deadline = 12;
        $rate = 25;

        $rate_sum = ($price/ 100) * $rate;
        $total_sum = $price+ $rate_sum;
        $base_per_month = $total_sum / $deadline;
} else {
        echo "product is empty" ;
};
            
?>
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content position-relative">
<div class="modal-header"><strong>Купить в рассрочку</strong></div>
<div class="modal-body">
<h3>Через MegaDream</h3>
<table class="table">
<thead>
<tr>
<th class="col" style="width: 33%;">Оплата за месяц</th>
<th class="col" style="width: 33%;">Срок (месяц)</th>
<th class="col" style="width: 33%;">Общая сумма</th>
</tr>
</thead>
<tbody>
<tr>
<td><b id="month_credit" style="color: #fe4f19; font-weight: 800;"><?php echo round($base_per_month); ?></b></td>
<td style="text-align: left;"><b style="color: #fe4f19; font-weight: 800;">12</b></td>
<td><b id="total_credit" style="color: #fe4f19; font-weight: 800;"><?php echo round($total_sum); ?></b></td>
</tr>
</tbody>
</table>
По вопросам получения товаров в рассрочку, звоните по номеру <strong>+998(99) 351-71-40</strong>
</div>
<div class="modal-footer"><a class="btn btn-primary" style="background-color: #457f33; color: white;" href="tel:+998993517140" type="button">Позвонить</a></div>
</div>
</div>

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    可变产品价格不同,因为可变产品可以有不同的变体价格,因此您可以获得最低价格和/或最高价格(或格式化的价格范围):

    $product = wc_get_product( get_the_id());
    
    if( is_a( $product, 'WC_Product' ) || ! empty( $product ) ) {
        
        // 1. Variable product (min and max)
        if ( $product->is_type('variable') ){
            $price_min = $product->get_variation_price('min'); // Float number
            $price_max = $product->get_variation_price('max'); // Float number
    
            $formatted_price_range = $product->get_price_html(); // Html formatted price range
        }
    
        // 2. Single product
        elseif ( $product->is_type('simple') ){
            $price = $product->get_price(); // Float number
    
            $formatted_price = $product->get_price_html(); // Html formatted price range
        }
    }
    

    现在应该可以更好地工作了。

    【讨论】:

    • 感谢您的回答,但它不适用于您所说的可变产品
    • @UNITAXI 抱歉,这个答案适用于简单的和可变产品。如果这个答案回答了你的问题,你可以请accept回答,谢谢。
    猜你喜欢
    • 2023-03-14
    • 2016-10-15
    • 2019-03-27
    • 2015-02-25
    • 2019-02-12
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多