0

I don't want to use object manager, how can i change my code?
In the past, i have used method cart $cart but it is deprecated

     <?php

     namespace Custom\ShippingMethod\Model\Carrier;

 use Magento\Quote\Model\Quote\Address\RateRequest;
 use Magento\Shipping\Model\Carrier\AbstractCarrier;
 use Magento\Shipping\Model\Carrier\CarrierInterface;
 use Magento\Framework\App\Config\ScopeConfigInterface;
 use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
 use Psr\Log\LoggerInterface;
 use Magento\Shipping\Model\Rate\ResultFactory;
 use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
 use Magento\Framework\ObjectManagerInterface;


 class PlanetExpress extends AbstractCarrier implements CarrierInterface
{

protected $_code = 'planetexpress';

protected $rateResultFactory;

protected $rateMethodFactory;

protected $_objectManager;

public function __construct(
    ObjectManagerInterface $objectManager,
    ScopeConfigInterface $scopeConfig,
    ErrorFactory $rateErrorFactory,
    LoggerInterface $logger,
    ResultFactory $rateResultFactory,
    MethodFactory $rateMethodFactory,
    array $data = []
)
{
    $this->_objectManager = $objectManager;
    $this->rateResultFactory = $rateResultFactory;
    $this->rateMethodFactory = $rateMethodFactory;
    parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}

public function getAllowedMethods()
{
    return ['planetexpress' => $this->getConfigData('name')];
}

public function collectRates(RateRequest $request)
{
    if (!$this->getConfigFlag('active')) {
        return false;
    }

    /** @var \Magento\Shipping\Model\Rate\Result $result */
    $result = $this->rateResultFactory->create();

    /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
    $method = $this->rateMethodFactory->create();
    $method->setCarrier($this->_code);
    $method->setCarrierTitle($this->getConfigData('title'));

    $method->setMethod($this->_code);
    $method->setMethodTitle($this->getConfigData('name'));


    $cart = $this->_objectManager->get('\Magento\Checkout\Model\Cart');

    $uniqueItems = $cart->getQuote()->getItemsCount();
    $totalItems = $cart->getQuote()->getItemsQty();
    $subTotal = $cart->getQuote()->getGrandTotal();

    $amount = $this->getConfigData('price');

    $countryRate = 1;
    if ($request->getDestCountryId() == 'US'){
        $countryRate = 3;
    }elseif ($request->getDestCountryId() == 'UA'){
        $countryRate = 2;
    }elseif ($request->getDestCountryId() == 'CA'){
        $countryRate = 7;
    }

        $shippingPrice = ($subTotal * $uniqueItems * $amount) / $totalItems * $countryRate;

    $method->setPrice($shippingPrice);
    $method->setCost($amount);

    $result->append($method);

    return $result;
}

}

2 Answers 2

1

Try below code

<?php

namespace Custom\ShippingMethod\Model\Carrier;

use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
use Psr\Log\LoggerInterface;
use Magento\Shipping\Model\Rate\ResultFactory;
use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
use Magento\Checkout\Model\Cart;

class PlanetExpress extends AbstractCarrier implements CarrierInterface
{

    protected $_code = 'planetexpress';

    protected $rateResultFactory;

    protected $rateMethodFactory;

    protected $cartRepository;

    public function __construct(
        Cart $cart,
        ScopeConfigInterface $scopeConfig,
        ErrorFactory $rateErrorFactory,
        LoggerInterface $logger,
        ResultFactory $rateResultFactory,
        MethodFactory $rateMethodFactory,
        array $data = []
    )
    {
        $this->cart = $cart;
        $this->rateResultFactory = $rateResultFactory;
        $this->rateMethodFactory = $rateMethodFactory;
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
    }

    public function getAllowedMethods()
    {
        return ['planetexpress' => $this->getConfigData('name')];
    }

    public function collectRates(RateRequest $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }

        /** @var \Magento\Shipping\Model\Rate\Result $result */
        $result = $this->rateResultFactory->create();

        /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
        $method = $this->rateMethodFactory->create();
        $method->setCarrier($this->_code);
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod($this->_code);
        $method->setMethodTitle($this->getConfigData('name'));

        $uniqueItems = $this->cart->getQuote()->getItemsCount();
        $totalItems = $this->cart->getQuote()->getItemsQty();
        $subTotal = $this->cart->getQuote()->getGrandTotal();

        $amount = $this->getConfigData('price');

        $countryRate = 1;
        if ($request->getDestCountryId() == 'US'){
            $countryRate = 3;
        }elseif ($request->getDestCountryId() == 'UA'){
            $countryRate = 2;
        }elseif ($request->getDestCountryId() == 'CA'){
            $countryRate = 7;
        }

            $shippingPrice = ($subTotal * $uniqueItems * $amount) / $totalItems * $countryRate;

        $method->setPrice($shippingPrice);
        $method->setCost($amount);

        $result->append($method);

        return $result;
    }
}
1
  • Yes it's good but ''use Magento\Checkout\Model\Cart;'' this is deprecated method. Mb have better idea?
    – Miron
    Commented Nov 11, 2022 at 11:16
0

i try to use helper and this code good for me.

i take quote by checkoutHelper

<?php

namespace Custom\ShippingMethod\Model\Carrier;

use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
use Psr\Log\LoggerInterface;
use Magento\Shipping\Model\Rate\ResultFactory;
use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
use Magento\Framework\ObjectManagerInterface;


class PlanetExpress extends AbstractCarrier implements CarrierInterface
{

protected $_code = 'planetexpress';

protected $rateResultFactory;

protected $rateMethodFactory;

protected $_objectManager;

protected $checkoutHelper;

public function __construct(
    \Magento\Checkout\Helper\Data $checkoutHelper,
    ObjectManagerInterface $objectManager,
    ScopeConfigInterface $scopeConfig,
    ErrorFactory $rateErrorFactory,
    LoggerInterface $logger,
    ResultFactory $rateResultFactory,
    MethodFactory $rateMethodFactory,
    array $data = []
)
{
    $this->_objectManager = $objectManager;
    $this->checkoutHelper = $checkoutHelper;
    $this->rateResultFactory = $rateResultFactory;
    $this->rateMethodFactory = $rateMethodFactory;
    parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}

public function getAllowedMethods()
{
    return ['planetexpress' => $this->getConfigData('name')];
}

public function collectRates(RateRequest $request)
{
    if (!$this->getConfigFlag('active')) {
        return false;
    }

    /** @var \Magento\Shipping\Model\Rate\Result $result */
    $result = $this->rateResultFactory->create();

    /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
    $method = $this->rateMethodFactory->create();
    $method->setCarrier($this->_code);
    $method->setCarrierTitle($this->getConfigData('title'));

    $method->setMethod($this->_code);
    $method->setMethodTitle($this->getConfigData('name'));


    $checkoutHelper = $this->checkoutHelper->getQuote();

    $uniqueItems = $checkoutHelper->getItemsCount();
    $totalItems = $checkoutHelper->getItemsQty();
    $subTotal = $checkoutHelper->getSubtotal();

    $amount = $this->getConfigData('price');

    $countryRate = 1;
    if ($request->getDestCountryId() == 'US'){
        $countryRate = 3;
    }elseif ($request->getDestCountryId() == 'UA'){
        $countryRate = 2;
    }elseif ($request->getDestCountryId() == 'CA'){
        $countryRate = 7;
    }

        $shippingPrice = ($subTotal * $uniqueItems * $amount) / $totalItems * $countryRate;

    $method->setPrice($shippingPrice);
    $method->setCost($amount);

    $result->append($method);

    return $result;
}

}

Your Answer

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

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