How to Add Custom Product Attribute as a Filter for the Shipping Rates

0
2253
How to Add Custom Product Attribute as a Filter for the Shipping Rates
Reading Time: 8 minutes

Often, in Magento 2, the number of standard product attributes that can be used to set up conditions is limited. Extra customization would be required to meet business needs.

From this article, you’ll learn how to achieve that and add custom product attributes as a filter for the shipping rates.

Notes: 

  1. See the complete code example on GitHub
  2. The first part of the example, which adds the ‘Volume Weight’ attribute as a filter to shipping rates, is available here.
  3. The original Magento 2 Shipping Suite Ultimate module is required.

Let’s go straight to discussing what exactly shall be done to achieve the objective.

Step-by-Step Guide on Adding Custom Product Attribute 

Step 1. Create a New Module by Adding the Base Files

Start with naming the module:

Then, detect and declare its requirements. You’ll also need to give a name to our module for the composer, set version, and add a short description:

Further, we can set the initial name and version in the Magento 2 configuration, declare the sequence:

Step 2. Create a Module Structure

Let’s assume that we have a product attribute named ‘shippingnew’, which was created from the admin side. It is a dropdown input type and has few options named ‘A, B, C, D’, etc. These options describe how we ship our items by zones. Each value has its own price, and products with the highest price will modify the shipping method cost during checkout.

First of all, we need to create a separate table for our shipping rates extended conditions. Later, we will add them using the regular extension attributes of the model (the ‘Shipping Rate’ model extends \Magento\Framework\Model\AbstractExtensibleModel ).

We named our table as follows: 'mageworx_shippingrules_rates_shippingnew'. It has just 2 columns. One of them is used as a foreign key. It is the rate_id column, which will be linked with the regular table mageworx_shippingrules_rates from the MageWorx Shipping Suite Ultimate module for Magento 2. Another column will contain values from the shippingnew attribute.

Before we make an observer load/save/delete our custom data to the table, we must create at least two models―regular model and resource model.

Notes:

  1. _eventPrefix will be used to detect our model events.
  2. _eventObject will be used to store data in the event object. Using this name we can get our model from the event object.
  3. $this->_init('MageWorx\ShippingRateByProductAttribute\Model\ResourceModel\ShippingNew’); links our model with the corresponding resource model.
  4. $this->setIdFieldName('rate_id'); describes, which field from the table must be used as a key (usually we call it id)

Notes:

  1. $this->_init(‘mageworx_shippingrules_rates_shippingnew’, ‘rate_id’); set the main table name and id field name.
  2. public function insertUpdateRecord($rateId, int $shippingNew) is the method, which could help us update the attribute value in our custom table.
  3. public function deleteRecord($rateId) is designed to remove the column.

Later, we will use those methods in our observers.

Now, let’s add our new data as an extension attribute to the Shipping Rate model:

We should also take care of the regular operations of our custom condition:

The first event is for save/update/delete custom attribute value in the rates condition.

The second two events are for adding this attribute value to the collection.

The last three events are for the Import/Export functionality. 

Let’s analyze them one by one in more detail:

It’s as simple as that. When we save a rate, we must take care of saving the custom attribute value too. In case its value equals null, just delete a record.

To make validation available, when a customer goes to the checkout or shipping rates estimation, let’s join our table with the custom attribute to the regular rates table.

This is the most complicated observer in our stack. It is designed to collect all attribute values ($shippingCategories) from a customer’s cart and adds the attribute value as a filter to the regular rates collection (our table is already joined). To keep it simple, I’ve named it ‘Filter’. When work is done, a customer will see actual shipping rates for the current cart items.

Another 3 event observers are designed to add and receive custom data during shipping rates export and import. We skip its code in the blog post, but it will be available in the repository with the source code.


MageWorx Magento 2 Extensions

Step 3. User Interface

It’s time to add our attribute to the grid and to the form of the shipping rates.

Form

Grid

As you can see, we use the custom source model in those files. Let’s create it. It will load a corresponding attribute (shippingnew) and give us all the available values.

The piece of code is pretty simple. We use the attributes repository to load our attribute and then obtain all options (values) from it. Keep in mind that the attribute with the shippingnew code must be created in the admin panel and must have a dropdown input type with predefined options (values). You can do that from the menu ‘Stores > Attributes > Product’. Don’t forget to add this attribute to the attribute set you are using for the products.

When everything is done, we only need to enable the module and run the setup:upgrade. Cache will be cleared automatically.

Go to the rates grid (‘Stores > Shipping Rates’) and you’ll see the new column:

How to Add Custom Product Attribute as a Filter for the Shipping Rates | MageWorx Blog

That condition will be available inside the rates form:

How to Add Custom Product Attribute as a Filter for the Shipping Rates | MageWorx Blog

If we set the ‘Multiple rates price calculation’ setting to the ‘Use Rate with Max Price’ in the corresponding Shipping Method, the rate with the highest price will be used during shipping price calculation. 

Here is a small example of how it works in the format of screenshots:

  1. Set up your  products
How to Add Custom Product Attribute as a Filter for the Shipping Rates | MageWorx Blog
  1. Set up the rates
How to Add Custom Product Attribute as a Filter for the Shipping Rates | MageWorx Blog
  1. Set up the  price calculation algorithm (in the shipping method form)
How to Add Custom Product Attribute as a Filter for the Shipping Rates | MageWorx Blog
  1. Check shipping price for the corresponding method with selected products in the cart (on the frontend).
How to Add Custom Product Attribute as a Filter for the Shipping Rates | MageWorx Blog
How to Add Custom Product Attribute as a Filter for the Shipping Rates | MageWorx Blog

This is not everything the Shipping Suite module is capable of. Feel free to play with the settings to get the desired result.

I’ll be glad to answer any questions! Thus, feel free to leave your comments in the dedicated field below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here