Ihor Mandziuk
- 2 min read
While looking through various , you can't miss downloadable products. As the name suggests, these are digital products that can be downloaded — books, software, videos, etc. This type of content is becoming more and more popular these days. As a result, you can benefit greatly from having them in your store's catalogue.
via the admin panel may be rather challenging since it requires lots of steps. Fortunately, there is a programmatic way to optimize the process.
The following code will help you to create a downloadable product in Magento 2 programmatically:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get(\Magento\Framework\App\State::class);
$state->setAreaCode('frontend');
try {
$product = $objectManager->create(\Magento\Catalog\Api\Data\ProductInterface::class);
$product
->setSku('sku')ihor
- 2 min read
Unlike the other , virtual products don't have a physical representation. These can be gift cards, services, certificates, warranties, etc.
Using the admin panel to is not the only way. You might as well be looking for some alternatives. In this case, you can try to create a virtual product programmatically and in this article, you'll learn how to do that.To create a virtual product in Magento 2 programmatically:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get(\Magento\Framework\App\State::class);
$state->setAreaCode('frontend');
try {
$product = $objectManager->create(\Magento\Catalog\Api\Data\ProductInterface::class);
$product
->setSku('Main Virtual') // Set your sku here
->setName('Virtual Color Product') // Name of Product
->setAttributeSetId(4) // Attribute set id
->setStatus(1)ihor
- 3 min read
Having multiple listed in your store catalog helps to make the customer experience in your store more diverse. One of the many options you can provide is a . Basically, it consists of simple and virtual products that can be customized according to the client's preferences. Customers can choose the size, weight, color, etc. to get the very product they like.
Although, you can create bundle products from the admin panel creating bundle products programmatically is much faster. So in this article, you'll learn how to do that.
Use the below method to create a bundle product in Magento 2 programmatically.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get(\Magento\Framework\App\State::class);
$state->setAreaCode('frontend');
try {
$bundleProduct = $objectManager->create(\Magento\Catalog\Api\Data\ProductInterface::class);ihor
- 1 min read
Grouped products are only one of many . What is special about them is that they consist of simple products that can be purchased separately or as a group. Moreover, grouped products don't have a price, since it's defined by its component products.
Of course, you can using the admin panel. However, you might also be looking for a programmatic way to perform this task.
If you want to know how to create grouped products programmatically, you've landed on the right page.
To create a grouped product in Magento programmatically use the following code:
<?php error_reporting(E_ALL);ini_set('display_errors', 1); use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get(\Magento\Framework\App\State::class);$state->setAreaCode('frontend'); try { $product = $objectManager->create(\Magento\Catalog\Api\Data\ProductInterface::class); $product->setTypeId(\Magento\GroupedProduct\Model\Product\Type\Grouped::TYPE_CODE)ihor
- 3 min read
While working with Magento 2 you perform a wide variety of tasks from to configuring indexers and running . Magento CLI is a useful tool that not only enables you to run a variety of operations but simplifies Magento development in general.
However, you might get overwhelmed by running the same commands over and over again. That's exactly when Magento 2 command shortcuts come in handy.
Surprisingly, many developers don't know about this. So in this guide, we've gathered the and command shortcuts for your convenience.
SSH
Full command
Shortcut
php bin/magento list
php bin/magento l
Cache
Full command
Shortcut
php bin/magento cache:clean
php bin/magento c:c
php bin/magento cache:enable
php bin/magento c:e
php bin/magento cache:disable
php bin/magento c:d
php bin/magento cache:flush
php bin/magento c:f
php bin/magento cache:status
php bin/magento c:s
Setup
Full command
Shortcut
php bin/magento setup:backup
php bin/magento se:b
php bin/magento setup:config:set
php bin/magento se:c:seihor
- 2 min read
Configurable products definitely stand out in the list of other . They come in handy when you want to provide your customers with multiple product options.
By default, Magento 2 doesn't allow you to assign the existing to the existing from the admin panel. The only option is to create a so-called placeholder in the configuration and then add the product you need instead of it.
That kind of solution is not really helpful, since it requires too many unnecessary steps. That is why a programmatic way to perform this task might of more use.
So, to add a simple product to a configurable product in Magento 2 programmatically, refer to the method below.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$simple_product = $objectManager->create(\Magento\Catalog\Model\Product::class);
$simple_product->setSku('test-simple');
$simple_product->setName('test name simple');
$simple_product->setAttributeSetId(10);
$simple_product->setSize_general(193); // value id of S sizeihor
- 1 min read
While product attribute is an individual characteristic of a product, is a group of those characteristics that define a product. You will use product attributes every time when creating products in Magento.
If you need to create multiple product attributes, doing this via the admin panel might require a lot of time. So in case, you need to do it fast, create an attribute set in Magento 2 programmatically.
Note: before you create attribute sets you have to create product attributes. So you might need to have a look at our guide on .
Use the following code to create an attribute set programmatically. Create the InstallData.php file in the app/code/Vendor/Module/Setup/InstallData.php.
<?phpnamespace Vendor\Extension\Setup;
use Magento\Framework\Setup\InstallDataInterface;use Magento\Framework\Setup\ModuleContextInterface;use Magento\Framework\Setup\ModuleDataSetupInterface;use Magento\Catalog\Setup\CategorySetupFactory;use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;ihor
- 2 min read
Product attributes are characteristics of a product that help you offer customers a wide range of options and influence their purchasing decisions. Once you , it doesn't end there. You can also use product attributes to define , cart price rules, , and multiple other options.
That being said, it is important for you to know how to create product attributes in Magento programmatically. Just in case you'll need to create multiple attributes faster.
To create product attributes in Magento programmatically create the InstallData.php file in the app/code/Vendor/Module/Setup/InstallData.php.
<?phpnamespace Vendor\Module\Setup;
use Magento\Eav\Setup\EavSetupFactory;use Magento\Framework\Setup\InstallDataInterface;use Magento\Framework\Setup\ModuleContextInterface;use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface{ private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactoryihor
- 1 min read
Related products are one of the most common ways to drive more sales in e-Commerce. You have to manually or automatically through the to increase the average order value. But there is one more way. You can get related products collection in Magento programmatically.
So if you want to avoid monotonous manual configuration of related products in Magento, keep reading.
To get related product collection in Magento 2:
1. Create an Extension.php file at app/code/Vendor/Extension/Block folder.
<?phpnamespace Vendor\Extension\Block;
use Magento\Framework\View\Element\Template;use Magento\Backend\Block\Template\Context;use Magento\Framework\Registry;
class Extension extends Template{ protected $registry; public function __construct( Context $context, Registry $registry, array $data = [] ) { $this->registry = $registry; parent::__construct($context, $data); } public function _prepareLayout() { return parent::_prepareLayout(); } public function getCurrentProduct() { return $this->registry->registry('current_product');}ihor
- 2 min read
In this article, we describe the GraphQl queries for getting the necessary auto-related rule data for Progressive Web App (PWA). To implement the GraphQL resolver you need to install .
Besides, you can easily check those queries with the ChromeiQL extension for Chrome Browser.
Related Product Rule GraphQL Request
With this query you will get all related products using rule ID (products that fall under the rule conditions).
Query:
query mfAutoRelatedProducts ($ruleId: Int $productId: Int $categoryId: Int $pageSize: Int $currentPage: Int) {
mfAutoRelatedProducts(
ruleId: $ruleId
productId: $productId
categoryId: $categoryId
pageSize: $pageSize
currentPage: $currentPage
){
total_count
items{
products{
id
name
sku
}
category_ids{
category_id
}
customer_group_ids{
group_id
}
store_ids{
store_id
}
id
name
description
status
priority
block_position
merge_type
from_one_category_only
only_with_higher_price
only_with_lower_price
conditions_serialized
actions_serialized
block_title
sort_byihor

