Post by Brettster » Fri Jun 20, 2014 10:07 pm

Hi,

I think I have the right place to post this. I'm trying to customize OC 1.5.6.1, specifically the Add To Cart controller, but I'm having a very hard time trying to do something.

Can someone please help me out? I'm trying to limit Add To Cart on a specific group of Products (which I match by Product_ID), that group of products can ONLY ALLOW A MAXIMUM OF 1 PRODUCT be added to the cart. So, I tried adding some code to catalog->controller->checkout->cart.php (right after $product_info= at top of public function add()) as follows:

Code: Select all

// BGS - MAKE SURE ONLY 1 DIGITAL PRODUCT IN CART
		$bgsalreadydigital = 0;
		$arrdigital = array(66, 67, 71, 72);
		if (in_array($product_id, $arrdigital)) {
			// DIGITAL PRODUCT - CHECK TO MAKE SURE NO OTHERS EXIST IN CART
			if ($this->cart->hasProducts()) {
				$this->data['products'] = array();
				$products = $this->cart->getProducts();
				foreach ($products as $product) {
					if (in_array($product['order_product_id'], $arrdigital)) {
						$bgsalreadydigital = 1;
						break;
					}
				}
			}
		}
Then right after that, if I've found this product is already in the cart, I want to display a status message, so I added this code (I'm not sure which message I should call here?):

Code: Select all

		if ( $bgsalreadydigital ) {
// BGS NOTIFY ONLY 1 DIGITAL PRODUCT ALLOWED IN CART
			$this->data['attention'] = "Only One Digital Product is Allowed in Cart at one time!";
			$json['error']['warning'] = "Only One Digital Product is Allowed in Cart at one time!";
			$this->response->setOutput(json_encode($json));
// END BGS NOTIFY ONLY 1 DIGITAL PRODUCT ALLOWED IN CART
		} else {
But - the sad part is, all this code seems to get ignored? Is there a problem with this code, or why would it get ignored?

Then I found the actual controller exists over in: system->library->cart.php

Should I be modifying that core file, or am I correct in modifying catalog->controller->checkout->cart.php? And, finally, if that is the right module to be modifying, why is my code getting ignored?

Thanks for any help in advance,
-Brett

Newbie

Posts

Joined
Mon Mar 03, 2014 2:14 pm

Post by straightlight » Fri Jun 20, 2014 10:35 pm

Another way to do it would be by creating a module which would compare each product ID with each customer group ID. Then, once that specific group exceeds the limit, by adding products to the cart, to disable the Add To Cart button from jQuery code. ;)

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Brettster » Fri Jun 20, 2014 10:53 pm

Hi,
to disable the Add To Cart button from jQuery code.
Thanks, does anyone know if there already a module out there that does that? I'm new to OC development and don't think I have the chops to create a module, that's why I've been coding inline - directly in the files.

On the other hand, I'm not sure just disabling the Add To Cart will work, at least not unless there is a customizable message that I can have appear when they click on Add To Cart?

If there is already a module that works like that, I could purchase it - does anyone know? If not, I'm back to understanding how the code above works?

Thanks,
-Brett

Newbie

Posts

Joined
Mon Mar 03, 2014 2:14 pm

User avatar
Active Member

Posts

Joined
Mon Mar 28, 2011 4:08 pm
Location - UK

Post by billynoah » Mon Jun 23, 2014 5:28 am

@grgr, you would know better than me since you're the author but doesn't that mod only work per product on and not for a product group?

@brettster: It looks like you're on the right track to me. A couple words of advice:
1) Install Firebug with FirePHP (http://www.firephp.org/) and start using it. This will make your life a heck of a lot easier when you're trying to figure out what various functions return and what data arrays and variables hold.
2) I'd probably consider creating a (optionally disabled) category to which all your limited products belong. Then you can create your "arrdigital" array by pulling a list of products from the product_to_category table. Might make life easier if you want to be able to change which products are in that group directly from your admin panel. Also could be a stepping stone to a full fledged extension.
3) Learn vQmod and consider using it. It's a relatively easy system and makes debugging extension conflicts a heck of alot easier down the road since you can disable massive chunks of code that are sprawl over multiple files in one easy step.

Once you've got FirePHP installed and working you can log your arrays and variables to console. Do this is conjunction with javascript console logging of your Ajax response and I have no doubt you'll find the problem spot in your code. Don't be afraid to push forward and work this out for yourself! You might spend way too long getting there but once you do you'll have a bunch of new knowledge and programming tools in your toolbelt.

Good Luck!

Image


Active Member

Posts

Joined
Tue Jan 15, 2013 12:46 pm

Post by Brettster » Wed Jun 25, 2014 3:23 am

Hi,

@grgr - thanks, I used some your module for the basis of my code. I got it working, but I also had to remove the ability to Update/Add to Quantities on the View Cart page, overall I'm happy with how it turned out, works a charm.

@billynoah:
Once you've got FirePHP installed and working you can log your arrays and variables to console. Do this is conjunction with javascript console logging of your Ajax response and I have no doubt you'll find the problem spot in your code. Don't be afraid to push forward and work this out for yourself! You might spend way too long getting there but once you do you'll have a bunch of new knowledge and programming tools in your toolbelt.
Thanks, appreciate the advice. Maybe you could answer a few questions for me
----
1.) Do you develop on localhost? (if so, which do you use, I think WAMP is what I'm doing now with WordPress, just need to move over my OpenCart install to it, and change my Windows Host file so that all of my licenses, etc, think I'm coming from my domain, does that all sound right to you?
2.) Regarding FirePHP, thanks for the tip, I use Firebug all the time, so having FirePHP will really help. As for logging to console, etc, I'll let you know how that goes, I've had problems with jquery, etc, and watching logged variables.
3.) On VQMOD, I've come to learn it, and I can just usually rip out, or change what is in there, if I have to, but how are you developing (MVC) do you use an evnironment for that Aptana, or something? And then how are you saving it to VQMOD do you use that free tool that creates VQMOD from php? I seem to be missing a bit of the development environment, and I come from the stone-age - developing everything in text editors.
4.) I will consider making this an extension, it wouldn't be hard, as the coding is all done.

Thanks again,
-Brett

Newbie

Posts

Joined
Mon Mar 03, 2014 2:14 pm

Post by billynoah » Wed Jun 25, 2014 5:35 am

1.) Do you develop on localhost?
I have but nowadays I just use the server sitting in my living room :-). It's a $60 used dell running ubuntu 12.04. to be honest it's about as fast as most vps's i've seen... especially since the bandwidth is a non issue on LAN. i don't do windows .. if i had a pc i'd just install linux but that's where i'm comfortable.

firephp is really easy. you just install the libraries, and the firebug extension and then in your code you can do things like this:

$a = this->model_catalog_product->getTotalProducts($data);
FB::log($a,"These are the total products");

and voila, it will show up in your firebug log as an array.

For writing vqmods i just use dreamweaver with a bunch of snippets to surround code with necessary xml. for instance.. say you want to insert some code after the line which contains:

Code: Select all

public function getCategory
I just paste it in, highlight it and click my ADD snippet and it transforms it into:

Code: Select all

<search position="after"><![CDATA[public function getCategory]]></search>
You only need a couple of these and i'm sure other editors have a similar function. maybe better ways but that's how i do it. one thing to keep in mind is use the minimal search you need to uniquely match. that really helps avoid breakage if by chance there is an extra space or something gets slightly changed. especially if you are writing with the public in mind.

Image


Active Member

Posts

Joined
Tue Jan 15, 2013 12:46 pm
Who is online

Users browsing this forum: No registered users and 5 guests