sarga/packages/Webkul/Checkout/src/Cart.php

955 lines
31 KiB
PHP
Raw Normal View History

2018-09-11 11:19:40 +00:00
<?php
2018-09-28 13:28:54 +00:00
namespace Webkul\Checkout;
2018-09-11 11:19:40 +00:00
2018-09-28 13:28:54 +00:00
use Webkul\Checkout\Repositories\CartRepository;
use Webkul\Checkout\Repositories\CartItemRepository;
use Webkul\Checkout\Repositories\CartAddressRepository;
2018-09-21 10:17:43 +00:00
use Webkul\Product\Repositories\ProductRepository;
2018-10-15 10:39:09 +00:00
use Webkul\Tax\Repositories\TaxCategoryRepository;
2019-06-28 14:18:52 +00:00
use Webkul\Checkout\Models\CartItem;
2018-09-28 13:28:54 +00:00
use Webkul\Checkout\Models\CartPayment;
2018-10-30 04:31:01 +00:00
use Webkul\Customer\Repositories\WishlistRepository;
2019-02-14 15:50:10 +00:00
use Webkul\Customer\Repositories\CustomerAddressRepository;
use Webkul\Product\Helpers\Price;
2019-08-19 09:30:24 +00:00
use Illuminate\Support\Facades\Event;
2018-09-11 11:19:40 +00:00
/**
2019-01-29 10:35:30 +00:00
* Facades handler for all the methods to be implemented in Cart.
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
2018-09-11 11:19:40 +00:00
class Cart {
2018-09-26 04:21:14 +00:00
/**
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
* CartRepository instance
2018-09-26 04:21:14 +00:00
*
* @var mixed
*/
2019-07-01 11:33:36 +00:00
protected $cartRepository;
2018-09-26 04:21:14 +00:00
/**
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
* CartItemRepository instance
2018-09-26 04:21:14 +00:00
*
* @var mixed
*/
2019-07-01 11:33:36 +00:00
protected $cartItemRepository;
2018-09-26 04:21:14 +00:00
/**
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
* CartAddressRepository instance
2018-09-26 04:21:14 +00:00
*
* @var mixed
*/
2019-07-01 11:33:36 +00:00
protected $cartAddressRepository;
2018-09-19 07:00:24 +00:00
2018-09-26 04:21:14 +00:00
/**
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
* ProductRepository instance
2018-09-26 04:21:14 +00:00
*
* @var mixed
*/
2019-07-01 11:33:36 +00:00
protected $productRepository;
2018-10-15 10:39:09 +00:00
/**
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
* TaxCategoryRepository instance
2018-10-15 10:39:09 +00:00
*
* @var mixed
*/
2019-07-01 11:33:36 +00:00
protected $taxCategoryRepository;
2018-10-15 10:39:09 +00:00
2018-10-30 04:31:01 +00:00
/**
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
* WishlistRepository instance
2018-10-30 04:31:01 +00:00
*
* @var mixed
*/
2019-07-01 11:33:36 +00:00
protected $wishlistRepository;
2018-10-30 04:31:01 +00:00
2019-02-14 15:50:10 +00:00
/**
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
* CustomerAddressRepository instance
2019-02-14 15:50:10 +00:00
*
* @var mixed
*/
2019-07-01 11:33:36 +00:00
protected $customerAddressRepository;
/**
* Product price helper instance
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
*/
protected $price;
2018-09-26 04:21:14 +00:00
/**
* Create a new controller instance.
*
2019-07-01 11:33:36 +00:00
* @param Webkul\Checkout\Repositories\CartRepository $cart
* @param Webkul\Checkout\Repositories\CartItemRepository $cartItem
* @param Webkul\Checkout\Repositories\CartAddressRepository $cartAddress
* @param Webkul\Product\Repositories\ProductRepository $product
* @param Webkul\Product\Repositories\TaxCategoryRepository $taxCategory
* @param Webkul\Product\Repositories\CustomerAddressRepository $customerAddress
* @param Webkul\Product\Repositories\CustomerAddressRepository $customerAddress
* @param Webkul\Discount\Repositories\CartRuleRepository $cartRule
* @param Webkul\Helpers\Discount $discount
2018-09-26 04:21:14 +00:00
* @return void
*/
public function __construct(
2019-07-01 11:33:36 +00:00
CartRepository $cartRepository,
CartItemRepository $cartItemRepository,
CartAddressRepository $cartAddressRepository,
ProductRepository $productRepository,
TaxCategoryRepository $taxCategoryRepository,
WishlistRepository $wishlistRepository,
CustomerAddressRepository $customerAddressRepository,
Price $price
2018-10-15 10:39:09 +00:00
)
2018-09-26 04:21:14 +00:00
{
2019-07-01 11:33:36 +00:00
$this->cartRepository = $cartRepository;
2019-07-01 11:33:36 +00:00
$this->cartItemRepository = $cartItemRepository;
2018-09-19 07:00:24 +00:00
2019-07-01 11:33:36 +00:00
$this->cartAddressRepository = $cartAddressRepository;
2018-09-26 04:21:14 +00:00
2019-07-01 11:33:36 +00:00
$this->productRepository = $productRepository;
2018-10-15 10:39:09 +00:00
2019-07-01 11:33:36 +00:00
$this->taxCategoryRepository = $taxCategoryRepository;
2018-10-30 04:31:01 +00:00
2019-07-01 11:33:36 +00:00
$this->wishlistRepository = $wishlistRepository;
2019-07-01 11:33:36 +00:00
$this->customerAddressRepository = $customerAddressRepository;
2019-02-14 15:50:10 +00:00
$this->price = $price;
2018-09-14 13:15:49 +00:00
}
/**
* Return current logged in customer
*
2019-08-19 09:30:24 +00:00
* @return Customer|boolean
*/
public function getCurrentCustomer()
{
2019-04-24 10:35:29 +00:00
$guard = request()->has('token') ? 'api' : 'customer';
2019-04-24 10:35:29 +00:00
return auth()->guard($guard);
}
2018-09-28 12:55:48 +00:00
/**
2019-08-19 09:30:24 +00:00
* Add Items in a cart with some cart and item details.
2018-09-28 12:55:48 +00:00
*
2019-08-19 09:30:24 +00:00
* @param integer $productId
* @param array $data
2019-08-19 09:30:24 +00:00
* @return Cart
*/
public function addProduct($productId, $data)
{
Event::fire('checkout.cart.add.before', $productId);
$cart = $this->getCart();
if (! $cart && ! $cart = $this->create($data))
return;
$product = $this->productRepository->findOneByField('id', $productId);
$cartProducts = $product->getTypeInstance()->prepareForCart($data);
if (is_string($cartProducts)) {
throw new \Exception($cartProducts);
} else {
$parentCartItem = null;
foreach ($cartProducts as $cartProduct) {
$cartItem = $this->getItemByProduct($cartProduct);
if (isset($cartProduct['parent_id']))
$cartProduct['parent_id'] = $parentCartItem->id;
if (! $cartItem) {
$cartItem = $this->cartItemRepository->create(array_merge($cartProduct, ['cart_id' => $cart->id]));
} else {
$cartItem = $this->cartItemRepository->update($cartProduct, $cartItem->id);
}
if (! $parentCartItem)
$parentCartItem = $cartItem;
}
}
Event::fire('checkout.cart.add.after', $cart);
$this->collectTotals();
return $cart;
}
/**
* Create new cart instance.
2018-09-28 12:55:48 +00:00
*
2019-08-19 09:30:24 +00:00
* @param array $data
* @return Cart|null
2018-09-28 12:55:48 +00:00
*/
2019-08-19 09:30:24 +00:00
public function create($data)
2018-09-28 12:55:48 +00:00
{
$cartData = [
'channel_id' => core()->getCurrentChannel()->id,
'global_currency_code' => core()->getBaseCurrencyCode(),
'base_currency_code' => core()->getBaseCurrencyCode(),
'channel_currency_code' => core()->getChannelBaseCurrencyCode(),
'cart_currency_code' => core()->getCurrentCurrencyCode(),
'items_count' => 1
];
2018-10-25 09:56:24 +00:00
//Authentication details
if ($this->getCurrentCustomer()->check()) {
$cartData['customer_id'] = $this->getCurrentCustomer()->user()->id;
$cartData['is_guest'] = 0;
$cartData['customer_first_name'] = $this->getCurrentCustomer()->user()->first_name;
$cartData['customer_last_name'] = $this->getCurrentCustomer()->user()->last_name;
$cartData['customer_email'] = $this->getCurrentCustomer()->user()->email;
} else {
$cartData['is_guest'] = 1;
2018-09-28 12:55:48 +00:00
}
2018-10-10 10:26:27 +00:00
2019-08-19 09:30:24 +00:00
$cart = $this->cartRepository->create($cartData);
2019-08-19 09:30:24 +00:00
if (! $cart) {
session()->flash('error', trans('shop::app.checkout.cart.create-error'));
2018-09-28 12:55:48 +00:00
2019-08-19 09:30:24 +00:00
return;
}
2018-10-11 05:39:17 +00:00
2019-08-19 09:30:24 +00:00
$this->putCart($cart);
2018-10-11 05:39:17 +00:00
2019-08-19 09:30:24 +00:00
return $cart;
}
/**
2019-08-19 09:30:24 +00:00
* Update cart items information
*
2019-08-19 09:30:24 +00:00
* @param array $data
*
* @return string|boolean
*/
2019-08-19 09:30:24 +00:00
public function updateItems($data)
{
2019-08-19 09:30:24 +00:00
foreach ($data['qty'] as $itemId => $quantity) {
$item = $this->cartItemRepository->findOneByField('id', $itemId);
if (! $item)
continue;
2018-10-11 05:39:17 +00:00
2019-08-19 09:30:24 +00:00
if ($quantity <= 0) {
$this->removeItem($itemId);
2018-10-30 11:22:36 +00:00
2019-08-19 09:30:24 +00:00
throw new \Exception(trans('shop::app.checkout.cart.quantity.illegal'));
}
2018-10-30 11:22:36 +00:00
2019-08-19 09:30:24 +00:00
if ($item->product->isStockable() && ! $item->product->haveSufficientQuantity($quantity))
throw new \Exception(trans('shop::app.checkout.cart.quantity.inventory_warning'));
2019-08-19 09:30:24 +00:00
Event::fire('checkout.cart.update.before', $item);
2019-06-28 14:18:52 +00:00
2019-08-19 09:30:24 +00:00
$this->cartItemRepository->update([
'quantity' => $quantity,
'total' => core()->convertPrice($item->price * $quantity),
'base_total' => $item->price * $quantity,
'total_weight' => $item->weight * $quantity,
'base_total_weight' => $item->weight * $quantity
], $itemId);
2019-08-19 09:30:24 +00:00
Event::fire('checkout.cart.update.after', $item);
}
2018-10-11 05:39:17 +00:00
2019-08-19 09:30:24 +00:00
$this->collectTotals();
2019-08-19 09:30:24 +00:00
return true;
2018-10-11 05:39:17 +00:00
}
2018-09-14 13:15:49 +00:00
/**
2019-08-19 09:30:24 +00:00
* Get cart item by product
2019-06-28 14:18:52 +00:00
*
2019-08-19 09:30:24 +00:00
* @param array $data
* @return CartItem|void
*/
2019-08-19 09:30:24 +00:00
public function getItemByProduct($data)
{
2019-08-19 09:30:24 +00:00
$items = $this->getCart()->all_items;
2019-08-19 09:30:24 +00:00
foreach ($items as $item) {
if ($item->product->getTypeInstance()->compareOptions($item->additional, $data['additional']))
return $item;
}
}
/**
* Remove the item from the cart
*
2019-08-19 09:30:24 +00:00
* @param integer $itemId
* @return boolean
*/
public function removeItem($itemId)
{
2019-08-19 09:30:24 +00:00
Event::fire('checkout.cart.delete.before', $itemId);
2018-09-13 13:40:01 +00:00
2019-08-19 09:30:24 +00:00
if (! $cart = $this->getCart())
return false;
2018-09-13 13:40:01 +00:00
2019-08-19 09:30:24 +00:00
$this->cartItemRepository->delete($itemId);
2018-09-26 04:21:14 +00:00
2019-08-19 09:30:24 +00:00
//delete the cart instance if no items are there
if ($cart->items()->get()->count() == 0) {
$this->cartRepository->delete($cart->id);
2019-08-19 09:30:24 +00:00
if (session()->has('cart')) {
session()->forget('cart');
}
}
2018-09-14 13:15:49 +00:00
2019-08-19 09:30:24 +00:00
Event::fire('checkout.cart.delete.after', $itemId);
$this->collectTotals();
return true;
2018-10-10 10:26:27 +00:00
}
/**
2018-10-11 05:39:17 +00:00
* This function handles when guest has some of cart products and then logs in.
2018-09-28 12:55:48 +00:00
*
2019-08-19 09:30:24 +00:00
* @return boolean
2018-09-28 12:55:48 +00:00
*/
2018-10-11 05:39:17 +00:00
public function mergeCart()
2018-09-28 12:55:48 +00:00
{
2019-01-15 11:54:41 +00:00
if (session()->has('cart')) {
2019-08-19 09:30:24 +00:00
$cart = $this->cartRepository->findOneWhere(['customer_id' => $this->getCurrentCustomer()->user()->id, 'is_active' => 1]);
2018-09-28 12:55:48 +00:00
2018-10-11 05:39:17 +00:00
$guestCart = session()->get('cart');
2018-09-27 10:46:57 +00:00
//when the logged in customer is not having any of the cart instance previously and are active.
2019-01-15 11:54:41 +00:00
if (! $cart) {
2019-08-19 09:30:24 +00:00
$this->cartRepository->update([
'customer_id' => $this->getCurrentCustomer()->user()->id,
'is_guest' => 0,
'customer_first_name' => $this->getCurrentCustomer()->user()->first_name,
'customer_last_name' => $this->getCurrentCustomer()->user()->last_name,
'customer_email' => $this->getCurrentCustomer()->user()->email
2019-08-19 09:30:24 +00:00
], $guestCart->id);
2018-10-11 05:39:17 +00:00
session()->forget('cart');
2018-10-10 10:26:27 +00:00
return true;
2018-10-11 05:39:17 +00:00
}
2019-08-19 09:30:24 +00:00
foreach ($guestCart->items as $key => $guestCartItem) {
$found = false;
foreach ($cart->items as $cartItem) {
if (! $cartItem->product->getTypeInstance()->compareOptions($cartItem->additional, $guestCartItem->additional))
continue;
2018-09-27 10:07:54 +00:00
2019-08-19 09:30:24 +00:00
$newQuantity = $cartItem->quantity + $guestCartItem->quantity;
2018-09-28 12:55:48 +00:00
2019-08-19 09:30:24 +00:00
if ($cartItem->product->isStockable() && ! $cartItem->product->haveSufficientQuantity($newQuantity)) {
2019-07-01 11:33:36 +00:00
$this->cartItemRepository->delete($guestCartItem->id);
2018-09-28 12:55:48 +00:00
2019-08-19 09:30:24 +00:00
continue;
}
2018-09-20 15:12:11 +00:00
2019-08-19 09:30:24 +00:00
$this->cartItemRepository->update([
'quantity' => $newQuantity,
'total' => core()->convertPrice($cartItem->price * $newQuantity),
'base_total' => $cartItem->price * $newQuantity,
'total_weight' => $cartItem->weight * $newQuantity,
'base_total_weight' => $cartItem->weight * $newQuantity
], $cartItem->id);
2018-09-28 12:55:48 +00:00
2019-08-19 09:30:24 +00:00
$guestCart->items->forget($key);
2019-08-19 09:30:24 +00:00
$this->cartItemRepository->delete($guestCartItem->id);
2019-07-01 11:33:36 +00:00
2019-08-19 09:30:24 +00:00
$found = true;
}
2019-07-01 11:33:36 +00:00
2019-08-19 09:30:24 +00:00
if (! $found) {
$this->cartItemRepository->update([
'cart_id' => $cart->id
], $guestCartItem->id);
2019-07-01 11:33:36 +00:00
2019-08-19 09:30:24 +00:00
foreach ($guestCartItem->children as $child) {
$this->cartItemRepository->update([
'cart_id' => $cart->id
], $child->id);
}
}
2018-10-11 05:39:17 +00:00
}
2019-08-19 09:30:24 +00:00
$this->collectTotals();
2019-08-19 09:30:24 +00:00
$this->cartRepository->delete($guestCart->id);
2018-09-28 12:55:48 +00:00
2018-10-11 05:39:17 +00:00
session()->forget('cart');
}
2019-08-19 09:30:24 +00:00
return true;
}
2018-10-11 05:39:17 +00:00
/**
* Save cart
*
2019-08-19 09:30:24 +00:00
* @param Cart $cart
* @return void
2018-10-11 05:39:17 +00:00
*/
public function putCart($cart)
{
if (! $this->getCurrentCustomer()->check()) {
2018-10-11 05:39:17 +00:00
session()->put('cart', $cart);
}
}
/**
* Returns cart
*
2019-08-19 09:30:24 +00:00
* @return Cart|null
2018-10-11 05:39:17 +00:00
*/
public function getCart()
{
2019-04-16 11:02:30 +00:00
$cart = null;
2018-10-11 05:39:17 +00:00
if ($this->getCurrentCustomer()->check()) {
2019-07-01 11:33:36 +00:00
$cart = $this->cartRepository->findOneWhere([
'customer_id' => $this->getCurrentCustomer()->user()->id,
'is_active' => 1
]);
2018-10-15 10:39:09 +00:00
} elseif (session()->has('cart')) {
2019-07-01 11:33:36 +00:00
$cart = $this->cartRepository->find(session()->get('cart')->id);
2018-10-11 05:39:17 +00:00
}
return $cart && $cart->is_active ? $cart : null;
}
/**
* Returns cart details in array
*
* @return array
*/
public function toArray()
{
$cart = $this->getCart();
$data = $cart->toArray();
2019-04-16 10:10:58 +00:00
$data['billing_address'] = $cart->billing_address->toArray();
2018-10-11 05:39:17 +00:00
2019-06-28 14:18:52 +00:00
if ($cart->haveStockableItems()) {
$data['shipping_address'] = $cart->shipping_address->toArray();
$data['selected_shipping_rate'] = $cart->selected_shipping_rate->toArray();
}
2018-10-11 05:39:17 +00:00
2019-04-16 10:10:58 +00:00
$data['payment'] = $cart->payment->toArray();
$data['items'] = $cart->items->toArray();
2018-10-11 05:39:17 +00:00
return $data;
}
2018-09-26 04:21:14 +00:00
/**
* Save customer address
*
2019-08-19 09:30:24 +00:00
* @param array $data
* @return boolean
2018-09-26 04:21:14 +00:00
*/
public function saveCustomerAddress($data)
{
2019-01-15 11:54:41 +00:00
if (! $cart = $this->getCart())
2018-09-26 04:21:14 +00:00
return false;
$billingAddress = $data['billing'];
2019-06-28 14:18:52 +00:00
$billingAddress['cart_id'] = $cart->id;
2018-09-26 04:21:14 +00:00
2019-02-15 14:40:47 +00:00
if (isset($data['billing']['address_id']) && $data['billing']['address_id']) {
2019-07-01 11:33:36 +00:00
$address = $this->customerAddressRepository->findOneWhere(['id'=> $data['billing']['address_id']])->toArray();
2019-06-28 14:18:52 +00:00
$billingAddress['first_name'] = $this->getCurrentCustomer()->user()->first_name;
$billingAddress['last_name'] = $this->getCurrentCustomer()->user()->last_name;
$billingAddress['email'] = $this->getCurrentCustomer()->user()->email;
2019-02-14 15:50:10 +00:00
$billingAddress['address1'] = $address['address1'];
$billingAddress['country'] = $address['country'];
$billingAddress['state'] = $address['state'];
$billingAddress['city'] = $address['city'];
$billingAddress['postcode'] = $address['postcode'];
$billingAddress['phone'] = $address['phone'];
}
2019-02-15 14:40:47 +00:00
if (isset($data['billing']['save_as_address']) && $data['billing']['save_as_address']) {
$billingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id;
2019-07-01 11:33:36 +00:00
$this->customerAddressRepository->create($billingAddress);
2019-02-15 14:40:47 +00:00
}
2019-06-28 14:18:52 +00:00
if ($cart->haveStockableItems()) {
$shippingAddress = $data['shipping'];
$shippingAddress['cart_id'] = $cart->id;
if (isset($data['shipping']['address_id']) && $data['shipping']['address_id']) {
2019-07-01 11:33:36 +00:00
$address = $this->customerAddressRepository->findOneWhere(['id'=> $data['shipping']['address_id']])->toArray();
2019-06-28 14:18:52 +00:00
$shippingAddress['first_name'] = $this->getCurrentCustomer()->user()->first_name;
$shippingAddress['last_name'] = $this->getCurrentCustomer()->user()->last_name;
$shippingAddress['email'] = $this->getCurrentCustomer()->user()->email;
$shippingAddress['address1'] = $address['address1'];
$shippingAddress['country'] = $address['country'];
$shippingAddress['state'] = $address['state'];
$shippingAddress['city'] = $address['city'];
$shippingAddress['postcode'] = $address['postcode'];
$shippingAddress['phone'] = $address['phone'];
}
if (isset($data['shipping']['save_as_address']) && $data['shipping']['save_as_address']) {
$shippingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id;
2019-07-01 11:33:36 +00:00
$this->customerAddressRepository->create($shippingAddress);
2019-06-28 14:18:52 +00:00
}
2019-02-15 14:40:47 +00:00
}
2019-02-14 15:50:10 +00:00
2019-01-15 11:54:41 +00:00
if ($billingAddressModel = $cart->billing_address) {
2019-07-01 11:33:36 +00:00
$this->cartAddressRepository->update($billingAddress, $billingAddressModel->id);
2018-09-26 04:21:14 +00:00
2019-06-28 14:18:52 +00:00
if ($cart->haveStockableItems()) {
if ($shippingAddressModel = $cart->shipping_address) {
if (isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
2019-07-01 11:33:36 +00:00
$this->cartAddressRepository->update($billingAddress, $shippingAddressModel->id);
2019-06-28 14:18:52 +00:00
} else {
2019-07-01 11:33:36 +00:00
$this->cartAddressRepository->update($shippingAddress, $shippingAddressModel->id);
2019-06-28 14:18:52 +00:00
}
2018-09-26 04:21:14 +00:00
} else {
2019-06-28 14:18:52 +00:00
if (isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
2019-07-01 11:33:36 +00:00
$this->cartAddressRepository->create(array_merge($billingAddress, ['address_type' => 'shipping']));
2019-06-28 14:18:52 +00:00
} else {
2019-07-01 11:33:36 +00:00
$this->cartAddressRepository->create(array_merge($shippingAddress, ['address_type' => 'shipping']));
2019-06-28 14:18:52 +00:00
}
2018-09-26 04:21:14 +00:00
}
2019-06-28 14:18:52 +00:00
}
} else {
2019-07-01 11:33:36 +00:00
$this->cartAddressRepository->create(array_merge($billingAddress, ['address_type' => 'billing']));
2019-06-28 14:18:52 +00:00
if ($cart->haveStockableItems()) {
2019-01-15 11:54:41 +00:00
if (isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
2019-07-01 11:33:36 +00:00
$this->cartAddressRepository->create(array_merge($billingAddress, ['address_type' => 'shipping']));
2018-09-26 04:21:14 +00:00
} else {
2019-07-01 11:33:36 +00:00
$this->cartAddressRepository->create(array_merge($shippingAddress, ['address_type' => 'shipping']));
2018-09-26 04:21:14 +00:00
}
}
}
if ($this->getCurrentCustomer()->check()) {
$cart->customer_email = $this->getCurrentCustomer()->user()->email;
$cart->customer_first_name = $this->getCurrentCustomer()->user()->first_name;
$cart->customer_last_name = $this->getCurrentCustomer()->user()->last_name;
} else {
$cart->customer_email = $cart->billing_address->email;
$cart->customer_first_name = $cart->billing_address->first_name;
$cart->customer_last_name = $cart->billing_address->last_name;
}
2018-10-26 13:43:01 +00:00
$cart->save();
2018-10-15 10:39:09 +00:00
2018-09-26 04:21:14 +00:00
return true;
}
/**
* Save shipping method for cart
*
* @param string $shippingMethodCode
2019-08-19 09:30:24 +00:00
* @return boolean
2018-09-26 04:21:14 +00:00
*/
public function saveShippingMethod($shippingMethodCode)
{
2019-01-15 11:54:41 +00:00
if (! $cart = $this->getCart())
2018-09-26 04:21:14 +00:00
return false;
2018-09-26 10:19:18 +00:00
$cart->shipping_method = $shippingMethodCode;
$cart->save();
2018-09-26 04:21:14 +00:00
return true;
}
2018-09-26 10:19:18 +00:00
/**
* Save payment method for cart
*
* @param string $payment
2019-08-19 09:30:24 +00:00
* @return CartPayment
2018-09-26 10:19:18 +00:00
*/
public function savePaymentMethod($payment)
{
2019-01-15 11:54:41 +00:00
if (! $cart = $this->getCart())
2018-09-26 10:19:18 +00:00
return false;
2019-01-15 11:54:41 +00:00
if ($cartPayment = $cart->payment)
2018-09-26 10:19:18 +00:00
$cartPayment->delete();
$cartPayment = new CartPayment;
$cartPayment->method = $payment['method'];
$cartPayment->cart_id = $cart->id;
$cartPayment->save();
return $cartPayment;
}
/**
* Updates cart totals
*
* @return void
*/
public function collectTotals()
{
2018-11-30 07:40:41 +00:00
$validated = $this->validateItems();
2019-08-19 09:30:24 +00:00
if (! $validated)
2018-09-26 10:19:18 +00:00
return false;
2019-01-15 11:54:41 +00:00
if (! $cart = $this->getCart())
2018-11-30 07:40:41 +00:00
return false;
2018-10-15 10:39:09 +00:00
$this->calculateItemsTax();
2018-10-10 10:41:33 +00:00
2018-10-15 10:39:09 +00:00
$cart->grand_total = $cart->base_grand_total = 0;
$cart->sub_total = $cart->base_sub_total = 0;
$cart->tax_total = $cart->base_tax_total = 0;
$cart->discount_amount = $cart->base_discount_amount = 0;
2018-09-26 10:19:18 +00:00
foreach ($cart->items()->get() as $item) {
$cart->discount_amount += $item->discount_amount;
$cart->base_discount_amount += $item->base_discount_amount;
$cart->grand_total = (float) $cart->grand_total + $item->total + $item->tax_amount - $item->discount_amount;
$cart->base_grand_total = (float) $cart->base_grand_total + $item->base_total + $item->base_tax_amount - $item->base_discount_amount;
2018-09-26 10:19:18 +00:00
$cart->sub_total = (float) $cart->sub_total + $item->total;
$cart->base_sub_total = (float) $cart->base_sub_total + $item->base_total;
$cart->tax_total = (float) $cart->tax_total + $item->tax_amount;
$cart->base_tax_total = (float) $cart->base_tax_total + $item->base_tax_amount;
2018-09-26 10:19:18 +00:00
}
2019-01-15 11:54:41 +00:00
if ($shipping = $cart->selected_shipping_rate) {
2018-09-26 10:19:18 +00:00
$cart->grand_total = (float) $cart->grand_total + $shipping->price;
$cart->base_grand_total = (float) $cart->base_grand_total + $shipping->base_price;
}
2018-10-05 11:59:43 +00:00
$quantities = 0;
2019-01-15 11:54:41 +00:00
foreach ($cart->items as $item) {
2018-10-05 11:59:43 +00:00
$quantities = $quantities + $item->quantity;
}
$cart->items_count = $cart->items->count();
$cart->items_qty = $quantities;
2018-09-26 10:19:18 +00:00
$cart->save();
}
/**
* To validate if the product information is changed by admin and the items have
* been added to the cart before it.
*
* @return boolean
*/
public function validateItems()
{
2019-08-19 09:30:24 +00:00
if (! $cart = $this->getCart())
return;
2018-11-30 07:40:41 +00:00
2018-11-05 04:55:05 +00:00
//rare case of accident-->used when there are no items.
2019-01-15 11:54:41 +00:00
if (count($cart->items) == 0) {
2019-07-01 11:33:36 +00:00
$this->cartRepository->delete($cart->id);
2019-08-19 09:30:24 +00:00
2018-11-30 07:40:41 +00:00
return false;
} else {
2019-08-19 09:30:24 +00:00
foreach ($cart->items as $item) {
if ($item->product_flat->getTypeInstance()->getMinimalPrice($item) == $item->price)
continue;
$price = ! is_null($item->custom_price) ? $item->custom_price : $this->price->getMinimalPrice($productFlat);
$this->cartItemRepository->update([
'price' => core()->convertPrice($price),
'base_price' => $price,
'total' => core()->convertPrice($price * ($item->quantity)),
'base_total' => $price * ($item->quantity),
], $item->id);
}
2019-02-14 15:50:10 +00:00
return true;
}
}
2018-10-11 06:12:39 +00:00
/**
* Calculates cart items tax
*
* @return void
*/
2018-10-15 10:39:09 +00:00
public function calculateItemsTax()
2018-10-11 06:12:39 +00:00
{
2019-02-04 09:48:24 +00:00
if (! $cart = $this->getCart())
return false;
2018-10-11 06:12:39 +00:00
2019-01-15 11:54:41 +00:00
if (! $shippingAddress = $cart->shipping_address)
2018-10-15 10:39:09 +00:00
return;
2018-10-11 06:12:39 +00:00
foreach ($cart->items()->get() as $item) {
2019-07-01 11:33:36 +00:00
$taxCategory = $this->taxCategoryRepository->find($item->product->tax_category_id);
2018-10-15 10:39:09 +00:00
2019-01-15 11:54:41 +00:00
if (! $taxCategory)
2018-10-15 10:39:09 +00:00
continue;
$taxRates = $taxCategory->tax_rates()->where([
'state' => $shippingAddress->state,
'country' => $shippingAddress->country,
])->orderBy('tax_rate', 'desc')->get();
if (count( $taxRates) > 0) {
foreach ($taxRates as $rate) {
$haveTaxRate = false;
2018-10-15 10:39:09 +00:00
if (! $rate->is_zip) {
if ($rate->zip_code == '*' || $rate->zip_code == $shippingAddress->postcode) {
$haveTaxRate = true;
}
} else {
if ($shippingAddress->postcode >= $rate->zip_from && $shippingAddress->postcode <= $rate->zip_to) {
$haveTaxRate = true;
}
2018-10-15 10:39:09 +00:00
}
if ($haveTaxRate) {
$item->tax_percent = $rate->tax_rate;
$item->tax_amount = ($item->total * $rate->tax_rate) / 100;
$item->base_tax_amount = ($item->base_total * $rate->tax_rate) / 100;
2018-10-15 10:39:09 +00:00
$item->save();
break;
}
2018-10-15 10:39:09 +00:00
}
} else {
$item->tax_percent = 0;
$item->tax_amount = 0;
$item->base_tax_amount = 0;
$item->save();
2018-10-15 10:39:09 +00:00
}
2018-10-11 06:12:39 +00:00
}
}
2018-10-05 11:59:43 +00:00
/**
* Checks if cart has any error
*
* @return boolean
*/
2018-10-05 11:59:43 +00:00
public function hasError()
{
2019-01-15 11:54:41 +00:00
if (! $this->getCart())
2018-10-05 11:59:43 +00:00
return true;
2019-01-15 11:54:41 +00:00
if (! $this->isItemsHaveSufficientQuantity())
2018-10-05 11:59:43 +00:00
return true;
2018-10-09 12:04:25 +00:00
2018-10-05 11:59:43 +00:00
return false;
}
/**
* Checks if all cart items have sufficient quantity.
*
* @return boolean
*/
public function isItemsHaveSufficientQuantity()
{
foreach ($this->getCart()->items as $item) {
2019-01-15 11:54:41 +00:00
if (! $this->isItemHaveQuantity($item))
2018-10-05 11:59:43 +00:00
return false;
}
return true;
}
/**
* Checks if all cart items have sufficient quantity.
*
2019-08-19 09:30:24 +00:00
* @param CartItem $item
2018-10-05 11:59:43 +00:00
* @return boolean
*/
public function isItemHaveQuantity($item)
{
2019-08-19 09:30:24 +00:00
return $item->product->getTypeInstance()->isItemHaveQuantity($item);
2018-10-05 11:59:43 +00:00
}
2018-10-09 12:04:25 +00:00
2018-10-05 06:18:58 +00:00
/**
* Deactivates current cart
*
* @return void
*/
public function deActivateCart()
{
2019-01-15 11:54:41 +00:00
if ($cart = $this->getCart()) {
2019-07-01 11:33:36 +00:00
$this->cartRepository->update(['is_active' => false], $cart->id);
2018-10-05 11:59:43 +00:00
2019-01-15 11:54:41 +00:00
if (session()->has('cart')) {
2018-10-05 11:59:43 +00:00
session()->forget('cart');
}
2018-10-05 06:18:58 +00:00
}
}
/**
* Validate order before creation
*
* @return array
*/
public function prepareDataForOrder()
{
$data = $this->toArray();
$finalData = [
2018-11-16 10:11:08 +00:00
'cart_id' => $this->getCart()->id,
2018-10-05 06:18:58 +00:00
'customer_id' => $data['customer_id'],
'is_guest' => $data['is_guest'],
'customer_email' => $data['customer_email'],
'customer_first_name' => $data['customer_first_name'],
'customer_last_name' => $data['customer_last_name'],
'customer' => $this->getCurrentCustomer()->check() ? $this->getCurrentCustomer()->user() : null,
2018-10-05 06:18:58 +00:00
'total_item_count' => $data['items_count'],
'total_qty_ordered' => $data['items_qty'],
'base_currency_code' => $data['base_currency_code'],
'channel_currency_code' => $data['channel_currency_code'],
'order_currency_code' => $data['cart_currency_code'],
'grand_total' => $data['grand_total'],
'base_grand_total' => $data['base_grand_total'],
'sub_total' => $data['sub_total'],
'base_sub_total' => $data['base_sub_total'],
'tax_amount' => $data['tax_total'],
'base_tax_amount' => $data['base_tax_total'],
'discount_amount' => $data['discount_amount'],
'base_discount_amount' => $data['base_discount_amount'],
2018-10-05 06:18:58 +00:00
'billing_address' => array_except($data['billing_address'], ['id', 'cart_id']),
'payment' => array_except($data['payment'], ['id', 'cart_id']),
'channel' => core()->getCurrentChannel(),
2018-10-05 06:18:58 +00:00
];
2019-06-28 14:18:52 +00:00
if ($this->getCart()->haveStockableItems()) {
$finalData = array_merge($finalData, [
'shipping_method' => $data['selected_shipping_rate']['method'],
'shipping_title' => $data['selected_shipping_rate']['carrier_title'] . ' - ' . $data['selected_shipping_rate']['method_title'],
'shipping_description' => $data['selected_shipping_rate']['method_description'],
'shipping_amount' => $data['selected_shipping_rate']['price'],
'base_shipping_amount' => $data['selected_shipping_rate']['base_price'],
'shipping_address' => array_except($data['shipping_address'], ['id', 'cart_id']),
]);
}
2019-01-15 11:54:41 +00:00
foreach ($data['items'] as $item) {
2018-10-05 06:18:58 +00:00
$finalData['items'][] = $this->prepareDataForOrderItem($item);
}
return $finalData;
}
/**
* Prepares data for order item
*
2019-08-19 09:30:24 +00:00
* @param array $data
2018-10-05 06:18:58 +00:00
* @return array
*/
public function prepareDataForOrderItem($data)
{
$finalData = [
2019-07-01 11:33:36 +00:00
'product' => $this->productRepository->find($data['product_id']),
2018-10-05 06:18:58 +00:00
'sku' => $data['sku'],
'type' => $data['type'],
'name' => $data['name'],
'weight' => $data['weight'],
'total_weight' => $data['total_weight'],
'qty_ordered' => $data['quantity'],
'price' => $data['price'],
'base_price' => $data['base_price'],
'total' => $data['total'],
'base_total' => $data['base_total'],
'tax_percent' => $data['tax_percent'],
'tax_amount' => $data['tax_amount'],
'base_tax_amount' => $data['base_tax_amount'],
2019-06-13 14:04:13 +00:00
'discount_percent' => $data['discount_percent'],
'discount_amount' => $data['discount_amount'],
'base_discount_amount' => $data['base_discount_amount'],
2018-10-05 06:18:58 +00:00
'additional' => $data['additional'],
];
2019-01-15 11:54:41 +00:00
if (isset($data['child']) && $data['child']) {
2018-10-05 06:18:58 +00:00
$finalData['child'] = $this->prepareDataForOrderItem($data['child']);
}
return $finalData;
}
/**
* Move a wishlist item to cart
2019-08-19 09:30:24 +00:00
*
* @param WishlistItem $wishlistItem
* @return boolean
*/
public function moveToCart($wishlistItem)
{
2019-08-19 09:30:24 +00:00
if (! $wishlistItem->product->getTypeInstance()->canBeMovedFromWishlistToCart($wishlistItem))
return false;
2019-06-29 11:54:41 +00:00
2019-08-19 09:30:24 +00:00
$result = $this->addProduct($wishlistItem->product_id, $wishlistItem->additional);
2019-08-19 09:30:24 +00:00
if ($result) {
$this->wishlistRepository->delete($wishlistItem->id);
2019-08-19 09:30:24 +00:00
return true;
}
2019-08-19 09:30:24 +00:00
return false;
}
/**
* Function to move a already added product to wishlist will run only on customer authentication.
2018-10-30 04:31:01 +00:00
*
2019-08-19 09:30:24 +00:00
* @param integer $itemId
* @return boolean|void
2018-10-30 04:31:01 +00:00
*/
public function moveToWishlist($itemId)
{
2018-10-30 11:22:36 +00:00
$cart = $this->getCart();
2019-06-28 14:18:52 +00:00
2019-08-19 09:30:24 +00:00
$cartItem = $cart->items()->find($itemId);
2018-10-30 11:22:36 +00:00
2019-08-19 09:30:24 +00:00
if (! $cartItem)
return false;
2018-10-30 11:22:36 +00:00
2019-08-19 09:30:24 +00:00
$wishlistItems = $this->wishlistRepository->findWhere([
'customer_id' => $this->getCurrentCustomer()->user()->id,
'product_id' => $cartItem->product_id
]);
2018-10-30 11:22:36 +00:00
2019-08-19 09:30:24 +00:00
$found = false;
2019-08-19 09:30:24 +00:00
foreach ($wishlistItems as $wishlistItem) {
if ($cartItem->product->getTypeInstance()->compareOptions($cartItem->additional, $wishlistItem->item_options))
$found = true;
2018-10-30 11:22:36 +00:00
}
2018-10-30 04:31:01 +00:00
2019-08-19 09:30:24 +00:00
if (! $found) {
$this->wishlistRepository->create([
'channel_id' => $cart->channel_id,
'customer_id' => $this->getCurrentCustomer()->user()->id,
'product_id' => $cartItem->product_id,
'additional' => $cartItem->additional
]);
}
2018-11-17 06:26:10 +00:00
2019-08-19 09:30:24 +00:00
$result = $this->cartItemRepository->delete($itemId);
2018-11-17 06:26:10 +00:00
2019-08-19 09:30:24 +00:00
if (! $cart->items()->count())
$this->cartRepository->delete($cart->id);
2018-11-30 07:40:41 +00:00
2019-08-19 09:30:24 +00:00
$this->collectTotals();
2018-11-16 12:58:41 +00:00
2019-08-19 09:30:24 +00:00
return true;
2018-10-26 07:05:19 +00:00
}
2018-09-11 11:19:40 +00:00
}