0

I created my own shipping method but I want the price to be set as 10% of the subTotal.

I think I should use something of the session or the quote cart inside the collectRates function of Magento but I can't seem to figure it out.

Can you help me out?

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('percentagetotaal');
    $method->setCarrierTitle($this->getConfigData('title'));

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

    $percentage = 10;
    // I want to get the session and get the cart subtotal and calculate 10% of the $subTotal.
    // How to get $subTotal;
    $price = ($subTotal * $percentage) / 100; //calculate 10% of the product cost



    $method->setPrice($price);
    $method->setCost($price);

    $result->append($method);

    return $result;
}

So I want to know how to get $subTotal inside the collectRates function.

2

In this you can get Quote id from $request object.

Lets do some trick in this case:

First inject Quote Factory class \Magento\Quote\Model\QuoteFactory at __construct()

Then From get load quote factory by quote id which you will get from $items = $request->getAllItems();

then easily you can get subtotal


protected $quoteFactory;
 public function __construct(
        \Magento\Quote\Model\QuoteFactory $quoteFactory
    ) {
     $this->quoteFactory = $quoteFactory;
   }
public function collectRates(RateRequest $request)
{
    if (!$this->getConfigFlag('active')) {
        return false;
    }

    $items = $request->getAllItems();
    $quoteId = false;

     foreach ($items  as $item) 
     {
         $quoteId = $item->getQuoteId();
         break;
     }

     $quote = $this->getCurrentQuote($quoteId);

    /** @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('percentagetotaal');
    $method->setCarrierTitle($this->getConfigData('title'));

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

    $percentage = 10;
    // I want to get the session and get the cart subtotal and calculate 10% of the $subTotal.
    // How to get $subTotal;
     $totals = $quote->getTotals();
    $price = ( $totals['subtotal']->getValue() * $percentage) / 100; //calculate 10% of the product cost



    $method->setPrice($price);
    $method->setCost($price);

    $result->append($method);

    return $result;
}

 protected function getCurrentQuote($quoteId){

 return $this->quoteFactory->create()->load($quoteId);
 }
0

If you want to get subtotal, you should use $request->getPackageValueWithDiscount().

Your code then would be:

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();

    $subTotal = $request->getBaseCurrency()->convert($request->getPackageValueWithDiscount(), $request->getPackageCurrency());
    $subTotal = $request->getPackageValueWithDiscount();
    // Additional line if some is dealing with free shipping (not this case).
    /* $this->_updateFreeMethodQuote($request); */

    $method->setCarrier('percentagetotaal');
    $method->setCarrierTitle($this->getConfigData('title'));

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

    $percentage = 10;
    $price = ($subTotal * $percentage) / 100; //calculate 10% of total cost

    $method->setPrice($price);
    $method->setCost($price);

    $result->append($method);

    return $result;
}

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.