Post by daliose » 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

Newbie

Posts

Joined
Thu Sep 05, 2024 3:33 pm

Post by IP_CAM » Thu Sep 05, 2024 8:02 pm

Well, before posting here, better read this about this: ;)
viewtopic.php?t=200480

My Github OC Site: https://github.com/IP-CAM
5'600 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland

Post by scottyboyyy » Thu Sep 12, 2024 12:20 am

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

Active Member

Posts

Joined
Fri Apr 07, 2017 2:36 am
Who is online

Users browsing this forum: effectgroup and 59 guests