Jump to content
criat

If cart has product -> Show Cart Link

Recommended Posts

Hello,

 

I'm trying to do something like this:

 

<ul class="nav">
    <li>My menu link 1</li>
    <li>My menu link 2</li>
    <li>My menu link 3</li>
    {if $cartitems==0}
    {else}
         <li><a href="cart.php?a=checkout">You have $cartitems in your cart</a></li>
    {/if}
</ul>

 

That didn't work though. Any suggestions?

Share this post


Link to post
Share on other sites

What .tpl file are you trying to edit or at least can you print_r(); the content of $cartitems variable when the cart is not empty? I ask this because depens on the structure of $cartitems array. Anyway this code sounds better to me.

 

<ul class="nav">
    <li>My menu link 1</li>
    <li>My menu link 2</li>
    <li>My menu link 3</li>
    {if !$cartitems}
    {else}
         <li><a href="cart.php?a=checkout">You have $cartitems in your cart</a></li>
    {/if}
</ul>  

Edited by Kian

Share this post


Link to post
Share on other sites

It's header.tpl, and I just figured it doesn't seems to have the $cartitems variable by default, only on orderforms templates... What to do then?

Share this post


Link to post
Share on other sites
You have to look at the $_SESSION['cart'] array.

 

Thanks, that's a really big help!

 

Thank you also Kian, I didn't know what how to print_r $cartitems, and it's as SiteOx said: ['cart']

 

Added this to the top of my header;

 

					{php}
					session_start(); 
					 Print_r ($_SESSION['cart']);
					{/php}

 

And the output:

 

Array ( [domains] => Array ( [0] => Array ( [type] => register [domain] => teetete.net [regperiod] => 1 [eppcode] => ) [1] => Array ( [type] => register [domain] => teetete.com.br [regperiod] => 1 [eppcode] => ) [2] => Array ( [type] => register [domain] => teetete.com [regperiod] => 1 [eppcode] => ) [3] => Array ( [type] => register [domain] => teetete.org [regperiod] => 1 [eppcode] => ) [4] => Array ( [type] => register [domain] => tetetete.com.br [regperiod] => 1 [eppcode] => ) [5] => Array ( [type] => register [domain] => tetetete.org [regperiod] => 1 [eppcode] => ) ) [ns1] => ns1.mynameserver.com [ns2] => ns2.mynameserver.com [ns3] => [ns4] => [ns5] => )

 

I'm really noob at PHP, so now I'm studying how to check if array is empty or not, and then I can echo it. The other problem I'll face down the road is how to echo how many arrays there is, like "You have X items in your cart"

 

First try...

 

					{php}
					if( !empty($_SESSION['cart']) )
					{
						 echo "there's **************** in the cart";
					}
					{/php}

So far so good!!

Edited by criat

Share this post


Link to post
Share on other sites

Thanks Brian, I'm almost doing it though, see:

 

{php}
if( !empty($_SESSION['cart']) )
{
	$result = count($_SESSION['cart']);
	echo "There is $result items in your cart";
}
{/php}

 

var_dump on 'cart':

array(6) { ["domains"]=> array(1) { [0]=> array(7) { ["type"]=> string( "register" ["domain"]=> string(19) "sampledomain.com.br" ["regperiod"]=> int(1) ["eppcode"]=> NULL ["dnsmanagement"]=> NULL ["emailforwarding"]=> NULL ["idprotection"]=> NULL } } ["ns1"]=> string(16) "ns1.mynameserver.com" ["ns2"]=> string(16) "ns2.mynameserver.com" ["ns3"]=> string(0) "" ["ns4"]=> string(0) "" ["ns5"]=> string(0) "" }

 

The code above echoes "There is 6 items in your cart", even though there's only 1 item

 

If we solve the item counter it's done, and won't have to buy the open source module :)

Share this post


Link to post
Share on other sites

Here's where I stand so far:

 

					{php}
					if( !empty($_SESSION['cart']) )
					{
					$itemCount = count($_SESSION['cart']['domains'])
				   + count($_SESSION['cart']['other'])
				   + count($_SESSION['cart']['stuff']);	
						if ($itemCount > 1)
						echo "{/php}{$LANG.headercart}{php} $itemCount {/php}{$LANG.headercartitems}{php}";
						else
						echo "{/php}{$LANG.headercart}{php} $itemCount {/php}{$LANG.headercartitem}{php}";
					}
					{/php}

 

It works. But with {$LANG} (like above), it only outputs "?>", if I hardcode the text on it it works

 

A guy in stackoverflow PHP Chat suggested this, but it's trowing unexpected '[' at $productKeys = ['domains', 'other', 'stuff'];

 

$itemCount   = 0;
$productKeys = ['domains', 'other', 'stuff'];
foreach ($productKeys as $productKey)
{
   if (!empty($_SESSION['cart'][$productKey]))
   {
       $itemCount += count($_SESSION['cart'][$productKey]);
   }
}
echo "You have {$itemCount} items in your cart.";

Share this post


Link to post
Share on other sites

I think that you have inverted slashes.

 

					{php}
					if( !empty($_SESSION['cart']) )
					{
					$itemCount = count($_SESSION['cart']['domains'])
				   + count($_SESSION['cart']['other'])
				   + count($_SESSION['cart']['stuff']);	
						if ($itemCount > 1)
						echo "{php}{$LANG.headercart}{/php} $itemCount {php}{$LANG.headercartitems}{/php}";
						else
						echo "{php}{$LANG.headercart}{/php} $itemCount {php}{$LANG.headercartitem}{/php}";
					}
					{/php}

Share this post


Link to post
Share on other sites

Here's the 'almost' final version of this script:

 

{php}
global $smarty;
$itemCount = 0; 
$productKeys = array('domains', 'other', 'stuff'); 
$language = $smarty->get_template_vars('language');
	foreach ($productKeys as $productKey) 
		{ 
			if (!empty($_SESSION['cart'][$productKey])) 
			{ 
				$itemCount += count($_SESSION['cart'][$productKey]); 
			} 
		}
	if (($itemCount += count($_SESSION['cart'][$productKey])) > 1):
		echo "<a href='https://domain.com/checkout/go'><li class='header-icon'><div class='header-right-icon-cart'></div></li>";
		if ($language == us) {
			echo "<li class='header-right-chat'>{$itemCount} items</li></a>";
		} else {
			echo "<li class='header-right-chat'>{$itemCount} itens</li></a>";
		}
		elseif (($itemCount += count($_SESSION['cart'][$productKey])) == 1):
			echo "<a href='https://domain.com/checkout/go'><li class='header-icon'><div class='header-right-icon-cart'></div></li>";
			echo "<li class='header-right-chat'>{$itemCount} item</li></a>";
		endif;
{/php}

 

There's only one thing bothering me now:

 

Is there a way to check $language variable without using $global? I think it would be safer

 

PS: For the code to work on your whmcs install just:

 

  1. Paste the code above where you want it to show
  2. Add the other products in the array, where it says 'domains, 'other', 'stuff'
  3. Edit all the "echo" to match your site design
  4. Replace "if ($language == us)" with "if ($language == English)"

 

 

You are good to go!

Edited by criat

Share this post


Link to post
Share on other sites

it sorta worked on mine - though just realised I didn't change the array options *duh*

what should productkeys array be changed to?

 

also, i'm not sure it takes into account multiple quantities of the same products - but again that might be because the array isn't setup correctly.

Share this post


Link to post
Share on other sites

Here is my code. I place the following at the top of my template (it could go in header.tpl to be used everywhere):

 

{php}
 //Check product count
 foreach ($_SESSION['cart']['products'] as $prodkey => $prodval){
   //Make sure it is a configured product
   if ($prodval['noconfig'] != '1'){
     //Check product quantity
     if ($prodval['qty'] > 1){
       $cartcount = $cartcount + $prodval['qty'];
     }else{
       $cartcount = $cartcount + count($prodkey);
     }
   }
 }
 //Check domain count
 foreach ($_SESSION['cart']['domains'] as $domkey => $domval){
   //Check for dnsmanagement field to be sure it is a register or transfer being purchased
   if (array_key_exists('dnsmanagement', $domval)){
     $cartcount = $cartcount + count($domkey);
   }
 }
 //Check addon count
 foreach ($_SESSION['cart']['addons'] as $addkey => $addval){
   $cartcount = $cartcount + count($addkey);
 }
 //Create a template tag if cart items are found
 if ($cartcount){
   $this->_tpl_vars['cartcount'] = $cartcount;
 }
{/php}

 

Then, in the template, I can call "$cartcount" by doing the following:

 

{if $cartcount}{$cartcount}{/if}

 

This checks for all types of cart items (products, domains, and addons). It also takes into consideration product quantities.

 

Sean

Edited by SeanP

Share this post


Link to post
Share on other sites

Here's the complete array

 

$productKeys = array('domains', 'products', 'addons');

 

I can confirm it's fully working here, if I have 3 domains and 5 products in cart it will show 8 items

 

@SiteOX

 

I didn't try quantitative products yet, by looking the code can you tell if mine does?

Edited by criat

Share this post


Link to post
Share on other sites
Here is my code. I place the following at the top of my template (it could go in header.tpl to be used everywhere):

 

{php}
 //Check product count
 foreach ($_SESSION['cart']['products'] as $prodkey => $prodval){
   //Make sure it is a configured product
   if ($prodval['noconfig'] != '1'){
     //Check product quantity
     if ($prodval['qty'] > 1){
       $cartcount = $cartcount + $prodval['qty'];
     }else{
       $cartcount = $cartcount + count($prodkey);
     }
   }
 }
 //Check domain count
 foreach ($_SESSION['cart']['domains'] as $domkey => $domval){
   //Check for dnsmanagement field to be sure it is a register or transfer being purchased
   if (array_key_exists('dnsmanagement', $domval)){
     $cartcount = $cartcount + count($domkey);
   }
 }
 //Check addon count
 foreach ($_SESSION['cart']['addons'] as $addkey => $addval){
   $cartcount = $cartcount + count($addkey);
 }
 //Create a template tag if cart items are found
 if ($cartcount){
   $this->_tpl_vars['cartcount'] = $cartcount;
 }
{/php}

 

Then, in the template, I can call "$cartcount" by doing the following:

 

{if $cartcount}{$cartcount}{/if}

 

This checks for all types of cart items (products, domains, and addons). It also takes into consideration product quantities.

 

Sean

 

You forgot to add renewal domains

 

{php}
 //Check product count
 foreach ($_SESSION['cart']['products'] as $prodkey => $prodval){
   //Make sure it is a configured product
   if ($prodval['noconfig'] != '1'){
     //Check product quantity
     if ($prodval['qty'] > 1){
       $cartcount = $cartcount + $prodval['qty'];
     }else{
       $cartcount = $cartcount + count($prodkey);
     }
   }
 }
 //Check domain count
 foreach ($_SESSION['cart']['domains'] as $domkey => $domval){
   //Check for dnsmanagement field to be sure it is a register or transfer being purchased
   if (array_key_exists('dnsmanagement', $domval)){
     $cartcount = $cartcount + count($domkey);
   }
 }
 //Check addon count
 foreach ($_SESSION['cart']['addons'] as $addkey => $addval){
   $cartcount = $cartcount + count($addkey);
 }
 //Check renewals count
 foreach ($_SESSION['cart']['renewals'] as $addkey => $addval){
   $cartcount = $cartcount + count($addkey);
 }
 //Create a template tag if cart items are found
 if ($cartcount){
   $this->_tpl_vars['cartcount'] = $cartcount;
 }
{/php}

Share this post


Link to post
Share on other sites

Now I'm facing a bigger issue

 

I'm trying to adapt it to work on my main website (wich is HTML/PHP, not WHMCS), how can I do it? I've googled a lot but I'm lost. My main website is on the same domain as my WHMCS

 

@druptech

Thanks for the tip

Edited by criat

Share this post


Link to post
Share on other sites

Did it!

 

		<?php
			require("../dbconnect.php");
			require("../includes/functions.php");
			require("../includes/clientareafunctions.php");
			$itemCount = 0; 
			$productKeys = array('domains', 'products', 'addons'); 
				foreach ($productKeys as $productKey) 
					{ 
						if (!empty($_SESSION['cart'][$productKey])) 
						{ 
							$itemCount += count($_SESSION['cart'][$productKey]); 
						} 
					}
				if (($itemCount += count($_SESSION['cart'][$productKey])) > 1):
					echo "<a href='https://yoursite.com/checkout/go?language=canada'><li class='header-icon'><div class='header-right-icon-cart'></div></li>";
						echo "<li class='header-right-chat'>{$itemCount} items</li></a>";
						echo "<div class='linha-vertical-header'></div>";
					elseif (($itemCount += count($_SESSION['cart'][$productKey])) == 1):
						echo "<a href='https://yoursite.com/checkout/go?language=canada'><li class='header-icon'><div class='header-right-icon-cart'></div></li>";
						echo "<li class='header-right-chat'>{$itemCount} item</li></a>";
						echo "<div class='linha-vertical-header'></div>";
					endif;
		?>

Share this post


Link to post
Share on other sites
You forgot to add renewal domains

 

Thank you. You were right, I forgot about renewals. I appreciate the code update.

 

Sean

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

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