PHP code example of tourze / order-core-bundle

1. Go to this page and download the library: Download tourze/order-core-bundle 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/ */

    

tourze / order-core-bundle example snippets


return [
    // ...
    OrderCoreBundle\OrderCoreBundle::class => ['all' => true],
];

use OrderCoreBundle\Entity\Contract;

$order = new Contract();
$order->setState(OrderState::INIT);
$order->setUser($user);
$order->setSn('C1234567890');

use OrderCoreBundle\Entity\OrderProduct;

$product = new OrderProduct();
$product->setContract($order);
$product->setSku($sku);
$product->setQuantity(2);
$product->setPrice(99.99);

use OrderCoreBundle\Entity\OrderPrice;

$price = new OrderPrice();
$price->setContract($order);
$price->setAmount(199.98);
$price->setType('total');

use OrderCoreBundle\Service\OrderService;
use OrderCoreBundle\DTO\ProductCheckoutItem;

class OrderController
{
    public function __construct(
        private OrderService $orderService,
    ) {}

    public function createOrder(): Contract
    {
        $items = [
            new ProductCheckoutItem($sku1, 2),
            new ProductCheckoutItem($sku2, 1),
        ];

        return $this->orderService->createOrder($user, $items, $contactInfo);
    }
}

use OrderCoreBundle\Event\OrderPaidEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class PaymentController
{
    public function __construct(
        private EventDispatcherInterface $eventDispatcher,
    ) {}

    public function handlePaymentSuccess(Contract $order): void
    {
        $order->setState(OrderState::PAID);

        $event = new OrderPaidEvent();
        $event->setContract($order);
        $this->eventDispatcher->dispatch($event);
    }
}

use OrderCoreBundle\Event\OrderPaidEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

class StockManager
{
    #[AsEventListener(event: OrderPaidEvent::class)]
    public function onOrderPaid(OrderPaidEvent $event): void
    {
        $order = $event->getContract();

        foreach ($order->getProducts() as $product) {
            $this->stockService->reserveStock(
                $product->getSku(),
                $product->getQuantity()
            );
        }
    }
}
bash
php bin/console order:expire-no-received