Hide add to cart for Guests

  • Posts: 51
  • Thank you received: 3
  • Hikashop Business
2 years 6 months ago #343751

-- HikaShop version -- : 4.6.1
-- Joomla version -- : 3.10.10
-- PHP version -- : 8.0.20
-- Browser(s) name and version -- : firefox

Hi,

I have a need to hide the add to cart button on certain products from guests and only show for registered users.
I want to allow guests to view the product details page and see the pricing.
I do not want them to be able to add the products to the cart.
There are a smaller number of products in which a guest can purchase.

On the products I want only registered users to purchase I added a price with ACL restrictions to allow only administrator registered, and super user.

I turned off the Display 'add to cart' button for free products.
I changed the translation file to FREE_PRICE="Members Only"

I need to change all the ACLs for numerous other products.

Can I do this in an SQL query?

UPDATE `fsw_hikashop_price` SET `price_access`= `,7,2,8,` WHERE `price_access` = `all`

And then manually reset the few products for guests access to the ACL = all in the price section.

Would this work and is it safe or do you have a better idea?

Thank You

Please Log in or Create an account to join the conversation.

  • Posts: 83338
  • Thank you received: 13476
  • MODERATOR
2 years 6 months ago #343760

Hi,

Yes, you can do it like that.
Note however that doing it like that will remove the price from these products for guests.

However, if you want to allow guests to see the prices while prevent them from adding the products to the cart, it would require create a custom product field so that you could select between yes/no for each product.
And then, in the view file where the add to cart button is being displayed, you could add some code around the add to cart button like that:

if($this->row->xxx == 'yes') {
 // code to display the add to cart button
}
where xxx is the column name of the custom field.
Now, it might require adapting a bit the code based on where you add it exactly.
To check which view file to edit to target the add to cart button, you can activate temporarily the "display view files" setting of the HikaShop configuration.

Please Log in or Create an account to join the conversation.

  • Posts: 51
  • Thank you received: 3
  • Hikashop Business
2 years 6 months ago #343774

Hi Nicolas,

I tried your method of adding a custom field of boolean type to the products. A couple of things I noted:

1. When I add the field with default value set to 'Yes' the value entered into the DB is Null for all products. They only add the default value of 'Yes' if I go to each product and then hit save. This is what I was trying to avoid having to do since their are 2000+ products and variants. Hence my SQL option.

2. I added the code as you suggested if($this->row->xxx == 'yes').
However, the boolean value for Yes is '1' not 'yes' so I had to modify the code accordingly.

I was also considering using a Joomla 'conditional content' plugin to hide/show the button based on the user access level. This would avoid having to edit every product one at a time but I was hoping for a more native solution.

On another note, is there any way to change the display order of multiple custom fields in the back-end form ?
They show in order of creation even if I change the order in the custom fields list.

Thank You

Please Log in or Create an account to join the conversation.

  • Posts: 83338
  • Thank you received: 13476
  • MODERATOR
2 years 6 months ago #343778

Hi,

1. You can run a quick mysql query to set the value of the custom field. For example:
UPDATE `fsw_hikashop_product` SET xxx= 'yes';
where xxx is the column name of the product custom field and "yes" the value you want to set for all the products.

2. Indeed, if you use boolean, then you need to use 1. I was thinking about a dropdown or a radio type where you add the possible values yourself.

3. Changing the order of the custom fields in the backend should do the trick. It works fine on my end at least. Did you click on the "save" icon at the top of the listing on the order column to validate your order modifications ?

Please Log in or Create an account to join the conversation.

  • Posts: 51
  • Thank you received: 3
  • Hikashop Business
2 years 6 months ago #343816

Hi Nicolas,

Adding the custom field does hide/show the button as you suggested. However, a custom field does not distinguish between Guests or Registered users. There would still have to be a check for the user level before showing the add to cart button. I which case the custom field would not be necessary if that check was added in the view file.

Instead of:
if($this->row->xxx == 'yes') {
// code to display the add to cart button
}

It would be something like
if ($user->id != 0) {
// code to display the add to cart button for only non guests
}

Or am I missing something?

Please Log in or Create an account to join the conversation.

  • Posts: 83338
  • Thank you received: 13476
  • MODERATOR
2 years 6 months ago #343818

Hi,

You're correct. I thought you already knew about this as what you wrote before meant you're quite knowledgable with Joomla and HikaShop so I skimmed over that part as it's quite common in Joomla coding world. My bad. There are several ways to do it:
joomla.stackexchange.com/questions/9546/...if-user-is-logged-in
In HikaShop, we usually use:

$user = JFactory::getUser();
if (!$user->guest) {
    echo "current user is logged in.";
}
So to integrate with the previous code:
$user = JFactory::getUser();
if ($this->row->xxx == 'yes' || !$user->guest) {
    echo "current user is logged in or product has 'yes' selected in the xxx custom field.";
}

Last edit: 2 years 6 months ago by nicolas.

Please Log in or Create an account to join the conversation.

  • Posts: 51
  • Thank you received: 3
  • Hikashop Business
2 years 6 months ago #343820

Hi,

OK, we are on the same page then. I just wanted to clarify since you skipped over that part in your reply.

Thank You.

Please Log in or Create an account to join the conversation.

  • Posts: 461
  • Thank you received: 36
2 years 4 months ago #345566

Hi guys,
another approach that I love is:

A - to create a "Product > Class" field, in this way we have an entire control on all the Product buttons etc.
@Nicolas - would be useful to consider it to be added by default during Product creation. A Class to inherit into all the Product views (like Product list, Product page etc.)

B - by using an External 3rd party like EngageBox you can build unlimited triggering with all the conditions you want

Really useful because you can also point on the right way the Guests / Users creating descriptions, instructions, forms etc.
Customizations are unlimited and without to have to override / change any Hika file...

Last edit: 2 years 4 months ago by joomleb.

Please Log in or Create an account to join the conversation.

Time to create page: 0.061 seconds
Powered by Kunena Forum