1

I have a custom loop for my website and I need to add addtowishlist and addtocart buttons in my foreach. How to add this buttons in Magento? I marked in the end of code where I need to insert the buttons.

$_productCollection = Mage::getModel('catalog/product')
                        ->getCollection()
                        // add sorting to collection optional
                        ->addAttributeToFilter('a_pecas_assinadas', array('eq' => 1))
                        ->addAttributeToSort('created_at', 'DESC')
                        ->addAttributeToSelect('*')
                        ->load();

foreach ($_productCollection as $_product){
    $t++;
    if($t==1){$variavel='first';}
    elseif($t==5){$variavel='last';}
    else{$variavel='';}

    if($t <= 5){
        echo '<div class="col-md-15 txtleft">';
            echo '<div class="'.$variavel.' produtos">';
                echo '<a class="ocinza upper" href="'.$_product->getProductUrl().'">';
                    echo '<img src="'.$url.$_product->getImage().'">';
                echo '</a>';
                echo '<span class="hidden">'.$_product->getId().'</span>';
                echo '<p class="fs14 fw700" style="margin:10px auto 5px">';
                    echo '<a class="ocinza upper" href="'.$_product->getProductUrl().'">';
                        echo $_product->getName();
                    echo '</a>';
                echo '</p>';
                echo '<div class="row opensans fs16">';
                    echo '<div class="col-md-12"><span class="lnt">'.money_format('%n', $_product->getPrice()).'</span>';
                    echo '&nbsp;&nbsp;&nbsp;<span class="verde">'.money_format('%n', $_product->getSpecialPrice()).'</span></div>';
                echo '</div>';
            echo '</div>';
        echo '</div>';

        -------> echo '<a>addtowishlist</a>';
        -------> echo '<a>addtocart</a>';
2

These codes should give you add to cart and wishlist buttons.

For add to cart:

<a href="<?php echo Mage::helper('checkout/cart')->getAddUrl($_product) ?>" class="button btn btn-cart">Add to Cart</a>

For wishlist:

<a href="<?php echo $this->helper('wishlist')->getAddUrl($_product); ?>" class="button btn btn-wishlist">Add to Wishlist</a>

Hope this helps.

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.