问题
I am using opencart and successfully added minimum order price for all transactions. This is the code I used:
<?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 } ?>
Now I want to exclude one category out of it so that $9 product from that category can be bought.
Update 1: Thank you so much for the help shadyyx
I tried shadyyx method but I am getting this error:
unexpected T_BOOLEAN_OR
in this line
<?php if ($this->cart->getSubtotal() >= 10 || $this->cart->productsAreInCategory(1)) { ?>
Update 2: I tried this but it gave a pop up saying just error and ok button
<?php if (($this->cart->getSubtotal() >= 10) || $this->cart->productsAreInCategory(1)) { ?>
I tried this
<?php if (($this->cart->getSubtotal() >= 10) || ($this->cart->productsAreInCategory(1))) { ?>
it did not give any error and does same work (min amount for all orders regardless of category id)
回答1:
I would go this way:
Extend the system/library/cart.php
and add a method:
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;
}
This method should accept a $category_id
parameter to test against and should load categories for all products in cart. After first match a true is returned, if no match, a false is returned. You can now use this method this way:
<?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 } ?>
Just replace the category ID in $this->cart->productsAreInCategory(1)
with the correct one.
来源:https://stackoverflow.com/questions/20042912/opencart-minimum-order-price-exclude-one-category