Category Archives: Magento 2

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

How To Add Or Create Magento 2 Virtual Product Programmatically

How can I add or create a virtual product for my Magento 2 store programmatically

You need to use Magento\Catalog\Api\Data\ProductInterfaceFactory and \Magento\Catalog\Api\ProductRepositoryInterface  to create a virtual product programmatically.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productFactory = $objectManager->get('Magento\Catalog\Api\Data\ProductInterfaceFactory');

$productRepository = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');

$_product = $productFactory->create();

$_product->setSku('product');

$_product->setName('Product');

$_product->setDescription('Product Description');

$_product->setShortDescription('Product Short Description');

$_product->setWebsiteIds([1]);

$_product->setCategoryIds(["1","2"]);

$_product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL);

$_product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);

$_product->setPrice(array(1));

$_product->setAttributeSetId(4);

$product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

$product->setUrlKey("test");


$_product->setStockData(array(
        'use_config_manage_stock' => 0, //'Use config settings' checkbox
        'manage_stock' => 1, //manage stock
        'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
        'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
        'is_in_stock' => 1, //Stock Availability
        'qty' => 100 //qty
        )
    );

$product = $productRepository->save($product);

$imagePath = '/product/product.jpg'; 

$product->addImageToMediaGallery($imagePath, ['image', 'small_image', 'thumbnail'], false, false);

$product->save();