我目前正在开设一个opencart网站。我已经编写了自定义价格计算php,具体取决于按顺序选择哪些选项。这个工作正常,选择选项时,无线电ot复选框。但..
我有一个选项,客户可以在其中输入带有文本字段的数字。我在价格计算中使用此数字,因此根据此值,opencart会计算价格。我使用SESSION将此号码发送到system / library / cart.php,我将其用于计算。当客户订购一次时,此功能正常。
当客户已经在购物车中有产品并想要再次订购时,opencart将覆盖会话价值,这将重新计算购物车中已有商品的价格。
有没有什么好方法可以单独存储这个会话值?
这是我修改过的system / library / cart.php:
if ($product_id == '65') {
$pqty = $_SESSION['pqty'];
$contents = file_get_contents("http://somepage.com/system/library/calculate.php?&qty=".$quantity."&pqty=".$pqty."&pnh=".$pnh."&czp=".$czp."&czt=".$czt."&bnd=".$bnd."&covcho=".$covcho."&covczp=".$covczp."&covczt=".$covczt."&lam=".$lam);
$contents = utf8_encode($contents);
$calcprice = json_decode($contents);
$this->data[$key] = array(
'key' => $key,
'product_id' => $product_query->row['product_id'],
'name' => $product_query->row['name'],
'model' => $product_query->row['model'],
'shipping' => $product_query->row['shipping'],
'image' => $product_query->row['image'],
'option' => $option_data,
'download' => $download_data,
'quantity' => $quantity,
'minimum' => $product_query->row['minimum'],
'subtract' => $product_query->row['subtract'],
'stock' => $stock,
'price' => $calcprice / quantity,
'total' => $calcprice,
'reward' => $reward * $quantity,
'points' => ($product_query->row['points'] ? ($product_query->row['points'] + $option_points) * $quantity : 0),
'tax_class_id' => $product_query->row['tax_class_id'],
'weight' => ($product_query->row['weight'] + $option_weight) * $quantity,
'weight_class_id' => $product_query->row['weight_class_id'],
'length' => $product_query->row['length'],
'width' => $product_query->row['width'],
'height' => $product_query->row['height'],
'length_class_id' => $product_query->row['length_class_id'],
'recurring' => $recurring
);
答案 0 :(得分:2)
您在系统文件中创建了if ($product_id == '65') {
。这意味着你必须为每个product id
制作代码,这是不可行的解决方案。
我的建议是你应该创建一个像$this->session->data['custom_price']['your_product_id'] = value entered by customer
这样的会话数组。使用它你应该计算每个项目的价格。
答案 1 :(得分:0)
opencart的问题是不是在您的购物车中创建单独的商品而是在同一产品上加倍?我不清楚这是否是您的确切问题,但如果是 - 您可以通过修改system / library / cart.php文件中的以下内容来启用购物车中的重复产品:
在第290行附近,您可以更改:
public function add($product_id, $quantity = 1, $option = array(), $recurring_id = 0) {
$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' AND product_id = '" . (int)$product_id . "' AND recurring_id = '" . (int)$recurring_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
if (!$query->row['total']) {
$this->db->query("INSERT " . DB_PREFIX . "cart SET api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "', customer_id = '" . (int)$this->customer->getId() . "', session_id = '" . $this->db->escape($this->session->getId()) . "', product_id = '" . (int)$product_id . "', recurring_id = '" . (int)$recurring_id . "', `option` = '" . $this->db->escape(json_encode($option)) . "', quantity = '" . (int)$quantity . "', date_added = NOW()");
} else {
$this->db->query("UPDATE " . DB_PREFIX . "cart SET quantity = (quantity + " . (int)$quantity . ") WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' AND product_id = '" . (int)$product_id . "' AND recurring_id = '" . (int)$recurring_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
}
$this->data = array();
}
更改为:
// Modified to ENABLE duplicate product
public function add($product_id, $quantity = 1, $option = array(), $recurring_id = 0) {
$query = $this->db->query("INSERT " . DB_PREFIX . "cart SET api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "', customer_id = '" . (int)$this->customer->getId() . "', session_id = '" . $this->db->escape($this->session->getId()) . "', product_id = '" . (int)$product_id . "', recurring_id = '" . (int)$recurring_id . "', `option` = '" . $this->db->escape(json_encode($option)) . "', quantity = '" . (int)$quantity . "', date_added = NOW()");
$this->data = array();
}
// End
我使用此修改是因为我也有自己的自定义定价,当它试图将产品乘以购物车时,它会干扰OpenCart的原始代码。从代码中可以看出,当您将另一个实例添加到购物车中时,您将删除OC对产品总数的查询并防止其覆盖/增加同一产品的QTY。相反,将添加单独的实例/项目。它不会篡改客户从产品页面添加超过1个QTY或在其查看购物车页面中增加超过1个QTY的功能,以便在仍然需要的情况下仍保持相同。这只会影响相同产品ID的产品如何添加到购物车中。