How do I get product name for whatever product page I am on?

  • 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');
    
     }
        ?>
    

    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.

    it was from the viewcart.phtml file

  • 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);
    

    I've tried this but it does not seem to print anything out.

    @Jake, are you using this on product view page ?

  • 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();
    

    I know i can do it like this but i want to get the product ID automatically

    You can get it by using `getId()` method on product object.

    doesn't work, i tried this getId(); echo $_product->getName(); ?>

    `$product->getId();` will print the product id.

  •   $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

  • 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>
    
  • 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);
    

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM