Page 1 of 1
Product purchase only for a specific user group possible?
Posted: Thu Sep 05, 2024 3:36 pm
by daliose
Hi,
I am looking for a way to show products to all users, but without seeing prices and the ability to purchase.
If a user has logged in and is member of a specific user group, the user should be possible to purchase, depend on the group related prices.
Any hint, how to get this setup working?
Thanks
Maik
Re: Product purchase only for a specific user group possible?
Posted: Thu Sep 05, 2024 8:02 pm
by IP_CAM
Well, before posting here, better read this about this:
viewtopic.php?t=200480
Re: Product purchase only for a specific user group possible?
Posted: Thu Sep 12, 2024 12:20 am
by scottyboyyy
daliose wrote: ↑Thu Sep 05, 2024 3:36 pm
Hi,
I am looking for a way to show products to all users, but without seeing prices and the ability to purchase.
If a user has logged in and is member of a specific user group, the user should be possible to purchase, depend on the group related prices.
Any hint, how to get this setup working?
Thanks
Maik
Hi Maik,
There are probably many ways to achieve this setup in OpenCart 3, I personally would:
In catalog/controller/product/product.php and catalog/controller/product/category.php, locate where the product prices are set:
Code: Select all
$data['price'] = $this->currency->format($product_info['price'], $this->session->data['currency']);
And replace this with a condition to check whether user is logged in and the correct user group (replace 1 with your group):
Code: Select all
if ($this->customer->isLogged() && in_array($this->customer->getGroupId(), 1)) {
$data['price'] = $this->currency->format($product_info['price'], $this->session->data['currency']);
$data['show_price'] = true;
$data['can_purchase'] = true;
} else {
$data['price'] = '';
$data['show_price'] = false;
$data['can_purchase'] = false;
}
This will allow you to display different content depending on if it is a user or not and that they match the user group or not.
Then in your view folder (.twig files), product.twig and category.twig, find {{price}} and wrap it in an if statement:
Code: Select all
{% if show_price %}
<p class="price">{{ price }}</p>
{% else %}
<p><a href="link to account">Login to see prices</a></p>
{% endif %}
And for your button:
Code: Select all
{% if can_purchase %}
<button type="button" class="btn btn-primary">Add to Cart</button>
{% else %}
<button type="button" class="btn btn-secondary" disabled>Login to purchase</button>
{% endif %}
Additional work would be needed for having {{special}}, or different prices per user group, etc. But this basic setup should get you started.
Hope this helps.
Scott