1

I have searched in Magento 2 developer documentation and also googled about the new Magento 2 GET URL method but haven't found something clear that explain how to get url path in new platform.

So I open up this post to complete with your help the missing commands and help also other user searching for the same argument.

The Magento 1 Platform

GET PRODUCT URL
<?php echo $this->helper('catalog/product')->getProductUrl($_product); ?>

GET ATTRIBUTE
<?php echo $_product->getData('manufacturer'); ?>
<?php echo $_product->getSku(); ?>

GET PRODUCT NAME
<?php echo $_product->getName(); ?>

GET DESCRIPTION
<?php echo strip_tags($_product->getDescription()); ?>

GET KEYWORDS
<?php echo htmlspecialchars($this->getKeywords()) ?>

GET PRODUCT IMAGE
<?php echo $this->helper('catalog/image')->init($_product, 'image')->resize(300, 300); ?>

The Magento 2 Platform

GET BASE URL
This is the only reference I have found
<?php echo $block->getBaseUrl(); ?>

if I try all other Magento 1 parameters to get the URL/ATTRIBUTE/NAME etc. Magento 2 crash.

1

1 Answer 1

0

Get product url from Helper

In your template, we can call directly (however, should be injected in our Block)

$this->helper->('Magento\Catalog\Helper\Product')->getProductUrl($product);

Get Product attributes in product listing

vendor/magento/module-catalog/view/frontend/templates/product/list.phtml

/**
 * Product list template
 *
 * @var $block \Magento\Catalog\Block\Product\ListProduct
 */

 $_productNameStripped = $block->stripTags($_product->getName(), null, true);

$_helper = $this->helper('Magento\Catalog\Helper\Output');

$_helper->productAttribute($_product, $_product->getManufacturer(), 'manufacturer');

$_helper->productAttribute($_product, $_product->getName(), 'name');

$_helper->productAttribute($_product, $_product->getDescription(), 'description')

$productImage = $block->getImage($_product, $image);
echo $productImage->toHtml();

Product Image helper

Magento 2 - Keep Product Image Aspect Ratio

//$image = 'category_page_grid' or 'category_page_list';
 $_imagehelper = $this->helper('Magento\Catalog\Helper\Image');

 $productImage = $_imagehelper->init($_product, $image)->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(400)->getUrl();

Note: some Helpers help us to get the product url easily, however, for the best practise, they should be injected in the constructor of our Blocks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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