criat Posted November 12, 2013 Share Posted November 12, 2013 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? 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted November 12, 2013 Share Posted November 12, 2013 (edited) 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 November 12, 2013 by Kian 0 Quote Link to comment Share on other sites More sharing options...
criat Posted November 12, 2013 Author Share Posted November 12, 2013 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? 0 Quote Link to comment Share on other sites More sharing options...
SeanP Posted November 12, 2013 Share Posted November 12, 2013 You have to look at the $_SESSION['cart'] array. 0 Quote Link to comment Share on other sites More sharing options...
criat Posted November 12, 2013 Author Share Posted November 12, 2013 (edited) 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 November 12, 2013 by criat 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted November 12, 2013 Share Posted November 12, 2013 I appreciate you're trying to code this yourself, but if you need a quick solution there is a similar free addon available. http://www.alfnyhost.com/project/whmcs-show-cart-items-and-pricing.html 0 Quote Link to comment Share on other sites More sharing options...
criat Posted November 12, 2013 Author Share Posted November 12, 2013 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 0 Quote Link to comment Share on other sites More sharing options...
criat Posted November 12, 2013 Author Share Posted November 12, 2013 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."; 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted November 12, 2013 Share Posted November 12, 2013 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} 0 Quote Link to comment Share on other sites More sharing options...
criat Posted November 13, 2013 Author Share Posted November 13, 2013 (edited) 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: Paste the code above where you want it to show Add the other products in the array, where it says 'domains, 'other', 'stuff' Edit all the "echo" to match your site design Replace "if ($language == us)" with "if ($language == English)" You are good to go! Edited November 13, 2013 by criat 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted November 13, 2013 Share Posted November 13, 2013 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. 0 Quote Link to comment Share on other sites More sharing options...
SeanP Posted November 13, 2013 Share Posted November 13, 2013 (edited) 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 November 13, 2013 by SeanP 0 Quote Link to comment Share on other sites More sharing options...
criat Posted November 15, 2013 Author Share Posted November 15, 2013 (edited) 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 November 15, 2013 by criat 0 Quote Link to comment Share on other sites More sharing options...
druptech Posted November 20, 2013 Share Posted November 20, 2013 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} 1 Quote Link to comment Share on other sites More sharing options...
criat Posted November 21, 2013 Author Share Posted November 21, 2013 (edited) 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 November 21, 2013 by criat 0 Quote Link to comment Share on other sites More sharing options...
criat Posted November 21, 2013 Author Share Posted November 21, 2013 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; ?> 0 Quote Link to comment Share on other sites More sharing options...
SeanP Posted November 28, 2013 Share Posted November 28, 2013 You forgot to add renewal domains Thank you. You were right, I forgot about renewals. I appreciate the code update. Sean 0 Quote Link to comment Share on other sites More sharing options...
Manchester Web Hosting Posted October 12, 2020 Share Posted October 12, 2020 appreciate this is a very very old pots but had this working on a pre V8 install and now testing on V8 doesnt work :( hunting the forums hasnt turned up anything else. Is there a way to show amount of products in cart in the header as a badge for V8? 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted October 13, 2020 Share Posted October 13, 2020 21 hours ago, Manchester Web Hosting said: Is there a way to show amount of products in cart in the header as a badge for V8? if you're doing this in the template, then getting the count using.... {$numitemsincart} should give you a count of products/domains (and renewals I think) - addons no longer seem to be included in the count, but i'm sure they used to be... I think the session array structure is slightly different in recent versions, so I don't know if that's an intentional change, or a bug.. note that I think this would only work if you were in the cart pages - outside of there, the variable won't exist.... though you could write a hook that queries the cart session and returned a count. you can access the Smarty session array using... {$smarty.session.cart.products|count} and it should give you a count of the *products* (not domains/renewals etc - you can get them in a similar way, but i'm assuming you mean products and not just a cart count). if it's a count of domain & products you want, then... {$smarty.session.cart.products|count + $smarty.session.cart.domains|count} the point to remember about v8 is that access to the session array in Smarty is now prevented, unless you specifically enable it in your configuration file. Smarty Security Policy also, there is a data feed that returns the cart count, though you'd probably have to duplicate and adjust it's output for your needs. 0 Quote Link to comment Share on other sites More sharing options...
Manchester Web Hosting Posted October 13, 2020 Share Posted October 13, 2020 Thanks @brian! 6 minutes ago, brian! said: if you're doing this in the template, then getting the count using.... Yup, thats how I would like to do it. the: 6 minutes ago, brian! said: {$numitemsincart} works only thing with that is that its included in a badge. SO if there isnt any items in the cart then it would be just a small blob of colour which looks bad. How could that be wrapped around into a badge. Her is what the above would get included in: <span class="badge">{$numitemsincart}</span> not sure if there is an IF statment that can be used? 9 minutes ago, brian! said: should give you a count of products/domains (and renewals I think) - addons no longer seem to be included in the count, but i'm sure they used to be... Yup it does indeed. It used to give the addons too but thats not working anyomre 😞 No biggie I can live with that. 10 minutes ago, brian! said: note that I think this would only work if you were in the cart pages - outside of there, the variable won't exist... Shame thats kind of a deal breaker. Already in the cart so i would assume it defeats the purpose as you can already (kind of) see whats in the cart... 11 minutes ago, brian! said: though you could write a hook that queries the cart session and returned a count. Not a hooks guy (as you probalry already know with the kind help given in the past). Any chance you could help in that area? 12 minutes ago, brian! said: you can access the Smarty session array using... Is that what the hook would use? Again as with enabling php output via admin settings another one which we dont want to do is the smarty tag for the reasons you have stated: 13 minutes ago, brian! said: the point to remember about v8 is that access to the session array in Smarty is now prevented, unless you specifically enable it in your configuration file. 13 minutes ago, brian! said: also, there is a data feed that returns the cart count, though you'd probably have to duplicate and adjust it's output for your needs. Ah, didnt realise that, that option seems a bit overkill thoigh doesnt it? compare to the simple: {$numitemsincart} (potentially wrapped around the span div) OR even the hook option... 0 Quote Link to comment Share on other sites More sharing options...
Manchester Web Hosting Posted October 13, 2020 Share Posted October 13, 2020 So this works: {if $numitemsincart}<span class="badge">{$numitemsincart}</span>{/if} {if $numitemsincart}<span class="badge">{$numitemsincart}</span>{/if} wrapped around the div you need and only displays IF something is in cart so no blob of random colur in the page header 🙂 Now to find a way to get it working on non cart pages.... 0 Quote Link to comment Share on other sites More sharing options...
Manchester Web Hosting Posted October 13, 2020 Share Posted October 13, 2020 3 minutes ago, Manchester Web Hosting said: Now to find a way to get it working on non cart pages.... Ah, actually it doesnt work on the new category/product selection url either, even though thats a cart page? 😒 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted October 14, 2020 Share Posted October 14, 2020 16 hours ago, Manchester Web Hosting said: works only thing with that is that its included in a badge. SO if there isnt any items in the cart then it would be just a small blob of colour which looks bad. How could that be wrapped around into a badge. Her is what the above would get included in: <span class="badge">{$numitemsincart}</span> not sure if there is an IF statment that can be used? it can - I had faith that you'd jump to that conclusion. 🙂 16 hours ago, Manchester Web Hosting said: Yup it does indeed. It used to give the addons too but thats not working anyomre 😞 No biggie I can live with that. it annoys me - it means the code used in the data feed, numitemsincart, cartitemcount etc now doesn't give an accurate value... now I think I tested this in v7.10 yesterday and it was the same, so it's not a v8 issue per se... it's a structure change to the session array by the looks of it and other calculated values that are based on the old structure haven't been updated by WHMCS. it's fixable, but I like to think that we shouldn't really be required to fix WHMCS functionality ourselves... it was humorous back in 2013, but the novelty is wearing a bit thin now seven years later. 16 hours ago, Manchester Web Hosting said: Shame thats kind of a deal breaker. Already in the cart so i would assume it defeats the purpose as you can already (kind of) see whats in the cart... I thought it might be - though checking other pages, there is another variable you can use on *all* pages... {if $cartitemcount}<span class="badge">{$cartitemcount}</span>{/if} 17 hours ago, Manchester Web Hosting said: Not a hooks guy (as you probably already know with the kind help given in the past). Any chance you could help in that area? the issue with addons not being counted still exists, but if you're not bothered about that, you shouldn't need a hook... the pain is, I could fix it and then suddenly there's a v8.0.3 released that fixes it too... and yet if we do nothing ourselves, it might be there for years... this is how WHMCS works everyone! i'm tempted to think upgrades should be included in the count, but let's not make works for ourselves. 17 hours ago, Manchester Web Hosting said: Is that what the hook would use? yes - the cart is a session.... and it would be a ClientAreaPage hook so that it worked on all pages... actually, you could use another hook that would add the badge to the page without editing the template - but again, let's not make work if it's unnecessary. 🙂 17 hours ago, Manchester Web Hosting said: Again as with enabling php output via admin settings another one which we don't want to do is the smarty tag for the reasons you have stated: personally, I would see using {php} tags and smarty as two separate things - I wouldn't use {php} tags in a template, but it wouldn't bother me accessing the session via Smarty if I had to or was quicker... I wouldn't necessarily see it as dangerous in v8, but safe in v7 - it's just a tightened (for whatever reason) security policy that makes it a slightly more involved process when doing it in v8. obviously, you could write a hook to access the session array and return whatever values are needed. 19 hours ago, Manchester Web Hosting said: Ah, didnt realise that, that option seems a bit overkill thoigh doesnt it? compare to the simple: {$numitemsincart} (potentially wrapped around the span div) OR even the hook option... yes - but the advantage would be that it's off-the-shelf and ready to go - the disadvantage would be that i've spotted multiple bugs with these data feeds now - I daresay because data feeds have been treated like an abandoned child locked in the attic that never sees the light of day... sadly neglected. 0 Quote Link to comment Share on other sites More sharing options...
Manchester Web Hosting Posted October 14, 2020 Share Posted October 14, 2020 3 hours ago, brian! said: it annoys me - it means the code used in the data feed, numitemsincart, cartitemcount etc now doesn't give an accurate value... now I think I tested this in v7.10 yesterday and it was the same, so it's not a v8 issue per se... Yup agree. annoying is putting it mildly tho... the dat feed needs to be scrapped it just doesnt work OR is way too buggy. has a mind of its own! 3 hours ago, brian! said: it's fixable, but I like to think that we shouldn't really be required to fix WHMCS functionality ourselves... it was humorous back in 2013, but the novelty is wearing a bit thin now seven years later. exactly. shouldnt be left to the community to fix core functions. I dont think it was funny back then and certinly dont think it is now. Pretty shameful TBH. 3 hours ago, brian! said: I thought it might be - though checking other pages, there is another variable you can use on *all* pages... Yes it would be... BUT 3 hours ago, brian! said: {if $cartitemcount}<span class="badge">{$cartitemcount}</span>{/if} You did it again. Nice an simple 🙂 that works a treat for what we need. Getting to use the simplest of functions etc seems the bets way to get updates done without bashing your head with a baseball bat wondering whats going wrong. Enough stress in the world as it is. 3 hours ago, brian! said: yes - but the advantage would be that it's off-the-shelf and ready to go - the disadvantage would be that i've spotted multiple bugs with these data feeds now - I daresay because data feeds have been treated like an abandoned child locked in the attic that never sees the light of day... sadly neglected. again coulnt agree with you more. They had potential and neglecting them well... begs the question what was the point in the first place? Sadly as its commentated time and time again the whmcs team just seem to roll out things as they please regardless if it actually has any benefit and neglect things that should be fixed OR add in what would be useful... No hope now that cpanel own them ;( Anyways, much gracias for your help. Stella support (ehm ehm) as usual 🙂 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted October 15, 2020 Share Posted October 15, 2020 22 hours ago, Manchester Web Hosting said: Yup agree. annoying is putting it mildly tho... the dat feed needs to be scrapped it just doesnt work OR is way too buggy. has a mind of its own! the data feeds have a role, e.g if you needed to output certain details on a non-WHMCS page - the problem is that they haven't been kept updated or relevant - I don't think WHMCS themselves have written any new data feeds as long as i've been using this award winning software. 22 hours ago, Manchester Web Hosting said: exactly. shouldn't be left to the community to fix core functions. I don't think it was funny back then and certainly don't think it is now. Pretty shameful TBH. I meant funny in the sense of when I got my first license back in 2013 (what a mistake that was!), the company seemed a little amateurish on so many levels, but I assumed it was just a blip and over time things would improve... 7 years later, it's still the same old nonsense occurring... poor internal testing, major releases launched before they're ready, poor communication, poor documentation, the laughable feature request system.. etc etc every long standing user knows all about it... there's more too going on that I wouldn't be allowed to mention here. one day they'll realise that it's sunny outside and they should have been mending the roof whilst they had the chance - because one day alternative realistic options to WHMCS might come along and they will be dead in the water.... I will not shed any tears when that happens. 22 hours ago, Manchester Web Hosting said: You did it again. Nice an simple 🙂 that works a treat for what we need. annoying that I missed it in the first place though! 🙄 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.