Jump to content

WHMCS Hide Certain Product from Public Visitor


nicktcy

Recommended Posts

On 02/01/2019 at 09:55, nicktcy said:

I am looking for a hook or product that could hide certain products from public visitor. Any idea?

you could try the following hook to remove specific products from the cart products pages for non-clients...

<?php

/**
* Hide Products Based On Loggedin Status
* @author brian!
*/
 
function cart_hide_product_hook($vars)
{
	$client = Menu::context('client');
	if ($vars['templatefile']=='products'){
		$products = $vars['products'];
		$removed = [45];
		if (!$client) {
			foreach ($products as $k => $item) {
				if (in_array($item['pid'],$removed)) {
					unset($products[$k]);
				}
			} 
			return array("products" => $products);
		}
	}
}
add_hook("ClientAreaPageCart", 1, "cart_hide_product_hook");

the only line that you should need to change is...

$removed = [45];

which contains the list of Product IDs that you want to hide to non-clients...

though bare in mind that they could still access the product via a direct url link if they know the pid - I daresay that the hook could be expanded to prevent that possibility if you thought it was essential to do so, but I wouldn't have thought it necessary.

Link to comment
Share on other sites

  • 1 year later...
On 1/4/2019 at 12:55 AM, brian! said:

you could try the following hook to remove specific products from the cart products pages for non-clients...


<?php

/**
* Hide Products Based On Loggedin Status
* @author brian!
*/
 
function cart_hide_product_hook($vars)
{
	$client = Menu::context('client');
	if ($vars['templatefile']=='products'){
		$products = $vars['products'];
		$removed = [45];
		if (!$client) {
			foreach ($products as $k => $item) {
				if (in_array($item['pid'],$removed)) {
					unset($products[$k]);
				}
			} 
			return array("products" => $products);
		}
	}
}
add_hook("ClientAreaPageCart", 1, "cart_hide_product_hook");

the only line that you should need to change is...


$removed = [45];

which contains the list of Product IDs that you want to hide to non-clients...

though bare in mind that they could still access the product via a direct url link if they know the pid - I daresay that the hook could be expanded to prevent that possibility if you thought it was essential to do so, but I wouldn't have thought it necessary.

thanks,  Just used this and very helpful    is there a way to expand it to have a message on the product page after youve hidden it all   at the moment its just blank

Link to comment
Share on other sites

10 hours ago, roxthaworld1 said:

is there a way to expand it to have a message on the product page after you've hidden it all   at the moment its just blank

<?php

/**
* Hide Products Based On Loggedin Status
* @author brian!
*/
 
function cart_hide_product_hook($vars)
{
	$client = Menu::context('client');
	if ($vars['templatefile']=='products'){
		$products = $vars['products'];
		$removed = [45];
		if (!$client) {
			foreach ($products as $k => $item) {
				if (in_array($item['pid'],$removed)) {
					unset($products[$k]);
				}
			}
			if (empty($products)) {
				$errormessage = Lang::trans('orderForm.errorNoProducts');
			}				
			return array("products" => $products, "errormessage" => $errormessage);
		}
	}
}
add_hook("ClientAreaPageCart", 1, "cart_hide_product_hook");

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated