5

I have looked around and I swear all I can do is just get all product names to display.

For example if I am on product page x I want to show x's product name. So far I have the below code and it displays them all.

    <?php
     $_products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name');

     foreach ($_products as $product){
     $prod_name = $product->getData('name');
     echo $prod_name . '<br>';
     $attribute = $product->getData('attribute');

 }
    ?>
2
  • What page are you calling this code from? Some pages have the product already loaded, and to load it a second (or 20th) time would have serious impact on performance.
    – Phil M
    Commented Nov 23, 2016 at 16:36
  • it was from the viewcart.phtml file
    – Jake
    Commented Nov 24, 2016 at 10:00

5 Answers 5

8

Try this :

$product = Mage::registry('current_product'); 

then

$product->getName();
$product->getId();

You can try this for product :

$id = $this->getRequest()->getParam('id');
$current_product=Mage::getModel('catalog/product')->load($id);
2
  • I've tried this but it does not seem to print anything out.
    – Jake
    Commented Nov 23, 2016 at 11:36
  • @Jake, are you using this on product view page ? Commented Nov 23, 2016 at 11:48
3

You can get product name using product id.

$obj = Mage::getModel('catalog/product');
$_product = $obj->load($product_id); // Enter your Product Id in $product_id
// get Product's name
echo $_product->getName();
4
  • I know i can do it like this but i want to get the product ID automatically
    – Jake
    Commented Nov 23, 2016 at 11:03
  • You can get it by using getId() method on product object. Commented Nov 23, 2016 at 11:07
  • doesn't work, i tried this <?php $obj = Mage::getModel('catalog/product'); $_product = $obj->getId(); echo $_product->getName(); ?>
    – Jake
    Commented Nov 23, 2016 at 11:10
  • $product->getId(); will print the product id. Commented Nov 23, 2016 at 11:12
3
  $currentUrl = Mage::helper('core/url')->getCurrentUrl();        
  $url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
  $path = $url->getPath();                                        
  $oRewrite = Mage::getModel('core/url_rewrite')->setStoreId(Mage::app()->getStore()->getId())
              ->loadByRequestPath(ltrim($path,'/'));              
  $pId = $oRewrite->getProductId();                             

   if($pId > 0)                                                               
    echo Mage::getModel('catalog/product')->load($pId)->getName(); 

tested on magento 1.9.2.3 (was placed in app/design/frontend/rwd/default/template/checkout/cart/minicart.phtml file)

references: https://stackoverflow.com/questions/9254789/magento-load-product-by-url https://stackoverflow.com/questions/25192212/get-current-url-in-magento-and-show-something

2

If you are already on the Product View page, then the Magento system already has loaded the product.

Look at the class: Mage_Catalog_Block_Product_View

Then look at its function:

/**
 * Retrieve current product model
 *
 * @return Mage_Catalog_Model_Product
 */
public function getProduct()
{
    if (!Mage::registry('product') && $this->getProductId()) {
        $product = Mage::getModel('catalog/product')->load($this->getProductId());
        Mage::register('product', $product);
    }
    return Mage::registry('product');
}

From there you are able to print various attributes from the product, such as code from the template file app/design/frontend/base/default/template/catalog/product/view.phtml:

<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
<?php /* ... cut ... */ ?>
<div class="product-view">
    <div class="product-essential">
    <form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
    <div class="no-display">
        <input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
        <input type="hidden" name="related_product" id="related-products-field" value="" />
    </div>

    <div class="product-shop">
        <div class="product-name">
            <h1><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>
        </div>
1

Try this,hope it's help to you.

$product = Mage::registry('current_product'); then
$product->getName();
$product->getId();

You can try this for product :

$id = $this->getRequest()->getParam('id');
$current_product=Mage::getModel('catalog/product')->load($id);

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.