Prevent Shipping Methods Based on Conditions

Let’s Begin with the new thing today. The most common or I would say, one of the most important too topic of Magento 2. Every Store flow in Magento has to go through this. so we can say it’s a common path for all.

Well Before proceeding with this topic Preventing Shipping. Let discuss the shipping page(Checkout Step 1) first.

Let’s not go into that deep. Just an overview that why we require the Shipping methods. As we all know on the Ecommerce store each product requires a shipping method to make the order successfully delivered to one’s Address.

So, the shipping methods comes many with challenges. Here are few Examples:

Shipping Method customization depends upon factors like Price, weight, products, country, etc.
Show custom message as per conditions and many more.

Here we will apply the condition to prevent Shipping methods depending upon conditions like products, price, weight, for all shipping methods, etc.

Let’s begin with this Step by step.

Let’s start with the Basic Module Structure First. Here I assume one should already Have Basic knowledge about Module and its structure, if not please refer to this link first.

I have created an Excellence_Preventshipping Module.

So, we first have to take an excess of the method which is responsible for filtering and shows valid shipping methods on checkout. Magento uses Shipping.php to validate all shipping methods

Magento/Shipping/Model/Shipping.php

Here in this we have to use collectCarrierRates($carrierCode, $request) function.

For this we can either override this model file or use plugin for this around/after/before to validate our conditions.

For now, I am overriding this model.

Let’s create di.xml file to override this class.

Now, as we have overridden the shipping.php class to our vendor files which are at Excellence\Preventshipping\Model\Rewrite\Shipping. So create a file at this path with the same name Shipping.php as we have given in di.xml.

Here in the above section, we have added the condition in collectCarrierRates() function for validation in which we are passing the method name.

Similarly, we can add a condition for price and weight too, depending upon our requirement.

We can also use a plugin in collectCarrierRates() function to do this and return false if our condition gets true.

Sending Email with Attachment in Magento 2.3.x

The Magento 2.3.x framework (actually as all previous versions) does not provide an opportunity for Sending Emails with Attachment to email messages. However, the implementation of this functionality for Magento 2.3.x is different from that of previous versions.  That’s the reason the Magento 2.3.x framework uses Zend Framework 2 (ZF2) and almost totally refuses to apply ZF1. 

There listed all Magento extensions are tested and fully compatible with Magento version 2. click here https://xmagestore.com/magento2.html

In This Article, we are going to describe how to implement the email with attachment in Magento 2.3.X with using the Coding technique.

To understand this process, it’s recommended to get an idea of some of the ZF2 components such as:

As createAttachment() method is no more available, we will override Magento\Framework\Mail\Message and create a class which will implement Magento\Framework\Mail\MailMessageInterface.

In this method, we will redifine/create some methods which would be responsible for different MIME parts which would be merged later before being sent. Let’s see some of the important ones:

Message::setBodyText() Method

In this method, we are adding the text MIME part to the email and storing in $this->parts array.

Message::setBodyHtml() Method

In this method, we are doing the same as above but we are setting the HTML part and storing in the same $this->parts array.

Message::setBodyAttachment() Method

Here we set the content of the file, then set a file type and specify its name. In addition, we set disposition. It’s necessary for the file to be treated as an attachment.

Above we’ve described all methods that help us to add mime parts to the message. But we also added to this class one more important method – Message::setPartsToBody().

Message::setPartsToBody() Method

This method is used to put all the pieces together.

Now, we will create a new class TransportBuilder which will extend the actual core class Magento\Framework\Mail\Template\TransportBuilder and instead of the core class, we will be using this new class.

TransportBuilder::addAttachment() Method

Now, you can call this method while sending mail as used in this file.

Further, you can find the entire module here. GitHub

Magento 2 Console Command Sortcuts

While working on Magento 2.x, you often need to run several console commands which are actually very handy in terms of managing the things such as cache, upgrades, deploy modes, indexers etc. You can check the entire list of commands by running this command:

 php bin/magento list 

Well, typing the full long commands are not very pleasant if it has to be done multiple times. So, we can use shortcuts instead full commands. This feature comes due to the Symphony framework which is used by each console class that implement command line functionality. The use Symfony\Component\Console\Command component to implement command line features. You can learn more in the documentation of Symphony framework. You can use shortest unambiguous name intead of full name, e.g. s instead of setup.

Now, let’s see shortcuts for some of the commonly used Magento 2 console commands:

1. Cache Commands

Where cache_type is the parameter in which you can pass one or more cache types separated by whitespaces, e.g.

php bin/magento c:c config layout

2. Indexer Commands

You may pass parameters for particular indexer type as well.

3. Compile Command

4. Setup Upgrade Command

5. Static Content Deploy Command

6. Module Management Commands