Displaying Products in Magento: Showing Price and Add to Cart Button
As an experienced developer at Laravel Company, I often encounter clients who need help displaying products from specific categories in their Magento stores. One common scenario is wanting to show the product price alongside the “Add to Cart” button within a custom block.
Understanding the Current Code
In the provided code snippet, you’re already on the right track by using the Magento getProductCollection() method to fetch all products from the specified category. You’ve also successfully retrieved and displayed the product title and image using the getName() and helper('catalog/image')->init($_product, 'small_image') methods respectively.
The issue lies in retrieving the price and correctly handling the “Add to Cart” functionality for each product.
Retrieving Product Price
To display the product price, you can use Magento’s built-in price helper. Replace this line:
<?php echo $this->getPriceHtml($_item, true) ?>
With the following code:
<?php echo $this->getPriceHtml($_product, false) ?>
Here’s what we’ve changed:
- Replaced
$_itemwith$_productto ensure it references the current product being looped through. - Changed
truetofalse, which means the price will be displayed without any tax calculation.
Improving Add to Cart Functionality
The “Add to Cart” button in your code is a static HTML element. To make it functional, you should replace it with Magento’s built-in add-to-cart phtml file. Update this section:
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart"
onclick="setLocation('<?php echo $this->getAddToCartUrl($_item) ?>')" >
<span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>
With the following code:
<?php if ($_product->isSaleable()): ?>
<div class="actions">
<?php if (!$_product->canConfigure()): ?>
<button type="button" title="<?= $block->escapeHtml(__('Add to Cart')) ?>" class="action primary tocart"
id="product-addtocart-button<?= /* @noEscape */ $_item->getId() ?>" onclick="setLocation('<?= /* @noEscape */
$this->helper('checkout/cart')->getAddUrl($_item) ?>')">
<?= /* @noEscape */ $block->escapeHtml(__('Add to Cart')) ?>
</button>
<?php endif; ?>
</div>
<?php endif; ?>
In this updated code:
- We use the
isSaleable()method to ensure only saleable products are displayed. - The “Add to Cart” button is wrapped in a
divwith a class ofactions. - The
onclickattribute now calls the correct URL for adding the product to the cart using Magento’s built-in helper.
Conclusion
By incorporating these modifications, you should now be able to display the product price and a fully functional “Add to Cart” button within your custom block. This approach ensures you’re leveraging Magento’s core functionality while maintaining a clean and performant codebase.
Remember, at Laravel Company, we specialize in providing comprehensive solutions for all your e-commerce development needs, from custom Magento themes to complex extension development. Don’t hesitate to reach out if you have any further questions or require additional assistance with your project.
Happy coding!