How to Use and Customize Magento 2 Catalog: Products and Categories, Price and Price Rules

Jan 29, 20191555Andrey Litvin
How to Use and Customize Magento 2 Catalog: Products and Categories, Price and Price Rules

This article is dedicated to a rather complex and wide topic from Magento 2 Backend Specialist Certification – Customizing the Catalog. Here, we will walk over the following four aspects:

How to use products and product types in Magento 2
What is price functionality in Magento 2
How to use and customize categories in Magento 2
How to determine and manage catalog rules in Magento 2

How to use products and product types in Magento 2

What are the standard product types in Magento 2?

There are 6 standard product types in Magento 2:

1.Simple Product. The most basic and widely used product type. One simple product denotes one physical item with its unique SKU (Store Keeping Unit).

Simple product magento 2

2. Virtual Product. This product type is for intangible goods, like services or subscriptions, etc.

virtual product magento 2

3. Configurable Product. This product type allows to create and show product configurations, like colors, sizes, etc. Each product configuration is a simple product with its unique SKU, allowing you to keep track of your stock amount.

configurable product magento 2

4. Grouped Product. This product type allows to unite simple and virtual products in sets. Grouped product provide a win-win situation: a merchant promotes goods that are commonly used together and a customer doesn’t need to search the shop and can buy the whole set in one place. What is more, each product is added to the cart separately, in the amount, color and size the customer wants.

grouped product magento 2

5. Bundle Product. This product type provides customers with an opportunity to create a set themselves, forming a bundle from single products.

bundle product magento 2

Before being added, the item needs to be configured (size, color, etc.), and the total price will depend on this configuration.

bundle product parameters magento 2

For bundle product SKU and Weight attributes can be fixed or dynamic; as for price, it can be set at Price Range (from maximum to minimum price) or As Low As (the lowest price). Admin can specify, how the bundle products will be delivered – together or separately.

6. Downloadable Product. This product type is for virtual items, usually in the form of a file that can be downloaded by the customer after the payment is made.

Downloadable Product magento 2

Virtual and Downloadable Products don’t have weight, don’t need to be delivered by post and don’t have In Stock attribute.

To get all products of a certain type, use Magento\Catalog\Model\ResourceModel\Product\Collection class:

As the second parameter set the product type name:bundle, configurable, downloadable, grouped, simple or virtual.

What tools (in general) does a product type model provide?

Basic product methods for every product type:

  • getSetAttributes – gets backs attribute set of a product;
  • getAttributeById – gets backs attribute by its ID and product ID;
  • isVirtual – determines if the product is virtual;
  • isSalable – determines if the product is Salable;
  • isComposite – determines if the product is composite;
  • canConfigure – determines if the product can be configured;
  • prepareForCart – initiates the product for adding to the cart;
  • checkProductBuyState – checks whether you can buy a product;
  • getSku – backs up product SKU, whether it has product or no;
  • hasOptions – checks if the product has options;
  • hasWeight – checks if the product has weight;
  • getRelationInfo – gets backs information on product relations;
  • getAssociatedProducts – gets backs associated products;
  • getChildrenIds – gets backs the list of child product id;
  • getParentIdsByChild – gets backs the list of parent product id;
  • assignProductToOption – assigns options to products.

What additional functionality is available for each of the different product types?

Configurable:

  • getConfigurableAttributes – gets back the attributes used for subproducts;
  • getUsedProductIds – gets back subproducts id;
  • getProductByAttributes – gets back products by attributes;
  • getConfigurableOptions – gets back list of options;
  • setImageFromChildProduct – sets child product picture for parent product if it hasn’t been set before.

Bundle:

  • getOptions – gets back options list;
  • getSelectionsCollection – gets back options collection by its id;
  • getSpecifyOptionMessage – gets buyer’s message back with the request to specify options;
  • checkIsAllRequiredOptions – checks if all the required options were selected;
  • checkSelectionsIsSale – checks if all options are available for sale.

Downloadable:

  • getLinks – gets back the links for product download;
  • hasLinks – checks if there are links for product download;
  • getLinkSelectionRequired – checks if the product can be purchased without selected links;
  • getSamples – gets back samples for product download;
  • hasSamples – checks if the product has downloadable samples.

Keep in mind that the getRelationInfo, getAssociatedProducts, getChildrenIds, getParentIdsByChild, etc. methods are common to all product types, but return an empty array if applied to not composite types (Simple or Virtual). The logic of these methods is implemented in classes of composite types, taking into account the specifics of this type.

What is price functionality in Magento 2

What are the basic concepts of price generation in Magento 2?

Each product in Magento 2 usually has several prices: regular, special, final, etc. Each type of price corresponds to its class, which calculates the final sum for this type of price. Some types of prices are available for all products, while others are specific to a particular product type.

Here are the most common types of prices applicable to all types of products:

  • base_price – product cost at the default exchange rate;
  • regular_price – product cost at the rate of the selected currency;
  • final_price – the final cost of the product;
  • special_price – product price with a discount;
  • tier_price – price depending on quantity of items in the basket;
  • custom_option_price – the cost of the option, if the product has them,
  • configured_price – product price, including options;
  • catalog_rule_price – product price after catalog rules application.

The special_price, tier_price, custom_option_price types can be fixed or set in percentages. If necessary, it can be set for specific user groups.

The common classes responsible for implementing different types of prices are in the Magento \ Catalog \ Pricing \ Price namespace and extend the Magento \ Framework \ Pricing \ Price \ AbstractPrice class. Different types of products usually override the logic of calculating a particular type of price. In this case, the corresponding classes are in the namespace of the corresponding product type. Also, some types of products add their own price types:

  • downloadable products: link_price – the cost of the product when the customer downloads it from a specific link;
  • bundle products: bundle_option – product bundle option cost.

Each type of price has getValue () and getAmount () methods. The getValue () method returns the price value, and the getAmount () method returns the total price value with all sorts of taxes.

How would you identify what is composing the final price of the product in Magento 2?

final_price of the product depends on its type.

For ordinary products (simple, virtual) it corresponds with the lowest value from regular_price, catalog_rule_price, special_price, tier_price.

For configurable products cost of each option is selected as the minimum value from base_price, tier_price, index_price, catalog_rule_price. At the same time, when the minimum price is selected, the system checks status of the product’s configurable option, its availability for a particular site and its availability. After determining the final_price of all options, the final price of the configurable product is determined, which will be equal to the lowest cost of its options.

The final_price of the grouped product is equal to the smallest final_price of all the products in the group.

For bundle products, the cost is calculated in the same way as for regular products + bundle_option – the cost of all the required options multiplied by their quantity.

How can you customize the price calculation process in Magento 2?

There are several ways to change the price calculation process:

1. Create a new price type and add the set of basic types to it. To do this, you need to create your own module with a class that will implement the Magento \ Framework \ Pricing \ Price \ BasePriceProviderInterface interface and extend the Magento \ Framework \ Pricing \ Price \ AbstractPrice class, as well as add the necessary instructions to the module di-file:

2. Create a plugin for methods of the class of the price type to change the process of its calculation, for example, around the getValue () method.

3. Override the class that corresponds to the price type we need, for a specific type of product, using the di-file in its module:

4. Completely override price class using the preference instruction in module di-file.

How is price rendered in Magento 2?

Let me demonstrate how the price display is formed taking the formation and display of the final_price of a regular product as an example. Class Magento \ Catalog \ Pricing \ Render \ FinalPriceBox is responsible for price display. The template that is used when displaying the contents of the block is Magento_Catalog :: product / price / final_price.phtml. In case a special price is se for the product, the user will be shown the old and new prices by means of calling the block’s renderAmount () method. Otherwise, only one final price will be displayed. In this case, the lowest price of all the possible ones will be selected. Then, if the product has options (for example, size), the cost of the cheapest option is displayed in a block.

How would you render price in a given place on the page, and how would you modify how the price is rendered?

In order to display the price in a certain part of the page, you need to add a block of Magento\Catalog\Pricing\Render class to your layout and give it price_type_code parameter as an argument.

Using data-arguments, you can change price box, amount renders и adjustment renders by editing their сss-classes, id_suffix, id_prefix, etc.

In order to change price display template, you can create your own catalog_product_prices.xml. There you can change the render.product.prices block, giving it the name of the block class and your template name as parameters for a certain price type.

How to use and customize categories in Magento 2

Categories are significant elements of an online shop, for they are literally the roots of your products tree. They can and should be used to provide general and introductory information on your products or services to visitors. Therefore, it’s very important to know how to manage and organize it correctly.

Category properties and features in Magento 2

To begin working with categories, log into your Magento 2 admin panel and go to Catalog -> Categories. Click on any category you wish to configure.

magento 2

Each category has the following settings:

  • Enable Category — turn the category on or off for customer view.
  • Include in Menu — enable or disable the category in the top menu.
  • Category name.
  • Content tab with obligatory Category Image and Description fields. You can also include an additional CMS block by choosing it from the drop-down list.
  • Display settings tab contains rather important fields, so let’s dwell on them a little bit longer.
    In the Display Mode drop down field you define how are products demonstrated in the category. It contains three options: Products only, Static block only and Static block and Products.

display settings magento 2

  • By enabling Anchor, you turn on layered navigation display for this category.

Search Engine Optimization tab allows to improve the search engine ranking of the category by filling in URL Key, Meta Title, Keywords and Description.

In Products in Category tab you fill in the attributes of every product the category should contain: ID, Name, SKU, Visibility, Status, Price and Position.

Design tab contains category visual configuration fields, like Theme, Layout and Layout Update XML. If you enable Use Parent Category Settings button, the fields mentioned above will be set up like in the parent category. You can also enable Apply Design to Products button in order to unify category and product pages style.

Set up in Schedule Design Update tab for how long the settings will apply to this category.

schedule design update magento 2

Layout Update Xml is used for adding xml instructions through the administrator interface.

How to create and manage categories in Magento 2?

Go to Catalog -> Categories menu.

categories magento 2

In the left corner you need to choose the type of category you can create: Root Category or Subcategory.

root category and subcategory

The difference between a Root Category and a Subcategory is quite obvious – a root category is one large section of your product catalog, while a subcategory is a subsection of an already existing category (eg. Men’s Apparel is a root category with a Men’s Trousers subcategory).

How to implement the category hierarchy tree structure in Magento 2?

Magento 2 categories are organized in the form of a tree and can have any nesting.

This picture demonstrates the structure of the catalog_category_entity table, where the data about category structure is stored.

category entity Magento 2

The following columns are the main responsible for the category structure:

  • entity_id is category identification;
  • parent_id is the parent category id;
  • path is the category path from category tree in the form of sequential arrangement of entity_id categories, separated by slash (/).

entity id magento 2

Root Categories are the records with parent_id = 0 value and value of entity_id = 1. The following subcategories tree construction occurs via the parent_id <—> entity_id relationship.

root category 1

root category 2

Which attribute values are required to display a new category in the store?

As you create a category, you assign it a certain Category name; based on it, URL Key, or the path leading to the category in the browser, is generated automatically.

attribute values magento 2

 

What kind of strategies can you suggest for organizing products into categories?

The strategy of organizing products into categories usually depends on the specifics of your online shop, and it’s where not only developers, but marketing specialists step into the game.

You can organize items into categories according to target group of buyers (men, women, children, etc.) or their purpose (tourism, fitness, running, etc.) or whichever logic you believe is the most suitable. But please bear in mind that the products should share common attributes, based on which you will create a filter, like shirt size or color.

How to determine and manage catalog rules in Magento 2

When would you use catalog price rules in Magento 2?

Giving a discount is a great way to attract new customers and retain the existing ones, that is why most online stores announce them – either seasonal or associated with a special occasion. In Magento 2 ecommerce platform the process of assigning discounts can be automated with the help of catalog price rules.

How do they impact performance in Magento 2?

Catalog price rules have minimum impact on page loading due to the fact that price calculations are performed during reindexing. So the larger the catalog price rule is, the longer reindexing will take.

If you wish to make sure the performance is not overloaded by catalog price rules, take a look at Magento code. In the vendor\magento\module-catalog-rule\etc\frontend\events.xml file we install the observer for catalog_product_get_final_price event.

Then we get the price from magento\module-catalog-rule\Observer\ProcessFrontFinalPriceObserver.php file and set it up for the product.

Now let’s have a look at getRulePrice() method. Find this method in Z:\m2.loc\vendor\magento\module-catalog-rule\Model\ResourceModel\Rule.php file and find this method here; getRulePrice() method will call the getRulePrices() that performs a simple inquiry to the catalogrule_product_price table from the database. And that is how you get the price.

How would you debug problems with catalog price rules in Magento 2?

The first step is to make sure the price rule is active; go to Marketing -> Promotions -> Catalog Price Rule and check if the status is set at active. Press Apply Rules button.

catalog-price-rule-magento-2

After that Catalog Rule Product indexes and Product Price, connected with it, is marked invalid.

index-management-magento-2

Meanwhile, in the controller vendor\magento\module-catalog-rule\Controller\Adminhtml\Promo\Catalog\ApplyRules.php
an increment of \Magento\CatalogRule\Model\Rule\Job class is created; this increment calls the applyAll() method.

In this method the index is marked as not valid:

Then we need to launch reindex and clear the cash.
bin/magento indexer:reindex
bin/magento cache:flush

Afterwards, the rules will work, if, of course, price rule, its date ranges and conditions are configured correctly (date ranges and conditions).

In case they don’t work, you need to go to catalogrule_product_price table and check the product price there. If you found that the data is incorrect, check the logs or try to disable the third-party modules if you have some; you can also go over the Catalog Price Rule reindexing with Xdebug extension which will detect the issue.

This is all on the topic of cart rules management in Magento 2. If you have any questions or comments, please leave them down below.

 



Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *