|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Webkul\PWA; |
| 4 | + |
| 5 | +use Webkul\Checkout\Repositories\CartRepository; |
| 6 | +use Webkul\Checkout\Repositories\CartItemRepository; |
| 7 | +use Webkul\Checkout\Repositories\CartAddressRepository; |
| 8 | +use Webkul\Customer\Repositories\CustomerRepository; |
| 9 | +use Webkul\Product\Repositories\ProductRepository; |
| 10 | +use Webkul\Tax\Repositories\TaxCategoryRepository; |
| 11 | +use Webkul\Checkout\Models\CartPayment; |
| 12 | +use Webkul\Customer\Repositories\WishlistRepository; |
| 13 | +use Webkul\Customer\Repositories\CustomerAddressRepository; |
| 14 | +use Webkul\Product\Helpers\Price; |
| 15 | +use Webkul\Checkout\Cart as BaseCart; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class Cart. |
| 19 | + * |
| 20 | + */ |
| 21 | +class Cart extends BaseCart |
| 22 | +{ |
| 23 | + |
| 24 | + /** |
| 25 | + * CartRepository instance |
| 26 | + * |
| 27 | + * @var mixed |
| 28 | + */ |
| 29 | + protected $cart; |
| 30 | + |
| 31 | + /** |
| 32 | + * CartItemRepository instance |
| 33 | + * |
| 34 | + * @var mixed |
| 35 | + */ |
| 36 | + protected $cartItem; |
| 37 | + |
| 38 | + /** |
| 39 | + * CustomerRepository instance |
| 40 | + * |
| 41 | + * @var mixed |
| 42 | + */ |
| 43 | + protected $customer; |
| 44 | + |
| 45 | + /** |
| 46 | + * CartAddressRepository instance |
| 47 | + * |
| 48 | + * @var mixed |
| 49 | + */ |
| 50 | + protected $cartAddress; |
| 51 | + |
| 52 | + /** |
| 53 | + * ProductRepository instance |
| 54 | + * |
| 55 | + * @var mixed |
| 56 | + */ |
| 57 | + protected $product; |
| 58 | + |
| 59 | + /** |
| 60 | + * TaxCategoryRepository instance |
| 61 | + * |
| 62 | + * @var mixed |
| 63 | + */ |
| 64 | + protected $taxCategory; |
| 65 | + |
| 66 | + /** |
| 67 | + * WishlistRepository instance |
| 68 | + * |
| 69 | + * @var mixed |
| 70 | + */ |
| 71 | + protected $wishlist; |
| 72 | + |
| 73 | + /** |
| 74 | + * CustomerAddressRepository instance |
| 75 | + * |
| 76 | + * @var mixed |
| 77 | + */ |
| 78 | + protected $customerAddress; |
| 79 | + |
| 80 | + /** |
| 81 | + * Suppress the session flash messages |
| 82 | + */ |
| 83 | + protected $suppressFlash; |
| 84 | + |
| 85 | + /** |
| 86 | + * Product price helper instance |
| 87 | + */ |
| 88 | + protected $price; |
| 89 | + |
| 90 | + /** |
| 91 | + * Create a new controller instance. |
| 92 | + * |
| 93 | + * @param Webkul\Checkout\Repositories\CartRepository $cart |
| 94 | + * @param Webkul\Checkout\Repositories\CartItemRepository $cartItem |
| 95 | + * @param Webkul\Checkout\Repositories\CartAddressRepository $cartAddress |
| 96 | + * @param Webkul\Customer\Repositories\CustomerRepository $customer |
| 97 | + * @param Webkul\Product\Repositories\ProductRepository $product |
| 98 | + * @param Webkul\Product\Repositories\TaxCategoryRepository $taxCategory |
| 99 | + * @param Webkul\Product\Repositories\CustomerAddressRepository $customerAddress |
| 100 | + * @param Webkul\Product\Repositories\CustomerAddressRepository $customerAddress |
| 101 | + * @param Webkul\Discount\Repositories\CartRuleRepository $cartRule |
| 102 | + * @param Webkul\Helpers\Discount $discount |
| 103 | + * @return void |
| 104 | + */ |
| 105 | + public function __construct( |
| 106 | + CartRepository $cart, |
| 107 | + CartItemRepository $cartItem, |
| 108 | + CartAddressRepository $cartAddress, |
| 109 | + CustomerRepository $customer, |
| 110 | + ProductRepository $product, |
| 111 | + TaxCategoryRepository $taxCategory, |
| 112 | + WishlistRepository $wishlist, |
| 113 | + CustomerAddressRepository $customerAddress, |
| 114 | + Price $price |
| 115 | + ) |
| 116 | + { |
| 117 | + parent::__construct( |
| 118 | + $cart, |
| 119 | + $cartItem, |
| 120 | + $cartAddress, |
| 121 | + $customer, |
| 122 | + $product, |
| 123 | + $taxCategory, |
| 124 | + $wishlist, |
| 125 | + $customerAddress, |
| 126 | + $price |
| 127 | + ); |
| 128 | + |
| 129 | + $this->product = $product; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Save customer address |
| 134 | + * |
| 135 | + * @return Mixed |
| 136 | + */ |
| 137 | + public function saveCustomerAddress($data) |
| 138 | + { |
| 139 | + if (! $cart = $this->getCart()) |
| 140 | + return false; |
| 141 | + |
| 142 | + $billingAddress = $data['billing']; |
| 143 | + $shippingAddress = $data['shipping']; |
| 144 | + $billingAddress['cart_id'] = $shippingAddress['cart_id'] = $cart->id; |
| 145 | + |
| 146 | + if (isset($data['billing']['address_id']) && $data['billing']['address_id']) { |
| 147 | + $address = $this->customerAddress->findOneWhere(['id'=> $data['billing']['address_id']])->toArray(); |
| 148 | + $billingAddress['first_name'] = $this->getCurrentCustomer()->user()->first_name; |
| 149 | + $billingAddress['last_name'] = $this->getCurrentCustomer()->user()->last_name; |
| 150 | + $billingAddress['email'] = $this->getCurrentCustomer()->user()->email; |
| 151 | + $billingAddress['address1'] = $address['address1']; |
| 152 | + $billingAddress['country'] = $address['country']; |
| 153 | + $billingAddress['state'] = $address['state']; |
| 154 | + $billingAddress['city'] = $address['city']; |
| 155 | + $billingAddress['postcode'] = $address['postcode']; |
| 156 | + $billingAddress['phone'] = $address['phone']; |
| 157 | + } |
| 158 | + |
| 159 | + if (isset($data['shipping']['address_id']) && $data['shipping']['address_id']) { |
| 160 | + $address = $this->customerAddress->findOneWhere(['id'=> $data['shipping']['address_id']])->toArray(); |
| 161 | + $shippingAddress['first_name'] = $this->getCurrentCustomer()->user()->first_name; |
| 162 | + $shippingAddress['last_name'] = $this->getCurrentCustomer()->user()->last_name; |
| 163 | + $shippingAddress['email'] = $this->getCurrentCustomer()->user()->email; |
| 164 | + $shippingAddress['address1'] = $address['address1']; |
| 165 | + $shippingAddress['country'] = $address['country']; |
| 166 | + $shippingAddress['state'] = $address['state']; |
| 167 | + $shippingAddress['city'] = $address['city']; |
| 168 | + $shippingAddress['postcode'] = $address['postcode']; |
| 169 | + $shippingAddress['phone'] = $address['phone']; |
| 170 | + } |
| 171 | + |
| 172 | + if (isset($data['billing']['save_as_address']) && $data['billing']['save_as_address'] && ! isset($data['billing']['address_id'])) { |
| 173 | + $billingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id; |
| 174 | + $this->customerAddress->create($billingAddress); |
| 175 | + } |
| 176 | + |
| 177 | + if (isset($data['shipping']['save_as_address']) && $data['shipping']['save_as_address']) { |
| 178 | + $shippingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id; |
| 179 | + $this->customerAddress->create($shippingAddress); |
| 180 | + } |
| 181 | + |
| 182 | + if ($billingAddressModel = $cart->billing_address) { |
| 183 | + $this->cartAddress->update($billingAddress, $billingAddressModel->id); |
| 184 | + |
| 185 | + if ($shippingAddressModel = $cart->shipping_address) { |
| 186 | + if (isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) { |
| 187 | + $this->cartAddress->update($billingAddress, $shippingAddressModel->id); |
| 188 | + } else { |
| 189 | + $this->cartAddress->update($shippingAddress, $shippingAddressModel->id); |
| 190 | + } |
| 191 | + } else { |
| 192 | + if (isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) { |
| 193 | + $this->cartAddress->create(array_merge($billingAddress, ['address_type' => 'shipping'])); |
| 194 | + } else { |
| 195 | + $this->cartAddress->create(array_merge($shippingAddress, ['address_type' => 'shipping'])); |
| 196 | + } |
| 197 | + } |
| 198 | + } else { |
| 199 | + $this->cartAddress->create(array_merge($billingAddress, ['address_type' => 'billing'])); |
| 200 | + |
| 201 | + if (isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) { |
| 202 | + $this->cartAddress->create(array_merge($billingAddress, ['address_type' => 'shipping'])); |
| 203 | + } else { |
| 204 | + $this->cartAddress->create(array_merge($shippingAddress, ['address_type' => 'shipping'])); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + if ($this->getCurrentCustomer()->check()) { |
| 209 | + $cart->customer_email = $this->getCurrentCustomer()->user()->email; |
| 210 | + $cart->customer_first_name = $this->getCurrentCustomer()->user()->first_name; |
| 211 | + $cart->customer_last_name = $this->getCurrentCustomer()->user()->last_name; |
| 212 | + } else { |
| 213 | + $cart->customer_email = $cart->billing_address->email; |
| 214 | + $cart->customer_first_name = $cart->billing_address->first_name; |
| 215 | + $cart->customer_last_name = $cart->billing_address->last_name; |
| 216 | + } |
| 217 | + |
| 218 | + $cart->save(); |
| 219 | + |
| 220 | + return true; |
| 221 | + } |
| 222 | +} |
0 commit comments