好的,指出你正确的方向这是我将如何做到这一点:
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',
// ...
});
});
如果您查看数据参数您将看到.product-info div中存在的所有输入,选择,textareas等都已填充并发布到PHP.
因此,我将带有自定义价格值的隐藏输入呈现到.product-info div中,根本不需要修改AJAX请求.假设该输入的名称为custom_price.
2.结帐/购物车/添加
打开目录/ 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(); //
}
最后一次编辑应该在方法getProducts()中,因为这个加载来自DB的所有数据并将它们转发到其他控制器以进行显示.
现在我不知道您的自定义价格是否应该覆盖价格选项价格或仅覆盖价格,因此选项价格将被添加到它,所以我坚持第二选择,因为它更具描述性,第一选择可能是很容易从我的例子中得出.
搜索该行
$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.这应该只指向正确的方向(同时认为完成不是那么远).
请享用!