不使用管理面板。我希望能够在产品页面上更改 OpenCart 中某件商品的价格。
基本上我有一个名为 Bespoke/Custom:
的选项,它是一个文本字段。如果客户在这里输入任何内容,我希望能够更改我已经通过 jQuery 执行的价格,然后我希望新的隐藏字段的价格覆盖该客户订单的购物车价格
这可能吗?有没有一个扩展,我可以让客户输入他们自己的价格然后我可以隐藏这个字段并通过 jQuery 等更新
这是对其他一些帖子的引用 Using an alternate price field in OpenCart还有关于这个模型的链接http://forum.opencart.com/viewtopic.php?t=36052它显示了主要的 oop 函数在哪里,但要完成它们的范围相当广泛
最佳答案
好的,为了给您指明正确的方向,我会这样做:
<强>1。隐藏输入渲染
您可能知道,在 catalog/view/theme/default/template/product/product.php
中有一个将产品添加到购物车的 AJAX 请求:
$('#button-cart').bind('click', function() {
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
dataType: 'json',
// ...
});
});
如果您查看 data
参数,您会看到 .product-info
div 中的所有输入、选择、文本区域等都已填充并发布到 PHP。
因此,我会将带有自定义价格值的隐藏输入呈现到 .product-info
div 中,这样根本不必修改 AJAX 请求。 让我们说该输入的名称将是 custom_price
。
<强>2。 checkout/cart/add
打开 catalog/controller/checkout/cart.php
并搜索 add
方法。所有的魔法都应该在这里完成。在这部分代码之后:
if (isset($this->request->post['option'])) {
$option = array_filter($this->request->post['option']);
} else {
$option = array();
}
我会添加这个:
if(isset($this->request->post['custom_price']) && $this->isCustomPriceValid($this->request->post['custom_price'])) {
$custom_price = $this->request->post['custom_price'];
} else {
$custom_price = false;
}
实现 isCustomPriceValid()
方法以满足您的要求...并前进到此处的最后编辑 - 更改此行:
$this->cart->add($this->request->post['product_id'], $quantity, $option);
到:
$this->cart->add($this->request->post['product_id'], $quantity, $option, $custom_price);
<强>3。购物车
现在打开这个文件:system/library/cart.php
并再次搜索 add
方法。您必须将方法的定义更改为此:
public function add($product_id, $qty = 1, $option = array(), $custom_price = false) {
在此方法的最后一行代码之前,添加另一行代码:
(此代码已根据 OP 的评论进行了编辑)
// ...
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;
}
$this->data = array(); // <- last line
}
最后的编辑应该在 getProducts()
方法中,因为这个方法从数据库加载所有数据并将它们转发给其他 Controller 以用于显示目的。
现在我不知道您的自定义价格是应该覆盖价格 + 期权价格还是只覆盖价格,因此期权价格将被添加到其中,所以我会坚持使用第二个选择,因为它更具描述性并且可以很容易地从我的示例中得出第一个选择。
搜索行
$price = $product_query->row['price'];
添加后立即
if(isset($this->session->data['cart']['custom_price'][$key])) {
$price = $this->session->data['cart']['custom_price'][$key];
}
现在价格应该被定制价格覆盖。进一步检查产品的价格稍后设置为:
$this->data[$key] = array(
// ...
'price' => ($price + $option_price),
// ...
);
因此,如果您想用自定义价格覆盖整个价格,请像这样在该数组之后添加该条件(而不是在 $price = ...;
之后):
if(isset($this->session->data['cart']['custom_price'][$key])) {
$this->data[$key]['price'] = $this->session->data['cart']['custom_price'][$key];
}
应该是这样的。我没有测试代码,它可能会或可能不会进行轻微修改。我正在使用 OC 1.5.5.1。这应该只会为您指明正确的方向(同时相信终点不远)。
尽情享受吧!
关于php - 如何更改 OpenCart 产品页面上的原价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18226794/