Category Archives: Uncategorized

How Can I Disable All Magento 2 Extensions

I would like to disable all extensions, what is the simplest way for Magento 2

You can use one command line to disabled all Magento 2 extensions or custom modules

php bin/magento module:status | grep -v Magento | grep -v List | grep -v None | grep -v -e ‘^$’| xargs php bin/magento module:disable

Enable all extensions or modules

php bin/magento module:status | grep -v Magento | grep -v List | grep -v None | grep -v -e ‘^$’| xargs php bin/magento module:enable

How to Active Magento 2 Category Programmatically

How can I active a Magento 2 category programmatically based on given category ID

After load Magento 2 category object, you can use setIsActive method to active or inactive the category.

$catid = 1;
$storeid = 1;
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$categoryRepository = $objectManager->get(‘MagentoCatalogModelCategoryRepository’);
$category = $categoryRepository->get($catid , $storeid);
$category->setIsActive(true);
$categoryRepository->save($category);

How To get Current Magento 2 Customer ID

How to get current Magento 2 customer id programmatically after logged in.

You can use “\Magento\Customer\Model\Session ” to retrieve the current customer session and use it to get customer id.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get(‘\Magento\Customer\Model\Session’);
$custom_id =  $customerSession->getCustomer()->getId();

How Can I Know the Current Page is Magento 2 CMS Page

How can I know the current page is CMS page from Magento 2 Block class

You can use “\Magento\Framework\App\Request\Http” to retrieve the current request and use it to find out which module.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $request = $objectManager->get(‘\Magento\Framework\App\Request\Http’); $checkModule = $request->getModuleName();
if($checkModule == ‘cms’){
echo “cms page”;
}else{
echo “not cms page”;
}

How to get Magento 2 Product’s Image URL Programmatically

How can I retrieve Magento 2 product’s image URL from published/cache frontend.

After get Magento 2 product’s image url, you need to use “\Magento\Catalog\Helper\Image” to generate a public image url.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$helperImport = $objectManager->get(‘\Magento\Catalog\Helper\Image’);
$registry = $objectManager->get(‘\Magento\Framework\Registry’);
$product = $registry->registry(‘current_product’);
$imageUrl = $helperImport->init($product, ‘product_page_image_small’) ->setImageFile($product->getSmallImage())->getUrl();

How To Get Current Magento 2 Category Name Programmatically

How to get current Magento 2 category name from Block class

To retrieve current Magento 2 category name from Block class, you need load “Magento\Framework\Registry” object first and then call “registry(‘current_category’)” method to get current category object. Here is the example.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$registry = $objectManager->get(‘\Magento\Framework\Registry’);
$category = $registry->registry(‘current_category’);
$categoryName = $category->getName();

How To Get Current Magento 2 Product Info Programmatically

How to get current Magento 2 product info programmatically from Block class

You can use “Magento\Framework\Registry ” to retrieve current magento 2 product object.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$registry = $objectManager->get(‘\Magento\Framework\Registry’);
$product = $registry->registry(‘current_product’);

Get product’s name

$productName = $product->getName();

Get product’s sku

$productName = $product->getSku();

Get product’s ID

$productName = $product->getId();

How To Get Current Magento 2 Store Info Programmatically

How can I get current Magento 2 store info from Block class programmatically. Like store name, store id, store code, store url, is store active, website id and store name
To get current Magento 2 store’s info programmatically, you need load “Magento\Store\Model\StoreManagerInterface” object first and then call “getStore()” method to retrive current store object. Here is the example.

objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get(‘\Magento\Store\Model\StoreManagerInterface’);

Get current store’s  id

$storeManager->getStoreId();

Get current store’s code

$storeManager->getStoreCode();

Get current website ID

$storeManager->getWebsiteId();

Get current store’s name

$storeManager->getStoreName();

Get current store’s url

$storeManager->getStoreUrl();

Check current store is active or inactive

$storeManager->isStoreActive();

How To Get Current Magento 2 Store Name Programmatically

How to get current Magento 2 store’s name from Block class

To retrieve current Magento 2 store’s name from Block class, you need load “Magento\Store\Model\StoreManagerInterface” object first and then call “getStore()” method to get current store object. Here is the example.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get(‘\Magento\Store\Model\StoreManagerInterface’);
$storename = $storeManager->getStore()->getName();