Post by keijers » Wed Oct 05, 2011 7:35 pm

I want to show/hide products for different customer groups. So if the customer has logged in and he is in costumer group wholesale, he sees different products than somebody that is in another group. Is this possible and how?

Thanks!

New member

Posts

Joined
Wed Sep 14, 2011 9:29 pm

Post by Xsecrets » Wed Oct 05, 2011 9:08 pm

It is currently not possible without custom coding, and I know of no modules that do it, nor any place where anyone has given any instructions on how to do it.

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by newdigitalboy » Wed Jan 04, 2012 8:41 am

I have figured out a way to do this, but it's not pretty. It's also only useful if you don't have a lot of products. (I only have a few items that I sell.) I sell individual units to retail customers, but I sell the product wholesale by the case. I wanted these options to come up differently based on whether the customer was buying wholesale or retail. (In my code I use Wholesale and Default buyer groups.)

All the new code is added to theme\default\product\template\product\category.tpl.

To start, add this code just below <h1><?php echo $heading_title; ?></h1>

Code: Select all

<?php
$customer_group_id = $this->customer->getCustomerGroupId();
if (is_null($customer_group_id)) {
	$group_id_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_group WHERE name = 'Default'");
}
else {
	$group_id_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_group WHERE customer_group_id = $customer_group_id");
}
?>
The first line gets the current customer's group ID number (a number that represents Wholesale/Retail/Default, etc.) and stores it in the variable $customer_group_id. The next line checks to see if the result is NULL, which gets returned if a customer is not signed in. If NULL, then the group type is set to Default. Else, the customer_group_id is used to reference the fixed group names stored in "customer_group" in the database. We will use this below to tell us whether the customer is a Wholesale or Default customer.

Next, we need to compare the current customer's group name with the various product groups we want to show. So our code is basically asking, is the current customer a Wholesale customer or Default customer? The new code gets added immediately below <?php foreach ($products as $product) { ?> and looks like this:

Code: Select all

    <?php foreach ($products as $product) { ?>

	<?php
		if ((($group_id_query->row['name'] === 'Wholesale') &&
				($product['name'] === 'Product1'))
			||
			(($group_id_query->row['name'] === 'Default') &&
				(($product['name'] === 'Product2') ||
				($product['name'] === 'Product3'))))
		{
	?>
I've separated the lines out in the code so that it's easier to add other products in the future. So if the current customer is a Wholesale customer, only Product1 will show up. If the current customer is a Default customer, Product2 and Product3 will show up. These product names need to match the names you set up through OpenCart.

Finally, we need to add the closing bracket for our new if statement, and it gets added as shown below. The final code should look like this:

Code: Select all

    </div>
    <?php } ?>  <!-- end of our new if statement added here.-->
    <?php } ?>
  </div>
  <div class="pagination"><?php echo $pagination; ?></div>
Sorry that I didn't include line numbers, but my code is too modified and doesn't match the original file too much. There may be an easier way to do this, but I'm not a hardcore PHP/SQL programmer, so... Hope this helps!

- ndb
Using version 1.5.1.3

Newbie

Posts

Joined
Wed Jan 04, 2012 4:12 am
Who is online

Users browsing this forum: No registered users and 6 guests