PHP code example of mahoudeau / universal-shipping-for-sylius

1. Go to this page and download the library: Download mahoudeau/universal-shipping-for-sylius library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mahoudeau / universal-shipping-for-sylius example snippets


Mahoudeau\UniversalShipping\UniversalShippingPlugin::class => ['all' => true],

// src/Entity/Shipping/Shipment.php
use Doctrine\ORM\Mapping as ORM;
use Mahoudeau\UniversalShipping\Model\ParcelAwareInterface;
use Mahoudeau\UniversalShipping\Model\ParcelAwareTrait;
use Mahoudeau\UniversalShipping\Model\PickupPointAwareInterface;
use Mahoudeau\UniversalShipping\Model\PickupPointAwareTrait;

#[ORM\Entity]
#[ORM\Table(name: 'sylius_shipment')]
#[ORM\Index(columns: ['universal_shipping_parcel_id'], name: 'IDX_universal_shipping_parcel_id')]
class Shipment extends BaseShipment implements PickupPointAwareInterface, ParcelAwareInterface
{
    use ParcelAwareTrait;
    use PickupPointAwareTrait;
    // ...
}

// src/Entity/Shipping/ShippingMethod.php
use Mahoudeau\UniversalShipping\Model\DeliveryOptionAwareInterface;
use Mahoudeau\UniversalShipping\Model\DeliveryOptionAwareTrait;

class ShippingMethod extends BaseShippingMethod implements DeliveryOptionAwareInterface
{
    use DeliveryOptionAwareTrait;
    // ...
}

use Mahoudeau\UniversalShipping\Label\RefundedAmountProviderInterface;
use Sylius\Component\Core\Model\OrderItemUnitInterface;
use Sylius\RefundPlugin\Model\RefundType;
use Sylius\RefundPlugin\Provider\RemainingTotalProviderInterface;
use Symfony\Component\DependencyInjection\Attribute\AsAlias;

#[AsAlias(RefundedAmountProviderInterface::class)]
final readonly class RefundedAmounts implements RefundedAmountProviderInterface
{
    public function __construct(private RemainingTotalProviderInterface $remaining)
    {
    }

    public function refundedAmount(OrderItemUnitInterface $unit): int
    {
        return $unit->getTotal() - $this->remaining->getTotalLeftToRefund((int) $unit->getId(), RefundType::orderItemUnit());
    }
}

use Mahoudeau\UniversalShipping\DeliveryOption\DeliveryOption;
use Mahoudeau\UniversalShipping\Model\PickupPoint;
use Mahoudeau\UniversalShipping\Provider\AsPickupPointProvider;
use Mahoudeau\UniversalShipping\Provider\PickupPointProviderInterface;
use Mahoudeau\UniversalShipping\Provider\PickupPointQuery;

#[AsPickupPointProvider('my_carrier')]
final class MyCarrierProvider implements PickupPointProviderInterface
{
    public function search(PickupPointQuery $query, DeliveryOption $option): array { /* ... */ }

    public function find(string $id, DeliveryOption $option): ?PickupPoint { /* ... */ }
}