0

A while ago, I asked this question and the code is working fine. I can change the "add to cart" button depending on the value of a yes/no attribute.

What I'm trying to achieve now is to create an if/elseif/elseif statement, in which the if condition is the yes/no attribute, the first elseif condition is a joined statement of the yes/no and customer group, and the last elseif condition is the default isSaleable() condition.

The code that I have is as follows but it throws the error that the variable $_myGroupId isn't defined:

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>

<!--getting the customer group like this works.-->
<?php $_isLoggedIn = $this->helper('customer')->isLoggedIn();
if($_isLoggedIn === true){
    $_myGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
} ?>

<!--changing the content of the button according to the yes/no attribute also works without issues.-->
<?php if ($_product->getFromRetailer()) {?>
    <div class="add-to-cart v-centered-content">
        <button type="button" title="<?php echo $this->__('Find your nearest retailer') ?>" id="product-addtocart-button" class="button btn-cart v-center" onclick="setLocation('<?php echo $this->getUrl('distributors') ?>')"><span><span><?php echo $this->__('Find your nearest retailer') ?></span></span></button>
    </div>
<?php }

<!--this combined elseif statement with product attribute and customer group doesn't work.-->
elseif ($_product->getFromRetailer() && $_myGroupId == 3) { ?>
    <div class="add-to-cart v-centered-content">
        <button type="button" title="<?php echo $this->__('Look at me') ?>" id="product-addtocart-button" class="button btn-cart v-center" onclick="setLocation('<?php echo $this->getUrl('distributors') ?>')"><span><span><?php echo $this->__('Look at me') ?></span></span></button>
    </div>
<?php }

<!--this default elseif statement works without issues.-->
elseif ($_product->isSaleable()) { ?>
    <div class="add-to-cart v-centered-content">
        <button type="button" title="<?php echo $buttonTitle ?>" id="product-addtocart-button" class="button btn-cart v-center" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php if(!$_product->isGrouped()){ ?>
            <div class="qty-wrapper v-center">
                <label for="qty"><?php echo $this->__('Qty:') ?></label>
                <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
            </div>
        <?php } ?>
    </div>
    <div class="paypal-wrapper"><?php echo $this->getChildHtml('', true, true) ?></div>
<?php } ?>

EDIT Just to make it clearer, I can get the customer group successfully and the problem that I have with the code is the statement elseif ($_product->getFromRetailer() && $_myGroupId == 3). It throws the error that $_myGroupId isn't defined.

What am I doing wrong?

closed as unclear what you're asking by David Manners, Rajeev K Tomy, Marius Apr 20 '15 at 15:55

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I think you just need to define a default value $_myGroupId above the if($_isLoggedIn) line – Robin Mar 31 '15 at 13:28
  • but what if in the future I want to make other elseif statements in this code for different customer groups? – VDD Mar 31 '15 at 13:31
  • then you just add them? the problem is that you're trying to access a variable that hasn't been declared yet (because apparently isLoggedIn is false). if you add a default value for myGroupId, for example 0, then you won't have an undefined variable – Robin Mar 31 '15 at 13:40
  • I've tried your suggestion, but unfortunately it doesn't work. It goes to the first if ($_product->getFromRetailer()) statement. – VDD Mar 31 '15 at 13:53
  • that means that getFromRetailer returns true: then it executes that piece of code.. but your problem with the undefined variable is solved – Robin Mar 31 '15 at 13:58
1

Thanks to @Anja for giving me pointers that have allowed me to solve my issue.

In the end, it was a matter of the order of the if/elseif/elseif statements.

The code that works for me is as follows:

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>

<?php $_isLoggedIn = $this->helper('customer')->isLoggedIn();
if($_isLoggedIn === true){
    $_myGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
} ?>

<?php if ($_myGroupId == 3 && $_product->getFromRetailer()) { ?>
    <div class="add-to-cart v-centered-content">
        <button type="button" title="<?php echo $this->__('Get a quote') ?>" id="product-addtocart-button" class="button btn-cart v-center" onclick="setLocation('<?php echo $this->getUrl('quote') ?>')"><span><span><?php echo $this->__('Get a quote') ?></span></span></button>
    </div>
<?php }

elseif ($_product->getFromRetailer()) {?>
    <div class="add-to-cart v-centered-content">
        <button type="button" title="<?php echo $this->__('Find your nearest retailer') ?>" id="product-addtocart-button" class="button btn-cart v-center" onclick="setLocation('<?php echo $this->getUrl('distributors') ?>')"><span><span><?php echo $this->__('Find your nearest retailer') ?></span></span></button>
    </div>
<?php }

elseif ($_product->isSaleable()) { ?>
    <div class="add-to-cart v-centered-content">
        <button type="button" title="<?php echo $buttonTitle ?>" id="product-addtocart-button" class="button btn-cart v-center" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php if(!$_product->isGrouped()){ ?>
            <div class="qty-wrapper v-center">
                <label for="qty"><?php echo $this->__('Qty:') ?></label>
                <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
            </div>
        <?php } ?>
    </div>
    <div class="paypal-wrapper"><?php echo $this->getChildHtml('', true, true) ?></div>
<?php } ?>
0

Try using

$login = Mage::getSingleton( 'customer/session' )->isLoggedIn(); //Check if User is Logged In
if($login) {
    $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
    //do your thing shorty!
} 
  • Hi, I already have that bit of code and I know that works, what I'm having problems with is the elseif ($_product->getFromRetailer() && $_myGroupId == 3) statement – VDD Mar 31 '15 at 12:57
  • That is not what you stated in your question. Please update it. – mbalparda Mar 31 '15 at 12:58
  • I put it there in the explanation, but I'll rewrite it anyway. – VDD Mar 31 '15 at 12:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.