Updated preorder package for customization

This commit is contained in:
Prashant Singh 2019-06-28 17:20:19 +05:30
parent baf104509b
commit b00374fa81
4 changed files with 78 additions and 87 deletions

View File

@ -66,67 +66,64 @@ class PreOrderController extends Controller
*/
public function complete()
{
if (request()->route('token'))
$preOrderItem = $this->preOrderItemRepository->findOneByField('token', request()->route('token'));
else
$preOrderItem = $this->preOrderItemRepository->find(request()->input('id'));
if (! $preOrderItem)
return abort(404);
$orderItem = $this->orderItemRepository->findOrFail($preOrderItem->order_item_id);
if (! $this->preOrderItemRepository->canBeComplete($orderItem)) {
session()->flash('error', trans('preorder::app.shop.preorders.complete-preorder-error'));
try {
if (request()->route('token'))
return redirct()->route('shop.home.index');
$preOrderItem = $this->preOrderItemRepository->findOneByField('token', request()->route('token'));
else
return back();
}
$preOrderItem = $this->preOrderItemRepository->find(request()->input('id'));
$data = [];
if (! $preOrderItem)
return abort(404);
if ($orderItem->type == 'configurable') {
$data = [
'pre_order_payment' => true,
'order_item_id' => $preOrderItem->order_item_id,
'product' => $orderItem->product_id,
'quantity' => $orderItem->qty_ordered,
'is_configurable' => true,
'selected_configurable_option' => $orderItem->child->product_id
];
$orderItem = $this->orderItemRepository->findOrFail($preOrderItem->order_item_id);
foreach ($this->productRepository->getSuperAttributes($orderItem->product) as $attribute) {
$data['super_attribute'][$attribute['id']] = $orderItem->child->product->{$attribute['code']};
if (! $this->preOrderItemRepository->canBeComplete($orderItem)) {
session()->flash('error', trans('preorder::app.shop.preorders.complete-preorder-error'));
if (request()->route('token'))
return redirct()->route('shop.home.index');
else
return back();
}
} else {
$data = [
'pre_order_payment' => true,
'order_item_id' => $preOrderItem->order_item_id,
'product' => $orderItem->product_id,
'quantity' => $orderItem->qty_ordered,
'is_configurable' => false,
];
}
request()->request->add($data);
$data = [];
$eventResult = Event::fire('checkout.cart.add.before', $data['product']);
if ($orderItem->type == 'configurable') {
$data = [
'pre_order_payment' => true,
'order_item_id' => $preOrderItem->order_item_id,
'product' => $orderItem->product_id,
'quantity' => $orderItem->qty_ordered,
'is_configurable' => true,
'selected_configurable_option' => $orderItem->child->product_id
];
$flag = true;
foreach ($eventResult as $res) {
if ($res != null) {
$flag = false;
foreach ($this->productRepository->getSuperAttributes($orderItem->product) as $attribute) {
$data['super_attribute'][$attribute['id']] = $orderItem->child->product->{$attribute['code']};
}
} else {
$data = [
'pre_order_payment' => true,
'order_item_id' => $preOrderItem->order_item_id,
'product' => $orderItem->product_id,
'quantity' => $orderItem->qty_ordered,
'is_configurable' => false,
];
}
}
if ($flag) {
request()->request->add($data);
Event::fire('checkout.cart.add.before', $data['product']);
$result = Cart::add($data['product'], $data);
Event::fire('checkout.cart.add.after', $result);
}
return redirect()->route('shop.checkout.onepage.index');
return redirect()->route('shop.checkout.onepage.index');
} catch(\Exception $e) {
session()->flash('error', trans($e->getMessage()));
return redirect()->back();
}
}
}

View File

@ -66,56 +66,50 @@ class Cart
{
$data = request()->all();
try {
if (! isset($data['pre_order_payment'])) {
if ($this->haveCompletePreorderProduct($productId))
throw new \Exception('Product can not be added with preorder payment.');
if (! isset($data['pre_order_payment'])) {
if ($this->haveCompletePreorderProduct($productId))
throw new \Exception('Product can not be added with preorder payment.');
$product = $this->productRepository->find($productId);
$product = $this->productRepository->find($productId);
if ($product->type == 'configurable') {
if (isset($data['selected_configurable_option'])) {
$product = $this->productRepository->find($data['selected_configurable_option']);
} else {
return;
}
}
if ($product->totalQuantity() > 0 || ! $product->allow_preorder)
if ($product->type == 'configurable') {
if (isset($data['selected_configurable_option'])) {
$product = $this->productRepository->find($data['selected_configurable_option']);
} else {
return;
}
}
if (! isset($data['quantity']))
$data['quantity'] = 1;
if ($product->totalQuantity() > 0 || ! $product->allow_preorder)
return;
if ($cart = CartFacade::getCart()) {
$cartItem = $cart->items()->where('product_id', $productId)->first();
if (! isset($data['quantity']))
$data['quantity'] = 1;
if ($cartItem) {
$quantity = $cartItem->quantity + $data['quantity'];
} else {
$quantity = $data['quantity'];
}
if ($cart = CartFacade::getCart()) {
$cartItem = $cart->items()->where('product_id', $productId)->first();
if ($cartItem) {
$quantity = $cartItem->quantity + $data['quantity'];
} else {
$quantity = $data['quantity'];
}
if ($product->preorder_qty && $product->preorder_qty < $quantity)
throw new \Exception('Requested quantity not available for preorder.');
} else {
if ($cart = CartFacade::getCart()) {
$cartItem = $cart->items()->where('product_id', $productId)->first();
$quantity = $data['quantity'];
}
if ($cartItem) {
throw new \Exception('Invalid quantity for complete preorder payment.');
} else {
throw new \Exception('Preorder payment can not be added with other product.');
}
if ($product->preorder_qty && $product->preorder_qty < $quantity)
throw new \Exception('Requested quantity not available for preorder.');
} else {
if ($cart = CartFacade::getCart()) {
$cartItem = $cart->items()->where('product_id', $productId)->first();
if ($cartItem) {
throw new \Exception('Invalid quantity for complete preorder payment.');
} else {
throw new \Exception('Preorder payment can not be added with other product.');
}
}
} catch (\Exception $e) {
session()->flash('error', trans($e->getMessage()));
return $e;
}
}

View File

@ -32,7 +32,7 @@ class Order
* Create a new Order event listener instance.
*
* @param Webkul\Product\Helpers\Price $priceHelper
* @param Webkul\SAASPreOrder\Repositories\PreOrderItemRepository $preOrderItemRepository
* @param Webkul\PreOrder\Repositories\PreOrderItemRepository $preOrderItemRepository
* @return void
*/
public function __construct(
@ -57,7 +57,7 @@ class Order
$preOrderItem = $this->preOrderItemRepository->findOneByField('order_item_id', $item->additional['order_item_id']);
$this->preOrderItemRepository->update([
'status' => 'completed',
'status' => 'processing',
'payment_order_item_id' => $item->id
], $preOrderItem->id);
} else {

View File

@ -16,7 +16,7 @@
@include ('shop::products.buy-now')
<button type="submit" class="btn btn-lg btn-primary pre-order-btn" style="width: 100%; display: none;background: #000 !important;">
<button type="submit" class="btn btn-lg btn-primary pre-order-btn" style="width: 100%; display: none;">
{{ __('preorder::app.shop.products.preorder') }}
</button>
@endif