0

I am having a bit of a issue figuring out the correct method to implement this. I am using OneStepCheckout on my site with free delivery for minimum cart value. When customers add product to their cart and proceed to the checkout page and if their subtotal amount is lower than minimum order required for free shipping then table rate shipping is calculated and displayed at the shipping method. Many customers are adding product quantity to qualify for the free shipping method on the checkout page but some of them are not manually selecting free shipping after adding the required quantity.

Please help me out... is there a way I can make sure the cheapest available shipping method is selected even in this scenario, so by default free shipping will be selected when customers place their orders.

Thanks

My code for app/code/Mageplaza/Osc/Model/Plugin/Checkout/ShippingMethodManagement.php is very different. would appreciate to know what edits should I make in this.

<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Osc
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\Osc\Model\Plugin\Checkout;

use Closure;
use Exception;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\EstimateAddressInterface;
use Magento\Quote\Model\ShippingMethodManagement as QuoteShippingMethodManagement;
use Magento\Customer\Api\Data\AddressInterface as CustomerAddressInterface;
use Magento\Quote\Model\Quote;

/**
 * Class ShippingMethodManagement
 *
 * @package Mageplaza\Osc\Model\Plugin\Checkout
 */
class ShippingMethodManagement
{
    /**
     * Quote repository.
     *
     * @var CartRepositoryInterface
     */
    protected $quoteRepository;

    /**
     * Customer Address repository
     *
     * @var AddressRepositoryInterface
     */
    protected $addressRepository;

    /**
     * @param CartRepositoryInterface    $quoteRepository
     * @param AddressRepositoryInterface $addressRepository
     */
    public function __construct(
        CartRepositoryInterface $quoteRepository,
        AddressRepositoryInterface $addressRepository
    ) {
        $this->quoteRepository   = $quoteRepository;
        $this->addressRepository = $addressRepository;
    }

    /**
     * @param QuoteShippingMethodManagement $subject
     * @param Closure                       $proceed
     * @param                               $cartId
     * @param EstimateAddressInterface      $address
     *
     * @return mixed
     * @throws NoSuchEntityException
     */
    public function aroundEstimateByAddress(
        QuoteShippingMethodManagement $subject,
        Closure $proceed,
        $cartId,
        EstimateAddressInterface $address
    ) {
        $this->saveAddress($cartId, $address);

        return $proceed($cartId, $address);
    }

    /**
     * @param QuoteShippingMethodManagement $subject
     * @param Closure                       $proceed
     * @param                               $cartId
     * @param AddressInterface              $address
     *
     * @return mixed
     * @throws NoSuchEntityException
     */
    public function aroundEstimateByExtendedAddress(
        QuoteShippingMethodManagement $subject,
        Closure $proceed,
        $cartId,
        AddressInterface $address
    ) {
        foreach ($address->getCustomAttributes() as $attribute) {
            if (isset($attribute->getValue()['value'])) {
                $attribute->setValue($attribute->getValue()['value']);
            }
        }

        $this->saveAddress($cartId, $address);

        return $proceed($cartId, $address);
    }

    /**
     * @param QuoteShippingMethodManagement $subject
     * @param Closure                       $proceed
     * @param                               $cartId
     * @param                               $addressId
     *
     * @return mixed
     * @throws LocalizedException
     */
    public function aroundEstimateByAddressId(
        QuoteShippingMethodManagement $subject,
        Closure $proceed,
        $cartId,
        $addressId
    ) {
        $address = $this->addressRepository->getById($addressId);
        $this->saveAddress($cartId, $address);

        return $proceed($cartId, $addressId);
    }

    /**
     * @param                                                                    $cartId
     * @param EstimateAddressInterface|AddressInterface|CustomerAddressInterface $address
     *
     * @return $this
     * @throws NoSuchEntityException
     */
    private function saveAddress($cartId, $address)
    {
        /** @var Quote $quote */
        $quote = $this->quoteRepository->getActive($cartId);

        if (!$quote->isVirtual()) {
            $addressData = [
                AddressInterface::KEY_COUNTRY_ID      => $address->getCountryId(),
                AddressInterface::KEY_POSTCODE        => $address->getPostcode(),
                AddressInterface::KEY_REGION_ID       => $address->getRegionId(),
                AddressInterface::KEY_STREET          => $address->getStreet(),
                AddressInterface::KEY_CITY            => $address->getCity(),
                AddressInterface::CUSTOMER_ADDRESS_ID => $address->getId()
            ];

            $shippingAddress = $quote->getShippingAddress();
            try {
                $shippingAddress->addData($addressData)
                    ->save();
                $this->quoteRepository->save($quote);
            } catch (Exception $e) {
                return $this;
            }
        }

        return $this;
    }
}

1 Answer 1

0

Try this code, i have hardcoded the shipping method or you can create your custom logic here

Vendor/Module/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\ShippingMethodManagement">
        <plugin name="tfc_shipping_method_management_plugin" type="Vendor\Module\Plugin\Model\ShippingMethodManagement" />
    </type>
</config>

\Vendor\Module\Plugin\Model\ShippingMethodManagement.php

<?php

namespace Vendor\Module\Plugin\Model;

class ShippingMethodManagement
{
    /**
     * This could be fetched from configuration field where you would be able to define multiple methods
     */
    const ALLOWED_METHODS = array(
        'custom_free_shipping'
    );

    /**
     * @param $shippingMethodManagement
     * @param $shippingMethods
     * @return array
     */
    public function afterEstimateByExtendedAddress($shippingMethodManagement, $shippingMethods)
    {
        /**
         * Add configuration to disable the functionality
         */
        if(true) {
            $shippingMethods = $this->getAllowedMethods($shippingMethods);
        }

        return $shippingMethods;
    }

    /**
     * Filter allowed methods if any are set
     *
     * @param $shippingMethods
     * @return array
     */
    protected function getAllowedMethods($shippingMethods)
    {
        $allowedMethods = [];

        foreach ($shippingMethods as $shippingMethod) {
            if(in_array($shippingMethod->getCarrierCode(),self::ALLOWED_METHODS)) {
                $allowedMethods[] = $shippingMethod;
            }
        }

        if($allowedMethods) {
            return $allowedMethods;
        }

        return $shippingMethods;
    }
}

here custom_free_shipping is my shiiping method

2
  • thanks for the help
    – rohit
    Aug 30, 2022 at 16:53
  • I have added my shippingmethodmanagement.php what edits should I make to the code?
    – rohit
    Aug 30, 2022 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.