Php 在WooCommerce中尝试获取产品价格时出现致命错误

Php 在WooCommerce中尝试获取产品价格时出现致命错误,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在做一个简单的弹出窗口,你们可以看到分期付款产品的价格。我写了一个简单的脚本。它能按我的要求工作。产品页面上有一个Button(弹出窗口)。一切都很好!但该网站的主页给出了致命错误。为什么我不知道。请帮忙!我正在使用WordPress v5.5.1和WooCommerce v4.5.2 <?php $product = wc_get_product( get_the_id()); $price = $produc

我正在做一个简单的弹出窗口,你们可以看到分期付款产品的价格。我写了一个简单的脚本。它能按我的要求工作。产品页面上有一个Button(弹出窗口)。一切都很好!但该网站的主页给出了致命错误。为什么我不知道。请帮忙!我正在使用WordPress v5.5.1和WooCommerce v4.5.2

    <?php
                  
       $product = wc_get_product( get_the_id());
       $price = $product->get_price();
       
        $deadline = 12;
        $rate = 25;

        $rate_sum = ($price/ 100) * $rate;
        $total_sum = $price+ $rate_sum;
        $base_per_month = $total_sum / $deadline;
 ?>
<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>


大梦想
Оплата за месяц
Срок (месяц)
Общая сумма
12
Паааааааааааааааааааааааааааа

也许您的$product是空的。您应该在执行操作之前检查它:

if(!empty($product)){
  $price = $product->get_price(); // and so on
}

“为什么我不知道。”-为什么,你需要去看看!我们不能从外部调查你的系统。每当你遇到500个内部服务器错误时,你首先要做的就是去查看错误日志。嗨,兄弟!它起作用了!非常感谢。但它不能正确地用于可变产品。它只有最低的价格,不是我选择的。我怎样才能解决它?