Extending
Actions
Replace and customize actions in Unleash Commerce Core
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Replace and customize actions in Unleash Commerce Core
<?php
namespace App\Providers;
use App\Actions\CreateOrderAction;
use Esign\UnleashCommerce\Core\Contracts\Actions\Orders\CreateOrderAction as CreateOrderActionContract;
use Illuminate\Support\ServiceProvider;
class UnleashCommerceServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->registerActions();
}
protected function registerActions(): void
{
$this->app->bind(
CreateOrderActionContract::class,
CreateOrderAction::class
);
}
}
namespace App\Actions;
use Esign\UnleashCommerce\Core\Contracts\Actions\Orders\CreateOrderAction as CreateOrderActionContract;
use Esign\UnleashCommerce\Core\Models\Order;
class CreateOrderAction implements CreateOrderActionContract
{
public function execute(array $data): Order
{
// Your custom implementation
$order = Order::create($data);
// Add custom behavior
$this->notifyWarehouse($order);
return $order;
}
private function notifyWarehouse(Order $order): void
{
// Custom logic
}
}
namespace App\Http\Controllers;
use Esign\UnleashCommerce\Core\Contracts\Actions\Orders\CreateOrderAction;
class OrderController extends Controller
{
public function store(CreateOrderAction $createOrder)
{
$order = $createOrder->execute($this->validated());
return response()->json($order);
}
}
public function test_creates_order_and_notifies_warehouse()
{
// Arrange
$data = [
'customer_id' => Customer::factory()->create()->getKey(),
'total' => 100.00,
];
// Act
$action = $this->app->make(CreateOrderAction::class);
$order = $action->execute($data);
// Assert
$this->assertDatabaseHas(Order::class, [
'customer_id' => $data['customer_id'],
'total' => $data['total'],
]);
}