Search code examples
calculatorsylius

Custom shipping calculator which takes destination into account


I am trying to create a custom calculator which calculates shipping costs based of a deleivery address. For now, I will be hardcoding different fees according to a postcode prefix... e.g.

  • SK1 = 4
  • SK2 = 4
  • SK3 = 4
  • SK4 = 4
  • M1 = 6
  • M2 = 6
  • M3 = 6
  • M4 = 6
  • Everything else = 10

I am following the tutorial here.

The code stub I have is as follows:

<?php
/**
 * Created by IntelliJ IDEA.
 * User: camerona
 * Date: 03/03/2017
 * Time: 08:09
 */

namespace AppBundle\Shipping;

use Sylius\Component\Shipping\Calculator\CalculatorInterface;
use Sylius\Component\Shipping\Model\ShippingSubjectInterface;

class PostCodeCalculator implements CalculatorInterface
{
    public function calculate(ShippingSubjectInterface $subject, array $configuration)
    {
        return $this->postCodeService->getShippingCostForPostCode($subject->getShippingAddress());
    }

    public function getType()
    {
        // TODO: Implement getType() method.
    }
}

Is there a way in sylius where I can get access to the shipping address of an order? The ShippingSubjectInterface only allows access to volume, weight, items, and shippables.


Solution

  • /** @var $subject Shipment  */
    $postCode = $subject->getOrder()->getShippingAddress()->getPostCode();
    

    Allowed me get address from subject.