PHP code example of deliverymatch / pdk

1. Go to this page and download the library: Download deliverymatch/pdk 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/ */

    

deliverymatch / pdk example snippets


class MyWebshopDataRepository implements \DeliveryMatch\Pdk\Common\Repository
{
    public function __construct(private readonly \PDO $db)
    {
    }

    public function setShipmentId(string $checkoutReference, int $shipmentId): void
    {
        $this->upsert($checkoutReference, ['shipment_id' => $shipmentId]);
    }

    public function getShipmentId(string $checkoutReference): ?int
    {
        $value = $this->column($checkoutReference, 'shipment_id');

        return $value === null ? null : (int) $value;
    }

    public function setShippingOptions(string $checkoutReference, array $shippingOptions): void
    {
        $this->upsert($checkoutReference, ['shipping_options' => base64_encode(serialize($shippingOptions))]);
    }

    public function getShippingOptions(string $checkoutReference): array
    {
        $value = $this->column($checkoutReference, 'shipping_options');

        return $value === null ? [] : unserialize(base64_decode($value));
    }

    public function setCheckId(string $checkoutReference, string $checkId): void
    {
        $this->upsert($checkoutReference, ['check_id' => $checkId]);
    }

    public function getCheckId(string $checkoutReference): ?string
    {
        return $this->column($checkoutReference, 'check_id');
    }

    public function flush(string $checkoutReference): void
    {
        $this->db->prepare('DELETE FROM dm_checkout WHERE checkout_reference = ?')->execute([$checkoutReference]);
    }

    private function column(string $checkoutReference, string $column): ?string
    {
        $stmt = $this->db->prepare("SELECT $column FROM dm_checkout WHERE checkout_reference = ?");
        $stmt->execute([$checkoutReference]);
        $value = $stmt->fetchColumn();

        return $value === false ? null : (string) $value;
    }

    private function upsert(string $checkoutReference, array $values): void
    {
        $values['updated_at'] = date('Y-m-d H:i:s');
        $columns = implode(', ', array_keys($values));
        $placeholders = implode(', ', array_fill(0, count($values), '?'));
        $updates = implode(', ', array_map(fn ($c) => "$c = VALUES($c)", array_keys($values)));

        $this->db
            ->prepare("INSERT INTO dm_checkout (checkout_reference, $columns) VALUES (?, $placeholders) ON DUPLICATE KEY UPDATE $updates")
            ->execute([$checkoutReference, ...array_values($values)]);
    }
}

class MyWebshopCheckoutContext implements \DeliveryMatch\Pdk\Common\CheckoutContext
{
    public function currentReference(): ?string
    {
        $cart = Storefront::currentCart();

        if ($cart === null || empty($cart->id)) {
            return null;
        }

        return (string) $cart->id;
    }
}

class MyWebshopPdkBootstrapper extends \DeliveryMatch\Pdk\Common\PdkBootstrapper
{
    protected function getAdditionalConfiguration(): array
    {
        return [
            \DeliveryMatch\Pdk\Common\Repository::class => \DI\autowire(MyWebshopDataRepository::class),
            \DeliveryMatch\Pdk\Common\CheckoutContext::class => \DI\autowire(MyWebshopCheckoutContext::class),
        ];
    }
}

$isAuthenticated = \DeliveryMatch\Pdk\Facade\Pdk::checkConnection();

$rates = \DeliveryMatch\Pdk\Facade\Pdk::fetchShippingOptions($request);

$options = \DeliveryMatch\Pdk\Facade\Pdk::getCachedOptions();

\DeliveryMatch\Pdk\Facade\Pdk::setSelectedOption($this->request->checkId);

$option = \DeliveryMatch\Pdk\Facade\Pdk::findShippingOption();

$cartId = (string) $order->cartId;

// Push the selected shipping option to DeliveryMatch
\DeliveryMatch\Pdk\Facade\Pdk::addShippingOptionToShipment($cartId);

// Store the DeliveryMatch shipment id with the E-commerce order
$order->dm_shipment_id = \DeliveryMatch\Pdk\Facade\Pdk::getShipmentId($cartId);
$order->save();

// The checkout is complete: remove its state from the repository
\DeliveryMatch\Pdk\Facade\Pdk::flushCheckout($cartId);

\DeliveryMatch\Pdk\Facade\Pdk::updateShipmentToNew(shipmentId: $order->dm_shipment_id, orderNumber: $order->number);