Reputation: 759
I am trying to create a new 'Order Totals' extension for Opencart. The extension would add a discount to the shipping if a specific product is in the cart.
I have set up the new 'Order Totals' extension but I don't know how to check if the specific product in question is in the cart. So far I have:
if ($product['product_id'] == 108) { ... }
But this doesn't work. I presume I need to add more php in order to get all of the product id's in the cart but I simply don't have the knowledge.
Thank you
EDIT
Thank you to shadyyx and Jay Gilford, my code is now:
class ModelTotalKipper extends Model {
public function getTotal(&$total_data, &$total, &$taxes) {
$products = $this->cart->getProducts();
foreach ($products as $product) {
if($product['product_id'] == 108) {
if (($this->cart->getSubTotal() < $this->config->get('kipper_total')) && ($this->cart->getSubTotal() > 0)) {
$this->language->load('total/kipper');
$total_data[] = array(
'code' => 'kipper',
'title' => $this->language->get('text_kipper'),
'text' => $this->currency->format($this->config->get('kipper_fee')),
'value' => $this->config->get('kipper_fee'),
'sort_order' => $this->config->get('kipper_sort_order')
);
if ($this->config->get('kipper_tax_class_id')) {
$tax_rates = $this->tax->getRates($this->config->get('kipper_fee'), $this->config->get('kipper_tax_class_id'));
foreach ($tax_rates as $tax_rate) {
if (!isset($taxes[$tax_rate['tax_rate_id']])) {
$taxes[$tax_rate['tax_rate_id']] = $tax_rate['amount'];
} else {
$taxes[$tax_rate['tax_rate_id']] += $tax_rate['amount'];
}
}
}
$total += $this->config->get('kipper_fee');
break;
}
}
}
}
}
This works perfectly. Thanks again.
Upvotes: 1
Views: 873
Reputation: 16055
I guess all You need then is this:
class ModelTotalKipper extends Model {
public function getTotal(&$total_data, &$total, &$taxes) {
$products = $this->cart->getProducts();
foreach ($products as $product) {
if($product['product_id'] == 108) {
// do whatever You need here...
break;
}
}
}
}
If there will be more then one product to be discounted, then change the if
slightly:
if(in_array($product['product_id'], array(108, 109, 110))) {
// do whatever You need here...
} // no break; here in this case...
Upvotes: 1
Reputation: 15151
This line
$products = $this->cart->getProducts();
Needs to be after this line
public function getTotal(&$total_data, &$total, &$taxes) {
Upvotes: 2