Category Archives: PHP

How To Get Magento 2 Product’s Status

How can I get Magento 2 product’s status. e.g I would like to check if the Magento 2 product is disabled or not

Need to load Magento 2 product first and then call getStatus method to retrieve the status info of the Magento 2 product

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productRepositoryInterface = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');
$product = $productRepositoryInterface->getById($productId);
$status = $product->getStatus();

How To Generate PDF programmatically in Magento 2

I would like to create a PDF file, how can I generate PDF programmatically in Magento 2

To create a PDF file in Magento 2

Step 1Define Zend_Pdf object and set page size
Step 2Set style and font for new Magento 2 PDF file
Step 3Draw content by called drawText, drawRectangle or other methods
Step 4Call class FileFactory and saving the content as Magento PDF file

Here is the example to creating a PDF file in Magento 2

$pdf = new \Zend_Pdf();
$pdf->pages[] = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);
$page = $pdf->pages[0]; // this will get reference to the first page.
$style = new \Zend_Pdf_Style();
$style->setLineColor(new \Zend_Pdf_Color_Rgb(0,0,0));
$font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_TIMES);
$style->setFont($font,15);
$page->setStyle($style);
$width = $page->getWidth();
$hight = $page->getHeight();
$x = 40;
$pageTopalign = 850; //default PDF page height
$y = $pageTopalign - 50;
$style->setFont($font,16);
$page->setStyle($style);
$page->drawText(__("Text Text"), $x + 5, $y+50, 'UTF-8');
$fileName = 'test.pdf';

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$fileFactory = $objectManager->get('Magento\Framework\App\Response\Http\FileFactory');

$fileFactory->create($fileName,$pdf->render(),\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR,'application/pdf');

How To Get Current Magento 2 Version

I would like to know current version of Magento 2, which function can I call to get current Magento 2 version

For Magento 2.0.x

Can call AppInterface::VERSION to get current Magento 2.0.x version

echo \Magento\Framework\AppInterface::VERSION;

For Magento 2.1.x

Can use “ProductMetadataInterface” module to retrieve the current Magento 2.1 version

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
$version = $productMetadata->getVersion();

How To Retrieve Saved Magento Session Value

How can I retrieve the Magento session value which I have saved in the past

You need to load Magento core/session object and call get method (based on what you have set when you are saving the Magento Session) to retrieve the Magento Session value

e.g
Saving the session value

Mage::getSingleton(“core/session”)->set[YOUR SESSION NAME]();

Retrieving the session value

Mage::getSingleton(“core/session”)->get[YOUR SESSION NAME]();

Here is the example

Mage::getSingleton("core/session")->setYourValue("test");
$youvalue = Mage::getSingleton("core/session")->getYourValue();

How To Get Current Magento Category

How can I get current Magento category and retrieve the category ID

You can use method “getCurrentCategory” to retrieve the current Magento category and use getId to find out the current Magento category ID

$category = Mage::getModel('catalog/layer')->getCurrentCategory();
$id = $category->getId();