Answers for "magento get current product"

-1

magento 1.9 get all product

//to overwrite limit but you need first to increase your memory limit

 $collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*') // select all attributes
->setPageSize(5000) // limit number of results returned
->setCurPage(1); // set the offset (useful for pagination)

// we iterate through the list of products to get attribute values
foreach ($collection as $product) {
  echo $product->getName(); //get name
  echo (float) $product->getPrice(); //get price as cast to float
  echo $product->getDescription(); //get description
  echo $product->getShortDescription(); //get short description
  echo $product->getTypeId(); //get product type
  echo $product->getStatus(); //get product status

  // getCategoryIds(); returns an array of category IDs associated with the product
  foreach ($product->getCategoryIds() as $category_id) {
      $category = Mage::getModel('catalog/category')->load($category_id);
      echo $category->getName();
      echo $category->getParentCategory()->getName(); // get parent of category
  }
  //gets the image url of the product
  echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).
      'catalog/product'.$product->getImage();
  echo $product->getSpecialPrice();
  echo $product->getProductUrl();  //gets the product url
  echo '<br />';
}
Posted by: Guest on November-10-2020
0

how to know who added product in magento

First of all create a custom attribute called : "updated_by" from magento backend and don't forget to drag that into default attribute set otherwise it will not work!!

Add events.xml file to vendor/module/etc/adminhtml/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="observer_name" instance="vendor\module\Observer\ProductSaveAfter" />
    </event>
</config>
Add ProductSaveAfter.php at vendor/module/Observer/

<?php
namespace vendor\module\Observer;
use Magento\Framework\Event\ObserverInterface;
class ProductSaveAfter implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $instance = \Magento\Framework\App\ObjectManager::getInstance();
        $productId = (int) $observer->getProduct()->getId();
        $user = $instance->get('Magento\Backend\Model\Auth\Session')->getUser()->getUsername();
        $action = $instance->create('Magento\Catalog\Model\Product\Action');
        $action->updateAttributes([$productId], array('updated_by' => $user), 0);
    }
}
Posted by: Guest on March-08-2022

Code answers related to "magento get current product"

Browse Popular Code Answers by Language