Jump to content

Limit specific group quantity at checkout


robetus

Recommended Posts

In the viewcart.tpl I have:

                                {if $product.productinfo.gid eq "2" && $cartitems > 1}
                                <div class="text-right">
                                    <div class="support-box red-support-box">
                                        One item per order
                                    </div>
                                    <br />
                                    <a href="cart.php" class="btn btn-link btn-continue-shopping" id="continueShopping">
                                        {$LANG.orderForm.continueShopping}
                                    </a>
                                </div>
                                {else}
                                <div class="text-right">
                                    <a href="cart.php?a=checkout" class="btn btn-success btn-lg btn-checkout{if $cartitems == 0} disabled{/if}" id="checkout">
                                        {$LANG.orderForm.checkout}
                                        <i class="fa fa-arrow-right"></i>
                                    </a><br />
                                    <a href="cart.php" class="btn btn-link btn-continue-shopping" id="continueShopping">
                                        {$LANG.orderForm.continueShopping}
                                    </a>
                                </div>
                                {/if}

It works but as soon as a different product is added from a different product group it does not work. Is there any way with smarty that I can limit a product group to only have one product from that group in the cart at a time? Maybe I need a hook, but I wanted to ask to see if anyone knew how to do with smarty syntax.

Link to comment
Share on other sites

15 hours ago, robetus said:

It works but as soon as a different product is added from a different product group it does not work. Is there any way with smarty that I can limit a product group to only have one product from that group in the cart at a time?

one way to do it would be to split this into two parts - first in the existing $products foreach loop (~ line 90), you create a Smarty counter variable that increases each time it finds a product from GID X in the cart...

{foreach $products as $num => $product}
{if $product.productinfo.gid eq "2"}{assign var=productcounter value=$productcounter+1}{/if}

that gives you a $productcounter variable that will tell you how many products from GID=2 are in the cart...

and then where you previously had...

{if $product.productinfo.gid eq "2" && $cartitems > 1}

you can change that to...

{if $productcounter gt 1}

which should mean that if there is more than 1 product from group #2 in the cart, then it will throw your "1 Item" error message and disable the checkout button... also, if $productcounter is more than 1, then you also know that there is more than one product in the cart, so $cartitems would always be more than 1 too.

you could trim that entire IF statement down in size if you had to...

<div class="text-right">
	{if $productcounter gt 1}
		<div class="support-box red-support-box">One item per order</div>
	{/if}
	<a href="cart.php?a=checkout&e=false" class="btn btn-success btn-lg btn-checkout{if $cartitems == 0 OR $productcounter gt 1} disabled{/if}" id="checkout">
	{$LANG.orderForm.checkout}
	<i class="fa fa-arrow-right"></i>
	</a><br />
	<a href="cart.php" class="btn btn-link btn-continue-shopping" id="continueShopping">
	{$LANG.orderForm.continueShopping}
	</a>
<div>

ZDoDkHl.png

15 hours ago, robetus said:

Maybe I need a hook, but I wanted to ask to see if anyone knew how to do with smarty syntax.

you might ultimately need a hook as the one obvious flaw in your Smarty method is that the user can change the url in the browser from cart.php?a=view to cart.php?a=checkout and thus totally bypass your condition checks (even if the checkout button is disabled or hidden)! 🙄

will most WHMCS users know that they can do that in the browser ? probably not.

you could duplicate the foreach $products loop in the checkout.tpl template - not all of it, just the bit that makes the counter...

{foreach $products as $num => $product}
	{if $product.productinfo.gid eq "2"}{assign var=productcounter value=$productcounter+1}{/if}
{/foreach}

and then disable the checkout button if there is more than one product from your specified product group in the cart...

<button type="submit" id="btnCompleteOrder" class="btn btn-primary btn-lg disable-on-click spinner-on-click{if $captcha}{$captcha->getButtonClass($captchaForm)}{/if}"{if $cartitems==0 or $productcounter gt 1}disabled="disabled"{/if}>
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