0

I am trying to implement a functionality in opencart where it will be possible to enter a custom price at the product page via text area and when item is added to the cart if custom price entered it will apply the price specified in the custom price field. Similar question was asked here someone kindly provided a good solution which applies to OpenCart 1.5.x. However I have tried to follow this approach on OpenCart 2 without any success. I have checked everything over and over for the last few days but I don't seem to be able to get this working as I am a newbie in programming world I wondering if anyone is able to point me to right direction to what I may be missing. I have search the web but unable to find any relevant information

I have checked and noticed that AJAX request is changed to #product div in 2.x, so I have enter my price input within this div underneath the quantity

<input name="custom_price" id="custom_price"  value=""  title="custom_price" class="input-text custom_price" type="textarea">

I have than moved on to the controller checkout/cart/add within the Add() method I have added this code

if(isset($this->request->post['custom_price'])) {
            $custom_price = $this->request->post['custom_price'];
        } else {
            $custom_price = false;
        }

Further down, I have changed this line

$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option, $recurring_id);

to:

$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option, $custom_price, $recurring_id);

Next, in the system/library/cart.php I have change the definition of the Add() method to the following

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

Before the end of the Add()method I have added the following

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;
    }

Within the GetProduct() I have added these lines

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

}

right after this line:

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

Finally at after the array where product price is set to price + option price

'price'           => ($price + $option_price),   

I have added the following

if(isset($this->session->data['custom_price'][$key])) {
$this->data[$key]['price'] = $this->session->data['custom_price'][$key];
}
Community
  • 1
  • 1
msd79
  • 11
  • 3
  • *to what I may be missing* - definitely programming skills. With them you could take the code working for 1.5.x and apply it to with correct changes for 2.x. Without them you are lost. – shadyyx Feb 16 '15 at 10:40
  • I totally agree with you. I am working on my programming skills but at the same time I am aware that I won't be able to make a giant leap over a week. With my current knowledge, I have tried and I believe I am very close but can't seem to figure out where is the problem. This of course doesn't mean that I will stop trying but any help would be greatly appropriated. – msd79 Feb 16 '15 at 11:16
  • ^^ *Appreciated*. *Would be greatly appreciated*. If you have tried something but failed, why not providing us with the code? I hope you understand the differences/changes between 1.5.x and 2.x and that you started to modify the *older* code to fit the *new* OC. Then provide us with the code you are trying to implement and what error(s) it produces. There is no better way to self-learning than try-fail-understand the problem-try-success. – shadyyx Feb 16 '15 at 12:52
  • That's correct, there is no better way to learn than where one tries to achieve something on their own. I have been trying to find a reference for differences between 1.5 and 2.x in respect to the relevant functions online without any success therefore I could not make any attempts to adapt the code to the version 2.x to tell you the truth. I will make the comparison by visually analysing functions in the old and new versions today and make another attempt to get this working that way. If I don't get any success I will come back with details error etc. – msd79 Feb 17 '15 at 10:12
  • To be honest, the most differences were done in the templates - the HTML is completely new as they are using Twitter Bootstrap 3 now. Controllers and Models didn't change that much. Maybe the core library classes a little. You can understand what has been changed easily by installing that module for 1.5.x under your 2.x and trying to fix each error. – shadyyx Feb 17 '15 at 14:59
  • Only major difference I could see is that the AJAX request has been changed to #product div, I have taken this into consideration but still could not get it working . I have checked it with Firebug and there is not error messages returned, it is just that custom price isn't applied. And this is the case even if I hard code the $custom_price variable to a value (i.e 20 ) within the Add method in checkout/cart/add – msd79 Feb 18 '15 at 00:28
  • I think you have a typo here: `$this >isCustomPriceValid($this->request->post['custom_price']))` - should be `$this->isCustomPriceValid($this->request->post['custom_price']))` - mind the `$this` **arrow** `isCustomPriceValid(...)`. – shadyyx Feb 18 '15 at 08:03
  • Also mind, that what you send into `Cart::ad()` is in this order: *1. product_id, 2. quantity, 3. option, 4. custom_price, 5. recurring_id* while what `Cart::add()` expects should come in this order: *1. product_id, 2. qty, 3. option, 4. recurring_id, 5. custom_price*. Do you see the difference? – shadyyx Feb 18 '15 at 08:07
  • I have removed the sanity check from there temporarily so it looks like this now `if(isset($this->request->post['custom_price'])) { $custom_price = $this->request->post['custom_price']; } else { $custom_price = false; }` and changed the order as below but not sure why it expects them in this order because $custom_price is implemented after the option and before the $recurring_id `$this->cart->add($this->request->post['product_id'], $this->request->post['quantity'], $option, $recurring_id, $custom_price); ` however still doesn't work – msd79 Feb 18 '15 at 10:04
  • I believe the if condition above returns false, how can i verify this for sure? – msd79 Feb 18 '15 at 10:30
  • Put in some `echo 'true';` in the `if` and/or `echo 'false';` in the `else` part, or use `var_dump($some_object_or_array)` to see the contents of the variable. Sometimes it is good to use `var_dump()` even on strings or booleans, because of case when empty string or false the PHP prints nothing to the browser. If it's an AJAX request, this will be printed to the response making the request to fail, so inspect the Console or Net tab of FireBug. – shadyyx Feb 18 '15 at 11:06
  • I have used `var_dump($custom_price);` and getting `bool(false)` which I believe proves my suspicion to be true. At this point I guess there is something wrong with way I setup things in product.tpl and product_price isn't getting posted – msd79 Feb 18 '15 at 11:38
  • I have managed get the $custom_price passed on to the checkeout/cart/add, it is coming through as string type and now i am getting this error. I have been trying to work around it without any success `unserialize(): Error at offset 0 of 8 bytes in system\library\cart.php line 179` `Undefined index: product_id system\library\cart.php in line 181` Any idea on this one at all ? – msd79 Feb 19 '15 at 20:08
  • This error is returned only if i enter a value in to the custom price text field – msd79 Feb 19 '15 at 20:14

0 Answers0