1

I have a few if conditions in the following PHP.

I can't enable the Javascript section which needs to hide the class when the cart "quantity" is more than the balance.

jimport( 'joomla.user.helper' );
$user = JFactory::getUser();
$groups = $user->get('groups');


if(in_array(15, $groups)) { 
$carts= VirtueMartCart::getCart();

foreach($carts->cartProductsData as $cartItem)
{
 if($cartItem['virtuemart_product_id'] == $product->virtuemart_product_id){

  echo "<br /> ".$cartItem['quantity']."  already added to cart";
}
if($cartItem['quantity'] == $max_balance) <?php { ?>
    <script type="text/javascript">
    $('.addtocart-button').attr('disabled', 'disabled');
    </script>
<?php } ?>
} }

I don't know how to close the first php instance. It refuses.

7
  • 4
    if($cartItem['quantity'] == $max_balance) <?php { ?> You're already inside PHP tags but you've opened them again. You cannot open php tags inside php. Commented Oct 17, 2017 at 8:15
  • And after the last if-statement, you also have <?php } ?>, then some curly braces after (outside of the PHP-block since you just closed it) and then yet another ?>. Commented Oct 17, 2017 at 8:17
  • So I should just remove the second instance and only use the " { ?> " Commented Oct 17, 2017 at 8:20
  • Remove <?php from if($cartItem['quantity'] == $max_balance) <?php { ?> and remove ?> from </script> <?php } ?> Commented Oct 17, 2017 at 8:21
  • You should only close PHP right before <script and open it again right after the js-block. Commented Oct 17, 2017 at 8:23

2 Answers 2

2

You cannot use <?php ... ?> in side <?php ... ?>, this is incorrect syntax.

Use the following:

<?php

jimport('joomla.user.helper');
$user = JFactory::getUser();
$groups = $user->get('groups');


if (in_array(15, $groups)):
    $carts = VirtueMartCart::getCart();

    foreach ($carts->cartProductsData as $cartItem):

        if ($cartItem['virtuemart_product_id'] == $product->virtuemart_product_id)
        {

            echo "<br /> " . $cartItem['quantity'] . "  already added to cart";
        }
        if ($cartItem['quantity'] == $max_balance):
            ?>
            <script type="text/javascript">
                $('.addtocart-button').attr('disabled', 'disabled');
            </script>
            <?php
        endif;
    endforeach;
endif;

This uses Alternative Syntax which is a preferred method when nesting PHP code in HTML blocks.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot mega. I realised my syntax was wrong. Thanks for the advice
@MailBlade you are welcome, if my solution was helpful please be sure to accept it.
2

You have a bit of a mess inside your php code.

I find using the if () : endif; notation is more readable when dealing with php+html

    <?php
    jimport( 'joomla.user.helper' );
    $user = JFactory::getUser();
    $groups = $user->get('groups');
    ?>


    <?php if(in_array(15, $groups)): ?>

        <?php foreach(VirtueMartCart::getCart()->cartProductsData as $cartItem): ?>

            <?php if($cartItem['virtuemart_product_id'] == $product->virtuemart_product_id): ?>
                <br /><?php echo $cartItem['quantity']; ?> already added to cart";
            <?php endif; ?>


            <?php if($cartItem['quantity'] == $max_balance): ?>
                <script type="text/javascript">
                    $('.addtocart-button').attr('disabled', 'disabled');
                </script>
            <?php endif; ?>
        <?php endforeach; ?>

    <?php endif; ?>

2 Comments

Thanks a lot for the suggestions. Will be testing it out.
@UnamataSanatarai I think your foreach loop is closing at location wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.