Türchen 22: How to: Complex product configurator in Magento 2

Dec
22
2018

On behalf of a merchant we built a complex product configurator for pallet racks. It is able to create individually assembled products from a large range of grouped products and displays them as a single entity – a bundled product – in cart. In this instructive post we demonstrate how this can be done in Magento 2.

The problem on a practical level

Our client’s expectations were defined clearly: Customers in the web shop shall be able to customize a rack consisting of a larger range of different parts to their individual needs by means of the configurator. After choosing the right edition (light, intermediate, or heavy duty) for the pallet rack he shall be able to define the number of rack bays and the number of shelves per rack bay. Mandatory supply like nuts and bolts shall be included automatically and upon request also optional extra parts like anti-ramming protection corners.

Product configurator in Magento 2

After finishing configuration the customer shall be able to add the individually assembled rack to cart and find it there as one entity instead of a chaotic hotchpotch of parts. Otherwise it would be unreasonable for the customer to select all the right parts in the accurate quantity to remove a complete rack from a cart full of parts belonging to two or more racks for example. And even if a customer would take on this busywork it could still happen that in the end there remains a selection of parts in the cart that do not fit together and would not add up to a complete and viable but to a completely insecure rack. This is what had to be avoided.

The problem on a technical level

As our task was to develop a configurator for the individual assembling of products, it was not an option to rely on pre-defined bundled products from the beginning. But with regard to the three editions of racks in the web shop there was the possibility to define a complex grouped product for each of the payloads as base for the configurator. So there would be one configurator on each of the three product pages. But in the end only the contained parts of a grouped product, i.e. simple products, are added to cart. And in the case of an individually configured pallet rack this would be quite a lot of parts. And as soon as more products are added to cart it would no longer be apparent which parts belong to the rack and which don’t.

Hotchpotch of simple products in cart

Hotchpotch of simple products in cart

This is why it was necessary to automatically create a bundled product from the parts of the grouped product and set the corresponding options out of the configurator for displaying the completely assembled rack as an entity as soon as the customer adds the rack to cart. A customer that would have configured two different racks would find in his cart only two products that are bundled products on a technical level and contain exactly the parts belonging to the respective rack.

Two pallet racks as bundled products in cart

Two pallet racks as bundled products in cart

The solution

In the Magento tests there is a php file that creates a bundled product. This file is to be found in
tests/integration/testsuite/Magento/Bundle/_files/product.php.
We used this file as a basis for the creation of a bundled product in our use case.

To transform the grouped product to a bundled product in the right moment (when adding it to cart) it is necessary to edit Magento\Checkout\Model\Cart. The method addProduct($productInfo, $requestInfo = null) has to be extended with this request:

First a bundled product with all needed options is being created using the method _createBundleCopy($productInfo, $requestInfo):

The options for the bundled product are created in _createOptionsArray($requestInfo). For the right mapping to be created it is necessary to get a closer look at the request. A request for a grouped product looks like this:

Here the array super_group is in focus because the options for the bundled product need to be created from it. The respective key in the array is the entity ID of the single product. The value is the quantity of the selected product. With these informations the options are created with the method _createOptionsArray($requestInfo). As it is known precisely which simple products were chosen in which quantity by the customer from the configuration of the grouped product the exactly matching options for the bundled product can be created from it.

Now the options can be added to the bundled product. This is done in the second part of the method _createBundleCopy.

After the options are added, the bundled product is saved and will now be available in the web shop.

As a next step the request has to be edited. It has to be transformed from the original request for a grouped product to a request for a bundled product. This requires a closer look at the request for a bundled product:

The options for the bundled product can be found in the array [bundle_option]. These have already been created from the array [super_group] by means of the method _createOptionsArray($requestInfo). Now the request is converted so it can be used for a bundled product with the method _changeRequestInfo($entityId, $requestInfo). To do so the options have to be fetched from the database. The options for this bundled product can be found in the table catalog_product_bundle_selection. From the column parent_product_id in this table the entries containing the entity ID of the bundled product have to be selected. As the options have been created just as they are needed for this configuration before, they can now be added to the request:

After the request for a grouped product has been transformed into a request for a bundled product the product can now be added to cart as usual. This is done with the method addProduct($productInfo, $requestInfo = null) again:

The further processing is standard Magento.

But there is one thing to note: With this solution a new bundled product will be created from the grouped product in each purchase. We solved that by automatically deleting the respective bundled product whenever a rack has been bought in the web shop.

Comments

  • 20. April 2019

    As a merchant in rackingsystems http://www.an-ink.nl, I’m looking for a racking configurator module as described at this blog.
    As a beginner in magento i’m not very experienced in creating something new, so i’m hoping there is some sort of module which i can implement to get a racking configurator.

    Thank you in advance,

    William.

    • cstech
      23. April 2019

      We’re sorry, William, but there is no pre-build module you could use.

Kommentieren

Your email address will not be published. Required fields are marked *