Improved checkout package code style

This commit is contained in:
Jitendra Singh 2020-02-19 17:44:07 +05:30
parent 98b373d0cb
commit 973d8ebc60
3 changed files with 141 additions and 118 deletions

View File

@ -176,8 +176,9 @@ class Cart {
} }
} }
if (! $parentCartItem) if (! $parentCartItem) {
$parentCartItem = $cartItem; $parentCartItem = $cartItem;
}
} }
} }
@ -197,12 +198,12 @@ class Cart {
public function create($data) public function create($data)
{ {
$cartData = [ $cartData = [
'channel_id' => core()->getCurrentChannel()->id, 'channel_id' => core()->getCurrentChannel()->id,
'global_currency_code' => core()->getBaseCurrencyCode(), 'global_currency_code' => core()->getBaseCurrencyCode(),
'base_currency_code' => core()->getBaseCurrencyCode(), 'base_currency_code' => core()->getBaseCurrencyCode(),
'channel_currency_code' => core()->getChannelBaseCurrencyCode(), 'channel_currency_code' => core()->getChannelBaseCurrencyCode(),
'cart_currency_code' => core()->getCurrentCurrencyCode(), 'cart_currency_code' => core()->getCurrentCurrencyCode(),
'items_count' => 1 'items_count' => 1
]; ];
//Authentication details //Authentication details
@ -241,8 +242,9 @@ class Cart {
foreach ($data['qty'] as $itemId => $quantity) { foreach ($data['qty'] as $itemId => $quantity) {
$item = $this->cartItemRepository->findOneByField('id', $itemId); $item = $this->cartItemRepository->findOneByField('id', $itemId);
if (! $item) if (! $item) {
continue; continue;
}
if ($quantity <= 0) { if ($quantity <= 0) {
$this->removeItem($itemId); $this->removeItem($itemId);
@ -252,16 +254,17 @@ class Cart {
$item->quantity = $quantity; $item->quantity = $quantity;
if (! $this->isItemHaveQuantity($item)) if (! $this->isItemHaveQuantity($item)) {
throw new \Exception(trans('shop::app.checkout.cart.quantity.inventory_warning')); throw new \Exception(trans('shop::app.checkout.cart.quantity.inventory_warning'));
}
Event::dispatch('checkout.cart.update.before', $item); Event::dispatch('checkout.cart.update.before', $item);
$this->cartItemRepository->update([ $this->cartItemRepository->update([
'quantity' => $quantity, 'quantity' => $quantity,
'total' => core()->convertPrice($item->price * $quantity), 'total' => core()->convertPrice($item->price * $quantity),
'base_total' => $item->price * $quantity, 'base_total' => $item->price * $quantity,
'total_weight' => $item->weight * $quantity, 'total_weight' => $item->weight * $quantity,
'base_total_weight' => $item->weight * $quantity 'base_total_weight' => $item->weight * $quantity
], $itemId); ], $itemId);
@ -286,8 +289,9 @@ class Cart {
foreach ($items as $item) { foreach ($items as $item) {
if ($item->product->getTypeInstance()->compareOptions($item->additional, $data['additional'])) { if ($item->product->getTypeInstance()->compareOptions($item->additional, $data['additional'])) {
if (isset($data['additional']['parent_id'])) { if (isset($data['additional']['parent_id'])) {
if ($item->parent->product->getTypeInstance()->compareOptions($item->parent->additional, request()->all())) if ($item->parent->product->getTypeInstance()->compareOptions($item->parent->additional, request()->all())) {
return $item; return $item;
}
} else { } else {
return $item; return $item;
} }
@ -305,12 +309,12 @@ class Cart {
{ {
Event::dispatch('checkout.cart.delete.before', $itemId); Event::dispatch('checkout.cart.delete.before', $itemId);
if (! $cart = $this->getCart()) if (! $cart = $this->getCart()) {
return false; return false;
}
$this->cartItemRepository->delete($itemId); $this->cartItemRepository->delete($itemId);
//delete the cart instance if no items are there
if ($cart->items()->get()->count() == 0) { if ($cart->items()->get()->count() == 0) {
$this->cartRepository->delete($cart->id); $this->cartRepository->delete($cart->id);
@ -338,14 +342,13 @@ class Cart {
$guestCart = session()->get('cart'); $guestCart = session()->get('cart');
//when the logged in customer is not having any of the cart instance previously and are active.
if (! $cart) { if (! $cart) {
$this->cartRepository->update([ $this->cartRepository->update([
'customer_id' => $this->getCurrentCustomer()->user()->id, 'customer_id' => $this->getCurrentCustomer()->user()->id,
'is_guest' => 0, 'is_guest' => 0,
'customer_first_name' => $this->getCurrentCustomer()->user()->first_name, 'customer_first_name' => $this->getCurrentCustomer()->user()->first_name,
'customer_last_name' => $this->getCurrentCustomer()->user()->last_name, 'customer_last_name' => $this->getCurrentCustomer()->user()->last_name,
'customer_email' => $this->getCurrentCustomer()->user()->email 'customer_email' => $this->getCurrentCustomer()->user()->email
], $guestCart->id); ], $guestCart->id);
session()->forget('cart'); session()->forget('cart');
@ -358,8 +361,9 @@ class Cart {
$found = false; $found = false;
foreach ($cart->items as $cartItem) { foreach ($cart->items as $cartItem) {
if (! $cartItem->product->getTypeInstance()->compareOptions($cartItem->additional, $guestCartItem->additional)) if (! $cartItem->product->getTypeInstance()->compareOptions($cartItem->additional, $guestCartItem->additional)) {
continue; continue;
}
$cartItem->quantity = $newQuantity = $cartItem->quantity + $guestCartItem->quantity; $cartItem->quantity = $newQuantity = $cartItem->quantity + $guestCartItem->quantity;
@ -370,10 +374,10 @@ class Cart {
} }
$this->cartItemRepository->update([ $this->cartItemRepository->update([
'quantity' => $newQuantity, 'quantity' => $newQuantity,
'total' => core()->convertPrice($cartItem->price * $newQuantity), 'total' => core()->convertPrice($cartItem->price * $newQuantity),
'base_total' => $cartItem->price * $newQuantity, 'base_total' => $cartItem->price * $newQuantity,
'total_weight' => $cartItem->weight * $newQuantity, 'total_weight' => $cartItem->weight * $newQuantity,
'base_total_weight' => $cartItem->weight * $newQuantity 'base_total_weight' => $cartItem->weight * $newQuantity
], $cartItem->id); ], $cartItem->id);
@ -432,7 +436,7 @@ class Cart {
if ($this->getCurrentCustomer()->check()) { if ($this->getCurrentCustomer()->check()) {
$cart = $this->cartRepository->findOneWhere([ $cart = $this->cartRepository->findOneWhere([
'customer_id' => $this->getCurrentCustomer()->user()->id, 'customer_id' => $this->getCurrentCustomer()->user()->id,
'is_active' => 1 'is_active' => 1
]); ]);
} elseif (session()->has('cart')) { } elseif (session()->has('cart')) {
$cart = $this->cartRepository->find(session()->get('cart')->id); $cart = $this->cartRepository->find(session()->get('cart')->id);
@ -475,8 +479,9 @@ class Cart {
*/ */
public function saveCustomerAddress($data) public function saveCustomerAddress($data)
{ {
if (! $cart = $this->getCart()) if (! $cart = $this->getCart()) {
return false; return false;
}
$billingAddress = $data['billing']; $billingAddress = $data['billing'];
$billingAddress['cart_id'] = $cart->id; $billingAddress['cart_id'] = $cart->id;
@ -497,6 +502,7 @@ class Cart {
if (isset($data['billing']['save_as_address']) && $data['billing']['save_as_address']) { if (isset($data['billing']['save_as_address']) && $data['billing']['save_as_address']) {
$billingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id; $billingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id;
$this->customerAddressRepository->create($billingAddress); $this->customerAddressRepository->create($billingAddress);
} }
@ -505,7 +511,7 @@ class Cart {
$shippingAddress['cart_id'] = $cart->id; $shippingAddress['cart_id'] = $cart->id;
if (isset($data['shipping']['address_id']) && $data['shipping']['address_id']) { if (isset($data['shipping']['address_id']) && $data['shipping']['address_id']) {
$address = $this->customerAddressRepository->findOneWhere(['id'=> $data['shipping']['address_id']])->toArray(); $address = $this->customerAddressRepository->findOneWhere(['id' => $data['shipping']['address_id']])->toArray();
$shippingAddress['first_name'] = $this->getCurrentCustomer()->user()->first_name; $shippingAddress['first_name'] = $this->getCurrentCustomer()->user()->first_name;
$shippingAddress['last_name'] = $this->getCurrentCustomer()->user()->last_name; $shippingAddress['last_name'] = $this->getCurrentCustomer()->user()->last_name;
@ -578,8 +584,9 @@ class Cart {
*/ */
public function saveShippingMethod($shippingMethodCode) public function saveShippingMethod($shippingMethodCode)
{ {
if (! $cart = $this->getCart()) if (! $cart = $this->getCart()) {
return false; return false;
}
$cart->shipping_method = $shippingMethodCode; $cart->shipping_method = $shippingMethodCode;
$cart->save(); $cart->save();
@ -595,8 +602,9 @@ class Cart {
*/ */
public function savePaymentMethod($payment) public function savePaymentMethod($payment)
{ {
if (! $cart = $this->getCart()) if (! $cart = $this->getCart()) {
return false; return false;
}
if ($cartPayment = $cart->payment) if ($cartPayment = $cart->payment)
$cartPayment->delete(); $cartPayment->delete();
@ -619,11 +627,13 @@ class Cart {
{ {
$validated = $this->validateItems(); $validated = $this->validateItems();
if (! $validated) if (! $validated) {
return false; return false;
}
if (! $cart = $this->getCart()) if (! $cart = $this->getCart()) {
return false; return false;
}
Event::dispatch('checkout.cart.collect.totals.before', $cart); Event::dispatch('checkout.cart.collect.totals.before', $cart);
@ -672,17 +682,16 @@ class Cart {
} }
/** /**
* To validate if the product information is changed by admin and the items have * To validate if the product information is changed by admin and the items have been added to the cart before it.
* been added to the cart before it.
* *
* @return boolean * @return boolean
*/ */
public function validateItems() public function validateItems()
{ {
if (! $cart = $this->getCart()) if (! $cart = $this->getCart()) {
return; return;
}
//rare case of accident-->used when there are no items.
if (count($cart->items) == 0) { if (count($cart->items) == 0) {
$this->cartRepository->delete($cart->id); $this->cartRepository->delete($cart->id);
@ -694,9 +703,9 @@ class Cart {
$price = ! is_null($item->custom_price) ? $item->custom_price : $item->base_price; $price = ! is_null($item->custom_price) ? $item->custom_price : $item->base_price;
$this->cartItemRepository->update([ $this->cartItemRepository->update([
'price' => core()->convertPrice($price), 'price' => core()->convertPrice($price),
'base_price' => $price, 'base_price' => $price,
'total' => core()->convertPrice($price * $item->quantity), 'total' => core()->convertPrice($price * $item->quantity),
'base_total' => $price * $item->quantity, 'base_total' => $price * $item->quantity,
], $item->id); ], $item->id);
} }
@ -712,17 +721,20 @@ class Cart {
*/ */
public function calculateItemsTax() public function calculateItemsTax()
{ {
if (! $cart = $this->getCart()) if (! $cart = $this->getCart()) {
return false; return false;
}
if (! $cart->shipping_address && ! $cart->billing_address) if (! $cart->shipping_address && ! $cart->billing_address) {
return; return;
}
foreach ($cart->items()->get() as $item) { foreach ($cart->items()->get() as $item) {
$taxCategory = $this->taxCategoryRepository->find($item->product->tax_category_id); $taxCategory = $this->taxCategoryRepository->find($item->product->tax_category_id);
if (! $taxCategory) if (! $taxCategory) {
continue; continue;
}
if ($item->product->getTypeInstance()->isStockable()) { if ($item->product->getTypeInstance()->isStockable()) {
$address = $cart->shipping_address; $address = $cart->shipping_address;
@ -758,6 +770,7 @@ class Cart {
$item->base_tax_amount = ($item->base_total * $rate->tax_rate) / 100; $item->base_tax_amount = ($item->base_total * $rate->tax_rate) / 100;
$item->save(); $item->save();
break; break;
} else { } else {
$this->setItemTaxToZero($item); $this->setItemTaxToZero($item);
@ -791,11 +804,13 @@ class Cart {
*/ */
public function hasError() public function hasError()
{ {
if (! $this->getCart()) if (! $this->getCart()) {
return true; return true;
}
if (! $this->isItemsHaveSufficientQuantity()) if (! $this->isItemsHaveSufficientQuantity()) {
return true; return true;
}
return false; return false;
} }
@ -808,8 +823,9 @@ class Cart {
public function isItemsHaveSufficientQuantity() public function isItemsHaveSufficientQuantity()
{ {
foreach ($this->getCart()->items as $item) { foreach ($this->getCart()->items as $item) {
if (! $this->isItemHaveQuantity($item)) if (! $this->isItemHaveQuantity($item)) {
return false; return false;
}
} }
return true; return true;
@ -852,42 +868,42 @@ class Cart {
$data = $this->toArray(); $data = $this->toArray();
$finalData = [ $finalData = [
'cart_id' => $this->getCart()->id, 'cart_id' => $this->getCart()->id,
'customer_id' => $data['customer_id'], 'customer_id' => $data['customer_id'],
'is_guest' => $data['is_guest'], 'is_guest' => $data['is_guest'],
'customer_email' => $data['customer_email'], 'customer_email' => $data['customer_email'],
'customer_first_name' => $data['customer_first_name'], 'customer_first_name' => $data['customer_first_name'],
'customer_last_name' => $data['customer_last_name'], 'customer_last_name' => $data['customer_last_name'],
'customer' => $this->getCurrentCustomer()->check() ? $this->getCurrentCustomer()->user() : null, 'customer' => $this->getCurrentCustomer()->check() ? $this->getCurrentCustomer()->user() : null,
'total_item_count' => $data['items_count'], 'total_item_count' => $data['items_count'],
'total_qty_ordered' => $data['items_qty'], 'total_qty_ordered' => $data['items_qty'],
'base_currency_code' => $data['base_currency_code'], 'base_currency_code' => $data['base_currency_code'],
'channel_currency_code' => $data['channel_currency_code'], 'channel_currency_code' => $data['channel_currency_code'],
'order_currency_code' => $data['cart_currency_code'], 'order_currency_code' => $data['cart_currency_code'],
'grand_total' => $data['grand_total'], 'grand_total' => $data['grand_total'],
'base_grand_total' => $data['base_grand_total'], 'base_grand_total' => $data['base_grand_total'],
'sub_total' => $data['sub_total'], 'sub_total' => $data['sub_total'],
'base_sub_total' => $data['base_sub_total'], 'base_sub_total' => $data['base_sub_total'],
'tax_amount' => $data['tax_total'], 'tax_amount' => $data['tax_total'],
'base_tax_amount' => $data['base_tax_total'], 'base_tax_amount' => $data['base_tax_total'],
'coupon_code' => $data['coupon_code'], 'coupon_code' => $data['coupon_code'],
'applied_cart_rule_ids' => $data['applied_cart_rule_ids'], 'applied_cart_rule_ids' => $data['applied_cart_rule_ids'],
'discount_amount' => $data['discount_amount'], 'discount_amount' => $data['discount_amount'],
'base_discount_amount' => $data['base_discount_amount'], 'base_discount_amount' => $data['base_discount_amount'],
'billing_address' => Arr::except($data['billing_address'], ['id', 'cart_id']), 'billing_address' => Arr::except($data['billing_address'], ['id', 'cart_id']),
'payment' => Arr::except($data['payment'], ['id', 'cart_id']), 'payment' => Arr::except($data['payment'], ['id', 'cart_id']),
'channel' => core()->getCurrentChannel(), 'channel' => core()->getCurrentChannel(),
]; ];
if ($this->getCart()->haveStockableItems()) { if ($this->getCart()->haveStockableItems()) {
$finalData = array_merge($finalData, [ $finalData = array_merge($finalData, [
'shipping_method' => $data['selected_shipping_rate']['method'], 'shipping_method' => $data['selected_shipping_rate']['method'],
'shipping_title' => $data['selected_shipping_rate']['carrier_title'] . ' - ' . $data['selected_shipping_rate']['method_title'], 'shipping_title' => $data['selected_shipping_rate']['carrier_title'] . ' - ' . $data['selected_shipping_rate']['method_title'],
'shipping_description' => $data['selected_shipping_rate']['method_description'], 'shipping_description' => $data['selected_shipping_rate']['method_description'],
'shipping_amount' => $data['selected_shipping_rate']['price'], 'shipping_amount' => $data['selected_shipping_rate']['price'],
'base_shipping_amount' => $data['selected_shipping_rate']['base_price'], 'base_shipping_amount' => $data['selected_shipping_rate']['base_price'],
'shipping_address' => Arr::except($data['shipping_address'], ['id', 'cart_id']), 'shipping_address' => Arr::except($data['shipping_address'], ['id', 'cart_id']),
'shipping_discount_amount' => $data['selected_shipping_rate']['discount_amount'], 'shipping_discount_amount' => $data['selected_shipping_rate']['discount_amount'],
'base_shipping_discount_amount' => $data['selected_shipping_rate']['base_discount_amount'], 'base_shipping_discount_amount' => $data['selected_shipping_rate']['base_discount_amount'],
]); ]);
} }
@ -908,24 +924,24 @@ class Cart {
public function prepareDataForOrderItem($data) public function prepareDataForOrderItem($data)
{ {
$finalData = [ $finalData = [
'product' => $this->productRepository->find($data['product_id']), 'product' => $this->productRepository->find($data['product_id']),
'sku' => $data['sku'], 'sku' => $data['sku'],
'type' => $data['type'], 'type' => $data['type'],
'name' => $data['name'], 'name' => $data['name'],
'weight' => $data['weight'], 'weight' => $data['weight'],
'total_weight' => $data['total_weight'], 'total_weight' => $data['total_weight'],
'qty_ordered' => $data['quantity'], 'qty_ordered' => $data['quantity'],
'price' => $data['price'], 'price' => $data['price'],
'base_price' => $data['base_price'], 'base_price' => $data['base_price'],
'total' => $data['total'], 'total' => $data['total'],
'base_total' => $data['base_total'], 'base_total' => $data['base_total'],
'tax_percent' => $data['tax_percent'], 'tax_percent' => $data['tax_percent'],
'tax_amount' => $data['tax_amount'], 'tax_amount' => $data['tax_amount'],
'base_tax_amount' => $data['base_tax_amount'], 'base_tax_amount' => $data['base_tax_amount'],
'discount_percent' => $data['discount_percent'], 'discount_percent' => $data['discount_percent'],
'discount_amount' => $data['discount_amount'], 'discount_amount' => $data['discount_amount'],
'base_discount_amount' => $data['base_discount_amount'], 'base_discount_amount' => $data['base_discount_amount'],
'additional' => $data['additional'], 'additional' => $data['additional'],
]; ];
if (isset($data['children']) && $data['children']) { if (isset($data['children']) && $data['children']) {
@ -947,11 +963,13 @@ class Cart {
*/ */
public function moveToCart($wishlistItem) public function moveToCart($wishlistItem)
{ {
if (! $wishlistItem->product->getTypeInstance()->canBeMovedFromWishlistToCart($wishlistItem)) if (! $wishlistItem->product->getTypeInstance()->canBeMovedFromWishlistToCart($wishlistItem)) {
return false; return false;
}
if (! $wishlistItem->additional) if (! $wishlistItem->additional) {
$wishlistItem->additional = ['product_id' => $wishlistItem->product_id]; $wishlistItem->additional = ['product_id' => $wishlistItem->product_id];
}
request()->merge($wishlistItem->additional); request()->merge($wishlistItem->additional);
@ -978,12 +996,13 @@ class Cart {
$cartItem = $cart->items()->find($itemId); $cartItem = $cart->items()->find($itemId);
if (! $cartItem) if (! $cartItem) {
return false; return false;
}
$wishlistItems = $this->wishlistRepository->findWhere([ $wishlistItems = $this->wishlistRepository->findWhere([
'customer_id' => $this->getCurrentCustomer()->user()->id, 'customer_id' => $this->getCurrentCustomer()->user()->id,
'product_id' => $cartItem->product_id 'product_id' => $cartItem->product_id
]); ]);
$found = false; $found = false;
@ -991,26 +1010,29 @@ class Cart {
foreach ($wishlistItems as $wishlistItem) { foreach ($wishlistItems as $wishlistItem) {
$options = $wishlistItem->item_options; $options = $wishlistItem->item_options;
if (! $options) if (! $options) {
$options = ['product_id' => $wishlistItem->product_id]; $options = ['product_id' => $wishlistItem->product_id];
}
if ($cartItem->product->getTypeInstance()->compareOptions($cartItem->additional, $options)) if ($cartItem->product->getTypeInstance()->compareOptions($cartItem->additional, $options)) {
$found = true; $found = true;
}
} }
if (! $found) { if (! $found) {
$this->wishlistRepository->create([ $this->wishlistRepository->create([
'channel_id' => $cart->channel_id, 'channel_id' => $cart->channel_id,
'customer_id' => $this->getCurrentCustomer()->user()->id, 'customer_id' => $this->getCurrentCustomer()->user()->id,
'product_id' => $cartItem->product_id, 'product_id' => $cartItem->product_id,
'additional' => $cartItem->additional 'additional' => $cartItem->additional
]); ]);
} }
$result = $this->cartItemRepository->delete($itemId); $result = $this->cartItemRepository->delete($itemId);
if (! $cart->items()->count()) if (! $cart->items()->count()) {
$this->cartRepository->delete($cart->id); $this->cartRepository->delete($cart->id);
}
$this->collectTotals(); $this->collectTotals();

View File

@ -33,14 +33,14 @@ class CustomerAddressForm extends FormRequest
} else { } else {
$this->rules = [ $this->rules = [
'billing.first_name' => ['required'], 'billing.first_name' => ['required'],
'billing.last_name' => ['required'], 'billing.last_name' => ['required'],
'billing.email' => ['required'], 'billing.email' => ['required'],
'billing.address1' => ['required'], 'billing.address1' => ['required'],
'billing.city' => ['required'], 'billing.city' => ['required'],
'billing.state' => ['required'], 'billing.state' => ['required'],
'billing.postcode' => ['required'], 'billing.postcode' => ['required'],
'billing.phone' => ['required'], 'billing.phone' => ['required'],
'billing.country' => ['required'] 'billing.country' => ['required']
]; ];
} }
@ -52,14 +52,14 @@ class CustomerAddressForm extends FormRequest
} else { } else {
$this->rules = array_merge($this->rules, [ $this->rules = array_merge($this->rules, [
'shipping.first_name' => ['required'], 'shipping.first_name' => ['required'],
'shipping.last_name' => ['required'], 'shipping.last_name' => ['required'],
'shipping.email' => ['required'], 'shipping.email' => ['required'],
'shipping.address1' => ['required'], 'shipping.address1' => ['required'],
'shipping.city' => ['required'], 'shipping.city' => ['required'],
'shipping.state' => ['required'], 'shipping.state' => ['required'],
'shipping.postcode' => ['required'], 'shipping.postcode' => ['required'],
'shipping.phone' => ['required'], 'shipping.phone' => ['required'],
'shipping.country' => ['required'] 'shipping.country' => ['required']
]); ]);
} }
} }

View File

@ -108,8 +108,9 @@ class Cart extends Model implements CartContract
public function haveStockableItems() public function haveStockableItems()
{ {
foreach ($this->items as $item) { foreach ($this->items as $item) {
if ($item->product->isStockable()) if ($item->product->isStockable()) {
return true; return true;
}
} }
return false; return false;