如何更改OpenCart产品页面上的原始价格? [英] How can I change the original price on OpenCart product page?

查看:281
本文介绍了如何更改OpenCart产品页面上的原始价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不使用管理控制台.我希望能够在产品页面上的OpenCart中更改商品的价格.

Without using the Admin panel. I want to be able to change the price of an item in OpenCart on the product page.

基本上,我有一个名为Bespoke/Custom:的选项,它是一个文本字段.如果客户在此处输入任何内容,我希望能够更改已经通过jQuery完成的价格,然后希望该价格的新隐藏字段覆盖此客户订单的购物车价格

Basically I have an Option called Bespoke/Custom: which is a text field. If the customer enters anything here I want to be able to change the price which I do already via jQuery and then I want that new hidden field with the price to override the cart price for this customer order

有可能吗?有扩展名可以让客户输入自己的价格,然后我可以隐藏该字段并通​​过jQuery等进行更新

Is that possible? Is there an extension where can I allow the customer to enter their own price then I could hide this field and update via jQuery etc

这是对其他一些帖子的引用在OpenCart中使用备用价格字段以及有关此模型的链接 http://forum.opencart.com/viewtopic .php?t = 36052 可以显示主要oop函数的位置,但是使用它们的

This is reference to some other posts Using an alternate price field in OpenCart and also about this model link http://forum.opencart.com/viewtopic.php?t=36052 which shows where the main oop functions are but it is quite extensive to do it their

推荐答案

确定,为您指出正确的方向,这就是我的操作方式:

OK, to point You the right direction this is how I would do this:

1.隐藏的输入呈现器
如您所知,在catalog/view/theme/default/template/product/product.php中,存在将产品添加到购物车的AJAX请求:

1. hidden input render
As You may know, in a catalog/view/theme/default/template/product/product.php there is AJAX request for adding product into the cart:

$('#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.

If You look on the data parameter You'll see that all the inputs, selects, textareas etc. present in a .product-info div are populated and posted to PHP.

因此,我将具有自定义价格值的隐藏输入呈现到该.product-info div中,而不必完全修改AJAX请求.假设该输入的名称为.

Therefore I'd render the hidden input with custom price value into that .product-info div to not have to modify the AJAX request at all. Let's say the name of that input will be custom_price.

2. checkout/cart/add
打开catalog/controller/checkout/cart.php并搜索add方法.在这里,所有的魔术都应该完成.在这部分代码之后:

2. The checkout/cart/add
Open up catalog/controller/checkout/cart.php and search for add method. Here all the magic should be done. After this part of code:

            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()方法以满足您的要求...并在此处进行最后的编辑-更改此行:

Implement the isCustomPriceValid() method to meet Your requirements...and advance to the last edit here - change this line:

$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方法.您必须将方法的定义更改为此:

3. The cart
Now open up this file: system/library/cart.php and again search for the add method. You would have to change the definition of the method to this one:

public function add($product_id, $qty = 1, $option = array(), $custom_price = false) {

在此方法的最后一行代码之前,添加另一行:
(此代码是根据OP的评论进行编辑的)

Before the last line of code within this method, add another one:
(this code was edited due to the comment from the 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()中,因为这是从数据库加载所有数据并将其转发给其他控制器以供显示的方法.

The last edit should be within the method getProducts() as this one is loading all the data from the DB and forwards them to other controllers for displaying purposes.

现在,我不知道您的自定义价格是覆盖价格+期权价格还是仅覆盖价格,因此期权价格将被添加到其中,因此我坚持第二种选择,因为它更具描述性,并且首选可以很容易地从我的示例中得出.

Now I don't know whether Your custom price should overwrite the price + options price or only the price, thus options price will be added to it, so I'd stick with the second choice as it is more descriptive and the first choice could be easily derived from my example.

搜索行

$price = $product_query->row['price'];

添加之后

if(isset($this->session->data['cart']['custom_price'][$key])) {
    $price = $this->session->data['cart']['custom_price'][$key];
}

现在,价格应使用自定义价格覆盖.进一步检查产品价格,稍后将其设置为:

Now the price should be overwritten with the custom one. Check further that the price for the product is later set as:

$this->data[$key] = array(
    // ...
    'price'           => ($price + $option_price),
    // ...              
);

因此,如果您想用自定义价格覆盖整个价格,请在该数组之后(而不是在$price = ...;之后)添加该条件:

So if You'd like to overwrite the whole price with the custom one, add that condition right after that array like this (instead of after $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.这只能将您指向正确的方向(同时相信完成效果不会那么遥远).

This should be it. I didn't test the code, It may or may not work with slight modifications. I was working with OC 1.5.5.1. This should only point You to the right direction (while believing the finish is not that far).

享受!

这篇关于如何更改OpenCart产品页面上的原始价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆