我目前在一个露天矿场工作。我已经编写了自定义价格计算php,这取决于按顺序选择的选项。当选择选项时,单选框或复选框都可以正常工作。但是
我有一个选项,客户可以在其中输入一个带文本字段的数字。我在价格计算中使用这个数字,所以根据这个值,opencart计算价格。我使用SESSION将这个数字发送到system/library/cart.php,在那里我使用它进行计算。当客户只订购一次时,这很好。
当客户的购物车中已经有产品,并且想要再下一个订单时,opencart将覆盖会话值,这将重新计算购物车中已有商品的价格。
有什么好的方法可以单独存储这个会话值吗?
这是我修改过的系统/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
);
您在系统文件中创建了if ($product_id == '65') {
。这意味着您必须为每个product id
编写代码,这是不可行的解决方案。
我的建议是,您应该创建一个像$this->session->data['custom_price']['your_product_id'] = value entered by customer
这样的会话数组。使用这个,你应该计算每个项目的价格。
opencart没有在您的购物车中创建单独的商品,而是在同一产品上加倍,这对您来说是个问题吗?我不清楚这是否是你的确切问题,但如果是,你可以通过修改系统/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试图在购物车中相乘产品时,它会干扰OpenCart的原始代码。从代码中可以看出,当您将另一个产品实例添加到购物车中时,您将删除OC对产品总数的查询,并防止其过度使用/增加同一产品的数量。相反,将添加一个单独的实例/项。它不会干扰客户从产品页面添加超过1个数量或在其查看车页面中增加数量的功能,以便在仍然需要的情况下保持不变。这只会影响相同产品id的产品添加到购物车中的方式。