diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index ae14b98ee..a117e0310 100644 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -159,9 +159,12 @@ class Cart { * * @return void */ - public function add($id, $data) + public function add($id, $data, $prepared = false, $preparedData = []) { - $itemData = $this->prepareItemData($id, $data); + if($prepared == false) + $itemData = $this->prepareItemData($id, $data); + else + $itemData = $preparedData; if(!$itemData) { return false; @@ -953,17 +956,78 @@ class Cart { * Move a wishlist item to cart */ public function moveToCart($productId) { - $data = [ - 'product' => $productId, - 'quantity' => 1, - ]; + $product = $this->product->find($productId); - $result = $this->add($productId, $data); + if($product->configurable == null) { + $data = [ + 'product' => $productId, + 'quantity' => 1, + ]; - if($result instanceof Collection || $result == true) { - return true; + $result = $this->add($productId, $data); + + if($result instanceof Collection || $result == true) { + return true; + } else { + return false; + } } else { - return false; + //in case the product added is a configurable product. + $result = $this->moveConfigurableFromWishlistToCart($product->parent_id, $product->id); + + if(is_array($result)) { + $data['quantity'] = 1; + + $data['selected_configurable_option'] = $product->parent_id; + + $moved = $this->add($data['selected_configurable_option'], $data, true, $result); + + if($moved) { + return true; + } else { + return false; + } + } } } + + /** + * Move a configurable product from wishlist to cart. + * + * @return mixed + */ + public function moveConfigurableFromWishlistToCart($configurableproductId, $productId) { + $product = $this->find($configurableproductId); + + $child = $childData = null; + if($product->type == 'configurable') { + $child = $this->product->findOneByField('id', $productId); + + $childData = [ + 'product_id' => $data['selected_configurable_option'], + 'sku' => $child->sku, + 'name' => $child->name, + 'type' => 'simple' + ]; + } + + $price = ($product->type == 'configurable' ? $child->price : $product->price); + + $parentData = [ + 'sku' => $product->sku, + 'product_id' => $productId, + 'quantity' => $data['quantity'], + 'type' => $product->type, + 'name' => $product->name, + 'price' => core()->convertPrice($price), + 'base_price' => $price, + 'total' => core()->convertPrice($price * $data['quantity']), + 'base_total' => $price * $data['quantity'], + 'weight' => $weight = ($product->type == 'configurable' ? $child->weight : $product->weight), + 'total_weight' => $weight * $data['quantity'], + 'base_total_weight' => $weight * $data['quantity'] + ]; + + return ['parent' => $parentData, 'child' => $childData]; + } } \ No newline at end of file diff --git a/packages/Webkul/Customer/src/Http/Controllers/WishlistController.php b/packages/Webkul/Customer/src/Http/Controllers/WishlistController.php index 600431d22..0aa2cab58 100644 --- a/packages/Webkul/Customer/src/Http/Controllers/WishlistController.php +++ b/packages/Webkul/Customer/src/Http/Controllers/WishlistController.php @@ -167,4 +167,4 @@ class WishlistController extends Controller return redirect()->back(); } } -} +} \ No newline at end of file