好的,为了给你指明正确的方向,我会这样做:
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 请求。假设该输入的名称为 @987654326 @。
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。这应该只会将您指向正确的方向(同时相信终点不会那么远)。
享受吧!