我正在尝试为Opencart创建一个新的'Order Totals'扩展。如果特定产品在购物车中,该扩展会为运费添加折扣。
我已经设置了新的“订单总计”扩展程序,但我不知道如何检查相关的特定产品是否在购物车中。到目前为止,我有:
if ($product['product_id'] == 108) { ... }
但这不起作用。我认为我需要添加更多的php才能获得购物车中的所有产品ID,但我根本就没有这方面的知识。
谢谢
修改 的
感谢shadyyx和Jay Gilford,我的代码现在是:
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;
}
}
}
}
}
这完美无缺。再次感谢。
答案 0 :(得分:2)
这一行
$products = $this->cart->getProducts();
需要在这一行之后
public function getTotal(&$total_data, &$total, &$taxes) {
答案 1 :(得分:1)
我想你需要的就是:
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
:
if(in_array($product['product_id'], array(108, 109, 110))) {
// do whatever You need here...
} // no break; here in this case...