如何在OpenCart产品页面上更改原始价格?
不使用管理面板。 我希望能够在产品页面上更改OpenCart中项目的价格。
基本上我有一个名为Bespoke/Custom:
的选项Bespoke/Custom:
这是一个文本字段。 如果客户在这里输入任何东西,我希望能够通过jQuery更改我已经做过的价格,然后我想要价格的新隐藏字段覆盖此客户订单的购物车价格
那可能吗? 是否有扩展,我可以允许客户输入自己的价格,然后我可以隐藏此字段并通过jQuery等更新
这是对其他一些post的引用在OpenCart中使用备用价格字段以及此模型链接http://forum.opencart.com/viewtopic.php?t=36052 ,其中显示了主要oopfunction的位置,但它非常广泛做他们的
好的,指出你正确的方向这是我将如何做到这一点:
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中存在的所有输入,选择,textareas等都已填充并发布到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()
因为这个加载来自数据库的所有数据并将它们转发到其他控制器以进行显示。
现在我不知道你的自定义价格是否应该覆盖价格+期权价格或只是价格,因此会增加选项价格,所以我坚持第二选择,因为它更具描述性,第一选择可以很容易从我的例子中得出。
搜索该行
$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。 这应该只指向正确的方向(同时认为完成不是那么远)。
请享用!
– “这应该只指向正确的方向(虽然相信结束不是那么远)。来自@shadyyx”
谢谢@shadyyx – 显示正确的方法……我设法让它工作,这是如何:
if(isset($this->session->data['cart']['custom_price'][$key])) { $this->data[$key]['price'] = $this->session->data['cart']['custom_price'][$key]; }
应该 :
if(isset($this->session->data['custom_price'][$key])) { $this->data[$key]['price'] = $this->session->data['custom_price'][$key]; }
再次感谢你,我希望有人觉得这很有用。