我正在使用opencart并成功为所有交易添加了最低订单价格。这是我使用的代码:
<?php if ($this->cart->getSubtotal() >= 10) { ?>
<div id="payment"><?php echo $payment; ?></div>
<?php } else { ?>
<div class="warning">Minimum 10 Euro to checkout</div>
<?php } ?>
现在我想从中排除一个类别,以便可以从该类别中购买 9 美元的产品。
更新 1:非常感谢你的帮助 沙迪克斯
我尝试了shadyyx方法,但出现此错误: unexpected T_BOOLEAN_OR
在这一行中
<?php if ($this->cart->getSubtotal() >= 10 || $this->cart->productsAreInCategory(1)) { ?>
更新2:我试过这个,但它给出了一个弹出窗口,说只是错误和确定按钮 <?php if (($this->cart->getSubtotal() >= 10) || $this->cart->productsAreInCategory(1)) { ?>
我试过这个 <?php if (($this->cart->getSubtotal() >= 10) || ($this->cart->productsAreInCategory(1))) { ?>
它没有给出任何错误并执行相同的工作(无论类别 ID 如何,所有订单的最低金额)
我会这样走:
扩展system/library/cart.php
并添加一个方法:
public function productsAreInCategory($category_id) {
$product_ids = array();
foreach($this->getProducts() as $product) {
$product_ids[] = $product['product_id'];
}
$categories = $this->db->query('SELECT category_id FROM ' . DB_PREFIX . 'product_to_category WHERE product_id IN (' . implode(',', $product_ids) . ')')->rows;
$category_ids = array();
foreach($categories as $category) {
$category_ids[] = $category['category_id'];
}
if(in_array($category_id, $category_ids) {
return true;
}
return false;
}
此方法应接受要测试的$category_id
参数,并应加载购物车中所有产品的类别。第一次匹配后返回 true,如果没有匹配项,则返回 false。您现在可以通过以下方式使用此方法:
<?php if (($this->cart->getSubtotal() >= 10) || $this->cart->productsAreInCategory(1)) { ?>
<div id="payment"><?php echo $payment; ?></div>
<?php } else { ?>
<div class="warning">Minimum 10 Euro to checkout</div>
<?php } ?>
只需将 $this->cart->productsAreInCategory(1)
中的类别 ID 替换为正确的类别 ID 即可。