0

I tried the below solution found over google but it doesn't gave me the required output. I have edited the file as follows, app/design/frontend/[template]/[package]/template/catalog/product/view/addtocart.phtml

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

if(Mage::helper('customer')->isLoggedIn() ): 
    if($_product->isSaleable()): 
?>
    <div class="add-to-cart">
        <?php if(!$_product->isGrouped()): ?>
        <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" />
        <?php endif; ?>
        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
<?php 
    endif;
else:
?>
    <p><?php echo $this->__('Please <a href="%s">login</a> to buy this product', Mage::getUrl('customer/account'));?></p>
<?php
endif; 
?>

Let me know, what I am missing here.

  • 1
    try Mage::getSingleton('customer/session')->isLoggedIn() instead. – Ricky Odin Matthews Oct 11 '17 at 15:55
0

=> Try to use this code. It may be helpful to you

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

    if(Mage::getSingleton('customer/session')->isLoggedIn()){
        //logged in
if($_product->isSaleable()): 
?>
    <div class="add-to-cart">
        <?php if(!$_product->isGrouped()): ?>
        <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" />
        <?php endif; ?>
        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
<?php 
    endif;
    }else{?>php
        //not logged in
<p><?php echo $this->__('Please <a href="%s">login</a> to buy this product', Mage::getUrl('customer/account'));?></p>
    <?php}
?>
0

You will be required to redirect your user to previous page after login. Hence write code for redirect also.

<?php
if(!Mage::helper('customer')->isLoggedIn()) {
    Mage::getSingleton('customer/session')
            ->setBeforeAuthUrl(Mage::helper('core/url')->getCurrentUrl()); 
    echo '<p>Please <a href="'.Mage::getUrl('customer/account/login').'">Login</a> to buy this product.</p>';
}

else
{
 put here your desired add to cart code.
}
?>

I hope this code will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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