Magento – PHP else: statements in addtocart.phtml – Adding conditions to the Add To Cart button on product page

addtocartmagento-1.9phtmltheme

My store has the options for user to Add To Cart, Call To Order, Visit Our Store.

I've modified the category page (list.phtml) to show "View Product" instead of Add to Cart.

On the product page, now, I need the options that my store will offer users, as stated in the first sentence. I've created attributes for these products with yes/no options when they make products. Attrib names are 'call_to_order' and 'visit_our_store' . What I'd like to do is on the addtocart.phtml add code to look if this product has one of those attribs as yes and then show the appropriate button, replacing the normal 'Add To Cart' with the appropriate text , and preferably no action to be taken when they click the button. I'd still like it to have the same styling as the 'Add to Cart' button.

I assume this can be done using get attrib and else php, my php is very rusty and I'm not sure where to put it/how to write it.

Current addtocart.phtml:

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>
<?php if($_product->isSaleable()): ?>
<div class="add-to-cart">
    <?php if(!$_product->isGrouped()): ?>
        <div class="qty-block">
            <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 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; ?>

If there are other changes that would need to be made, please advise. I haven't found a solution to this on the web. Even some of the extensions that I think might work would involve custom coding as well imho. This seems like a pretty straight forward solution to my problem.

Thank you!

Best Answer

Assumptions:

  1. The customer will be redirected to http://www.example.com/call-to-order or http://www.example.com/visit-our-store up on clicking on the corresponding buttons.

  2. The priority of displaying the button will be Visit Our Store > Call To Order > Add To Cart

Your code should be

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>
<?php if($_product->isSaleable()): ?>
    <div class="add-to-cart">
        <?php if(!$_product->isGrouped()): ?>
            <div class="qty-block">
                <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 endif; ?>
        <?php if ($_product->getData('visit_our_store')): ?>
            <?php $buttonTitle = $this->__('Visit Our Store'); ?>
            <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="window.location('/visit-our-store/')"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php elseif ($_product->getData('call_to_order')): ?>
            <?php $buttonTitle = $this->__('Call To Order'); ?>
            <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="window.location('/call-to-order/')"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php else: ?>
            <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php endif; ?>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
<?php endif; ?>
Related Topic