我正在尝试在 opencart 中实现一个功能,可以通过文本区域在产品页面上输入自定义价格,如果输入了自定义价格,当将商品添加到购物车时,它将应用自定义价格字段中指定的价格。此处提出了类似的问题,有人好心地提供了适用于 OpenCart 1.5.x 的良好解决方案。但是,我试图在 OpenCart 2 上遵循这种方法,但没有成功。在过去的几天里,我一遍又一遍地检查了所有内容,但我似乎无法使其正常工作,因为我是编程界的新手,我想知道是否有人能够为我指明正确的方向失踪。我已经在网上搜索但找不到任何相关信息
我检查并注意到 AJAX 请求在 2.x 中更改为 #product div,因此我在此 div 中的数量下方输入了我的价格输入
<input name="custom_price" id="custom_price" value="" title="custom_price" class="input-text custom_price" type="textarea">
我已经在 Add() 方法中转到控制器结帐/购物车/添加,我已经添加了这段代码
if(isset($this->request->post['custom_price'])) {
$custom_price = $this->request->post['custom_price'];
} else {
$custom_price = false;
}
再往下,我改变了这条线
$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option, $recurring_id);
至:
$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option, $custom_price, $recurring_id);
接下来,在system/library/cart.php
我将 Add()
方法的定义更改为以下
public function add($product_id, $qty = 1, $option = array(), $recurring_id = 0, $custom_price = false) {
在 Add() 方法结束之前,我添加了以下内容
if($custom_price) {
if(!isset($this->session->data['cart']['custom_price'])) {
$this->session->data['cart']['custom_price'] = array();
}
$this->session->data['cart']['custom_price'][$key] = $custom_price;
}
在 GetProduct() 中,我添加了这些行
if(isset($this->session->data['cart']['custom_price'][$key])) {
$price = $this->session->data['cart']['custom_price'][$key];
}
在这一行之后:
$price = $product_query->row['price'];
最后在产品价格设置为价格+期权价格的数组之后
'price' => ($price + $option_price),
我添加了以下内容
if(isset($this->session->data['custom_price'][$key])) {
$this->data[$key]['price'] = $this->session->data['custom_price'][$key];
}