PHP code example of setono / sylius-pickup-point-plugin

1. Go to this page and download the library: Download setono/sylius-pickup-point-plugin 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/ */

    

setono / sylius-pickup-point-plugin example snippets



# config/bundles.php

return [
    // ...
    Setono\SyliusPickupPointPlugin\SetonoSyliusPickupPointPlugin::class => ['all' => true],
    // ...
];


// src/Entity/Shipment.php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Setono\SyliusPickupPointPlugin\Model\PickupPointAwareTrait;
use Setono\SyliusPickupPointPlugin\Model\ShipmentInterface;
use Sylius\Component\Core\Model\Shipment as BaseShipment;

#[ORM\Entity]
#[ORM\Table(name: 'sylius_shipment')]
class Shipment extends BaseShipment implements ShipmentInterface
{
    use PickupPointAwareTrait;
}


// src/Entity/ShippingMethod.php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Setono\SyliusPickupPointPlugin\Model\PickupPointProviderAwareTrait;
use Setono\SyliusPickupPointPlugin\Model\ShippingMethodInterface;
use Sylius\Component\Core\Model\ShippingMethod as BaseShippingMethod;

#[ORM\Entity]
#[ORM\Table(name: 'sylius_shipping_method')]
class ShippingMethod extends BaseShippingMethod implements ShippingMethodInterface
{
    use PickupPointProviderAwareTrait;
}



declare(strict_types=1);

namespace App\PickupPoint;

use Setono\SyliusPickupPointPlugin\Attribute\AsProvider;
use Setono\SyliusPickupPointPlugin\DTO\Address;
use Setono\SyliusPickupPointPlugin\DTO\PickupPoint;
use Setono\SyliusPickupPointPlugin\Provider\Provider;

#[AsProvider(code: 'acme', name: 'ACME')]
final class AcmeProvider extends Provider
{
    public function __construct(
        private readonly AcmeClient $client, // your carrier's API client
    ) {
    }

    /**
     * @return list<PickupPoint>
     */
    public function findPickupPoints(Address $address): array
    {
        // Every Address field is nullable (the cart may not have a full address yet) — bail when a needed one is missing.
        if (null === $address->postalCode || null === $address->countryCode) {
            return [];
        }

        $points = [];
        foreach ($this->client->search($address->postalCode, $address->countryCode) as $shop) {
            $points[] = $this->transform($shop);
        }

        // Return them ordered by distance from the address: the first one is auto-selected at checkout.
        return $points;
    }

    public function findPickupPoint(string $id, array $metadata = []): ?PickupPoint
    {
        // Called when re-resolving a single point by id; $metadata carries the context you stored (see below).
        $shop = $this->client->get($id, $metadata['country'] ?? null);

        return null === $shop ? null : $this->transform($shop);
    }

    private function transform(object $shop): PickupPoint
    {
        $point = new PickupPoint();
        $point->provider = $this->getCode(); // always stamp the code the provider is registered under
        $point->id = (string) $shop->id;     // unique within this provider
        $point->name = $shop->name;
        $point->address = $shop->street;
        $point->zipCode = $shop->zip;
        $point->city = $shop->city;
        $point->country = $shop->countryCode;
        $point->latitude = (string) $shop->lat;
        $point->longitude = (string) $shop->lng;

        return $point;
    }
}