2

I need to apply table rate shipping based on following conditions

City to City

Metro to City

Metro to Metro

City to Other

City to Special Locations

I written my own module but i am getting only the postal code and country id in $request

Array ( [shipping_details] => Array ( [0] => Array ( [seller_id] => 0 [items_weight] => 4.05 [product_name] => My Tshirt) [item_id] => 812 ) ) [dest_country_id] => IN [dest_postal] => 500049 ) 

My code which is caluculating carrier amount as follows. How can i get the city name and state/region id in $request.

namespace My\TableRateShipping\Model;

use Magento\Quote\Model\Quote\Address\RateResult\Error;
use Magento\Quote\Model\Quote\Address\RateRequest;
use My\TableRateShipping\Model\ShippingmethodFactory;
use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\Session\SessionManager;
use Magento\Quote\Model\Quote\Item\OptionFactory;
use My\TableRateShipping\Model\TableRateShippingFactory;
use \Magento\Framework\Unserialize\Unserialize;

class Carrier extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
    \Magento\Shipping\Model\Carrier\CarrierInterface
{
    /**
     * Code of the carrier.
     *
     * @var string
     */
    const CODE = 'mytablerateshipping';
    /**
     * Code of the carrier.
     *
     * @var string
     */
    protected $_code = self::CODE;
    /**
     * Rate request data.
     *
     * @var \Magento\Quote\Model\Quote\Address\RateRequest|null
     */
    protected $_request;
    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $_productFactory;
    /**
     * Rate result data.
     *
     * @var Result|null
     */
    protected $_result;
    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $_objectManager;
    /**
     * @var array
     */
    protected $_errors = [];

    protected $_isFixed = true;
    /**
     * @var \Magento\Shipping\Model\Rate\ResultFactory
     */
    protected $_rateResultFactory;
    /**
     * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
     */
    protected $_rateMethodFactory;
    /**
     * Raw rate request data.
     *
     * @var \Magento\Framework\DataObject|null
     */
    protected $_rawRequest = null;
    /**
     * @var My\TableRateShipping\Model\ShippingmethodFactory
     */
    protected $_mpshippingMethod;
    /**
     * @var Magento\Framework\Session\SessionManager
     */
    protected $_coreSession;
    /**
     * @var OptionFactory
     */
    protected $_itemOptionModel;
    /**
     * @var My\Marketplace\Model\ProductFactory
     */
    protected $_mpProductFactory;
    /**
     * @var TableRateShippingFactory
     */
    protected $_mpShippingModel;
    /**
     * @var Unserialize
     */
    protected $_unserialize;
     /**
     * @var Unserialize
     */
    protected $collectionFactory;
    /**
     * @param \Magento\Framework\App\Config\ScopeConfigInterface          $scopeConfig
     * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory  $rateErrorFactory
     * @param \Psr\Log\LoggerInterface                                    $logger
     * @param \Magento\Shipping\Model\Rate\ResultFactory                  $rateResultFactory
     * @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
     * @param ProductFactory                                              $productFactory
     * @param \Magento\Framework\ObjectManagerInterface                   $objectManager
     * @param ShippingmethodFactory                                     $shippingmethodFactory
     * @param SessionManager                                              $coreSession
     * @param OptionFactory                                               $itemOptionModel
     * @param TableRateShippingFactory                                           $mpshippingModel
     * @param array                                                       $data
     */

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
        \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
        ProductFactory $productFactory,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        ShippingmethodFactory $shippingmethodFactory,
        SessionManager $coreSession,
        OptionFactory $itemOptionModel,
        ShippingFactory $mpshippingModel,
        Unserialize $unserialize,
        \Magento\Framework\Data\CollectionFactory $collectionFactory,
        array $data = []
    ) {
        $this->collectionFactory = $collectionFactory;
        $this->_productFactory = $productFactory;
        $this->_objectManager = $objectManager;
        $this->_rateResultFactory = $rateResultFactory;
        $this->_rateMethodFactory = $rateMethodFactory;
        $this->_mpshippingMethod = $shippingmethodFactory;
        $this->_coreSession = $coreSession;
        $this->_itemOptionModel = $itemOptionModel;
        $this->_mpShippingModel = $mpshippingModel;
        $this->_unserialize = $unserialize;
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
    }
    /**
     * Collect and get rates.
     *
     * @param RateRequest $request
     *
     * @return \Magento\Quote\Model\Quote\Address\RateResult\Error|bool|Result
     */
    public function collectRates(RateRequest $request)
    {
        $this->setRequest($request);
        $shippingpricedetail = $this->getShippingPricedetail($this->_rawRequest);

        return $shippingpricedetail;
    }
    /**
     * @param \Magento\Framework\DataObject|null $request
     * @return $this
     * @api
     */
    public function setRawRequest($request)
    {
        $this->_rawRequest = $request;

        return $this;
    }
    /**
     * Prepare and set request to this instance.
     *
     * @param \Magento\Quote\Model\Quote\Address\RateRequest $request
     *
     * @return $this
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function setRequest(\Magento\Quote\Model\Quote\Address\RateRequest $request)
    {
        $this->_request = $request;
        $requestData = new \Magento\Framework\DataObject();
        $product = $this->_productFactory->create();
        $mpassignproductId = 0;
        $shippingdetail = [];
        $partner = 0;
        $handling = 0;
        foreach ($request->getAllItems() as $item) {
            $proid = $item->getProductId();
            $options = $item->getProductOptions();
            $mpassignproductId = 0;
            $itemOption = $this->_itemOptionModel->create()
                ->getCollection()
                ->addFieldToFilter('item_id', ['eq' => $item->getId()])
                ->addFieldToFilter('code', ['eq' => 'info_buyRequest']);

            if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
                continue;
            }

            $weight = $this->calculateWeightForProduct($item);
            if (empty($shippingdetail)) {
                array_push(
                    $shippingdetail,
                    [
                        'seller_id' => $partner,
                        'items_weight' => $weight,
                        'product_name' => $item->getName(),
                        'item_id' => $item->getId(),
                    ]
                );
            } else {
                $shipinfoflag = true;
                $index = 0;
                foreach ($shippingdetail as $itemship) {
                    if ($itemship['seller_id'] == $partner) {
                        $itemship['items_weight'] = $itemship['items_weight'] + $weight;
                        $itemship['product_name'] = $itemship['product_name'].','.$item->getName();
                        $itemship['item_id'] = $itemship['item_id'].','.$item->getId();
                        $shippingdetail[$index] = $itemship;
                        $shipinfoflag = false;
                    }
                    ++$index;
                }
                if ($shipinfoflag == true) {
                    array_push(
                        $shippingdetail,
                        [
                            'seller_id' => $partner,
                            'items_weight' => $weight,
                            'product_name' => $item->getName(),
                            'item_id' => $item->getId(),
                        ]
                    );
                }
            }
        }

        //$request->getDestCity();
        //$request->getDestRegionId();
        //$request->getDestPostcode();

        //$request->getSourceCity();



        if ($request->getShippingDetails()) {
            $shippingdetail = $request->getShippingDetails();
        }
        $requestData->setShippingDetails($shippingdetail);

        $requestData->setDestCountryId($request->getDestCountryId());

        if ($request->getDestPostcode()) {
            $requestData->setDestPostal(str_replace('-', '', $request->getDestPostcode()));
        }


        $this->setRawRequest($requestData);

        return $this;
    }

Calling my own function like this

if ($shipping->getSize() != 0) {
                $priceArr = $this->getDpriceArrForRate($shipping,$request);
            } else {
                $msg = $this->getErrorMsg($msg, $shipdetail);
            }

My getDpriceArrForRate function is like below

 public function getDpriceArrForRate($shipping,$request)
    {
         //$request->getDestCity();
        //$request->getDestRegionId();
        //$request->getDestPostcode();

        //$request->getSourceCity();


            print_r($request->getDestCity());
             exit;
       }

Its giving empty

0

You can get request city name from $request

For city use $request->getDestCity()

For getting region Id use $request->getDestRegionId()

For getting region use $request->getDestRegion()

  • Its giving empty value. – Dinesh Mar 20 at 13:45
  • I am getting City Name in setRequest function , but i need to use this value in another function. How can i pass this value as global value? – Dinesh Mar 20 at 13:59
  • You have to pass the $request to this function – Amit Bera Mar 20 at 14:02
  • I passed $request but not able to get the city name. I shared my code in my question. – Dinesh Mar 21 at 4:39
0

Finally I changed my code in my modal as follows.

         $requestData->setDestCountryId($request->getDestCountryId());
         $requestData->setDestCity($request->getDestCity());
         $requestData->setDestRegionId($request->getDestRegionId());
          $this->setRawRequest($requestData);

Now i can access the City , Region and CountryId by calling Raw Request As follows.

    $dest_city=$this->_rawRequest->getDestCity();
    $dest_regionid=$this->_rawRequest->getDestRegionId();
    $dest_postcode=$this->_rawRequest->getDestPostcode();
  • I Want to add buffer weight of currently selected shipping method. How can i do it? – Dinesh Apr 4 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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