Skip to content

Instantly share code, notes, and snippets.

@mckenziearts
Created March 23, 2024 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mckenziearts/092b382427cb3b80d2bc91012a691d9e to your computer and use it in GitHub Desktop.
Save mckenziearts/092b382427cb3b80d2bc91012a691d9e to your computer and use it in GitHub Desktop.
A simple example to create Order in Laravel Shopper using Livewire
<?php
namespace App\Http\Livewire\Checkout;
use App\Actions\PlaceOrder as PlaceOrderAction;
use App\Notifications\PlaceOrder;
use Darryldecode\Cart\Facades\CartFacade;
use Livewire\Component;
use Shopper\Framework\Models\Shop\Order\Order;
use Shopper\Framework\Models\Shop\Order\OrderItem;
class Browse extends Component
{
/**
* Component event listeners.
*
* @var string[]
*/
protected $listeners = ['shippingMethodUpdated', 'goToStep', 'mergeState'];
/**
* Complete checkout process step.
*
* @var int
*/
public $step = 1;
/**
* Place order state.
*
* @var array
*/
public $state = [];
/**
* Shipping price.
*
* @var int|null
*/
public $shippingPrice;
/**
* Shipping method used.
*
* @var string
*/
public $shippingMethod;
/**
* Shipping delivery time.
*
* @var string
*/
public $shippingDelivery;
/**
* Total order price.
*
* @var int|null
*/
public $totalPrice;
/**
* Determine if order have a shipping method.
*
* @var bool
*/
public $hasShippingMethod = false;
/**
* Validation rules.
*
* @var string[]
*/
public $rules = [
'state.shipping_address_id' => 'required',
'state.shipping_method_id' => 'required',
'state.payment_method_id' => 'required',
];
/**
* Custom validation errors messages.
*
* @var string[]
*/
public $messages = [
'state.shipping_address_id.required' => "L'adresse de livraison n'a pas été validé, veuillez cliquez de nouveau.",
'state.shipping_method_id.required' => "Le moyen d livraison n'a pas été choisi.",
'state.payment_method_id.required' => 'Vous devez choisi un moyen de paiement.',
];
/**
* Component Mount instance.
*
* @return void
* @throws \Exception
*/
public function mount()
{
$this->totalPrice = CartFacade::session(session()->getId())->getTotal();
}
/**
* Store a new order to storage.
*
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
public function store()
{
$this->validate();
$order = Order::query()->create([
'number' => generate_number(),
'price_amount' => CartFacade::session(session()->getId())->getSubTotal(),
'currency' => shopper_currency(),
'shipping_total' => $this->state['price'],
'shipping_method' => $this->state['method'],
'shipping_address_id' => $this->state['shipping_address_id'],
'payment_method_id' => $this->state['payment_method_id'],
'user_id' => auth()->id(),
]);
foreach (CartFacade::session(session()->getId())->getContent() as $cart) {
OrderItem::query()->create([
'name' => $cart->name,
'sku' => $cart->associatedModel->sku,
'quantity' => $cart->quantity,
'unit_price_amount' => $cart->price,
'order_id' => $order->id,
'product_id' => $cart->associatedModel->id,
'product_type' => config('shopper.system.models.product'),
]);
}
auth()->user()->notify(new PlaceOrder($order));
(new PlaceOrderAction($order));
CartFacade::session(session()->getId())->clear();
session()->flash('success', __('Votre commande a été passée et sera traitée dans les plus brefs délais. Assurez-vous de noter votre numéro de commande, qui est :number. Vous recevrez sous peu un e-mail avec la confirmation de votre commande.', ['number' => $order->number]));
$this->redirectRoute('account.orders');
}
/**
* Go to the current step passed as parameter.
*
* @param $step
* @return void
*/
public function goToStep($step)
{
$this->step = $step;
}
/**
* Merge state of all step.
*
* @param array $state
* @return void
*/
public function mergeState(array $state)
{
$this->state = array_merge($this->state, $state);
}
/**
* Updated shipping method and total order price.
*
* @param mixed $params
* @throws \Exception
*/
public function shippingMethodUpdated($params)
{
$this->hasShippingMethod = true;
$this->shippingPrice = $params['price'];
$this->shippingMethod = $params['method'];
$this->shippingDelivery = $params['delivery_time'];
$this->totalPrice = (int) CartFacade::session(session()->getId())->getTotal() + $params['price'];
}
/**
* Render the component.
*
* @return \Illuminate\View\View
* @throws \Exception
*/
public function render()
{
return view('livewire.checkout.browse', [
'items' => CartFacade::session(session()->getId())->getContent(),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment