【问题标题】:Fatal error when trying to get product price in WooCommerce尝试在 WooCommerce 中获取产品价格时出现致命错误
【发布时间】:2020-10-05 12:32:37
【问题描述】:

我正在做一个简单的弹出窗口,您可以在其中查看分期付款的产品价格。我写了一个简单的脚本。它按我的意愿工作。产品页面上有一个按钮(弹出窗口)。一切都很好!但是网站的主页给出了致命的错误。为什么会这样,我不知道。请帮忙!我正在使用 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>

【问题讨论】:

  • “为什么我不知道。” - 为什么会这样,需要去看看!我们无法从外部查看您的系统。每当您收到 500 Internal Server 错误时,您必须做的第一件事就是检查错误日志的内容。

标签: php wordpress woocommerce


【解决方案1】:

也许您的 $product 是空的。你应该在行动之前检查它:

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

【讨论】:

  • 你好兄弟!有用!谢谢!但它不适用于可变产品。它只得到最低的价格,而不是我选择的。我该如何解决?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
  • 2020-05-05
  • 2021-02-04
  • 1970-01-01
  • 2015-02-25
  • 2016-02-11
相关资源
最近更新 更多