Skip to content

Commit

Permalink
feat: add route to create cart from wishlist (#31)
Browse files Browse the repository at this point in the history
* feat: add is_type on wishlist

* feat: add is_type on wishlist and correction of bug in clone and dupplicate service

* feat: add route to create cart from wishlist

---------

Co-authored-by: Bruno Payet <[email protected]>
  • Loading branch information
BrunoPYT and Bruno Payet authored Nov 22, 2024
1 parent 2914a93 commit 4f23d15
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>2.2.0</version>
<version>2.2.1</version>
<authors>
<author>
<name>Michaël Espeche</name>
Expand Down
31 changes: 26 additions & 5 deletions Controller/Front/Api/WishListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@
use OpenApi\Service\OpenApiService;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

use Symfony\Component\HttpFoundation\Request;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\HttpFoundation\JsonResponse;
use Thelia\Core\Security\SecurityContext;
use Thelia\Model\ProductQuery;
use TheliaSmarty\Template\Plugins\Security;
use WishList\Model\WishList;
use WishList\Model\WishListQuery;
use WishList\Service\WishListService;

Expand Down Expand Up @@ -645,6 +640,32 @@ public function addToCart($wishListId, WishListService $wishListService)
return new JsonResponse();
}

/**
* @Route("/cart/from-wishlist/{wishListId}", name="cart_from_wishlist", methods="POST")
* @OA\Post(
* path="/wishlist/cart/from-wishlist/{wishListId}",
* tags={"WishList"},
* summary="Create cart from wishlist",
* @OA\Parameter(
* in="path",
* name="wishListId",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response="200",
* description="Success"
* )
* )
*/
public function createCartFromWishlist($wishListId, WishListService $wishListService): JsonResponse
{
$wishListService->createCartFromWishlist($wishListId);

return new JsonResponse();
}

protected function getOpenApiWishList($wishListId, ModelFactory $modelFactory, WishListService $wishListService)
{
$wishList = $wishListService->getWishList($wishListId);
Expand Down
48 changes: 33 additions & 15 deletions Service/WishListService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace WishList\Service;

use Cocur\Slugify\Slugify;
use http\Exception;
use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down Expand Up @@ -300,20 +299,39 @@ public function addWishListToCart($wishListId): void
$wishList = $this->getWishListObject($wishListId, $customerId, $sessionId);

if (null !== $wishList) {
$cart = $this->requestStack->getCurrentRequest()?->getSession()->getSessionCart($this->eventDispatcher);

foreach ($wishList->getWishListProducts() as $wishListProduct) {
$event = new CartEvent($cart);
$event
->setProduct($wishListProduct->getProductSaleElements()->getProductId())
->setProductSaleElementsId($wishListProduct->getProductSaleElementsId())
->setQuantity($wishListProduct->getQuantity())
->setAppend(true)
->setNewness(true)
;

$this->eventDispatcher->dispatch($event, TheliaEvents::CART_ADDITEM);
}
$this->addWishlistProductsToCart($wishList);
}
}

public function createCartFromWishlist($wishListId): void
{
[$customerId, $sessionId] = $this->getCurrentUserOrSession();

$wishList = $this->getWishListObject($wishListId, $customerId, $sessionId);

if (null !== $wishList) {
// Store a new empty cart in the session.
$this->requestStack->getCurrentRequest()?->getSession()->clearSessionCart($this->eventDispatcher);

$this->addWishlistProductsToCart($wishList);
}
}

private function addWishlistProductsToCart(WishList $wishList): void
{
$cart = $this->requestStack->getCurrentRequest()?->getSession()->getSessionCart($this->eventDispatcher);

foreach ($wishList->getWishListProducts() as $wishListProduct) {
$event = new CartEvent($cart);
$event
->setProduct($wishListProduct->getProductSaleElements()->getProductId())
->setProductSaleElementsId($wishListProduct->getProductSaleElementsId())
->setQuantity($wishListProduct->getQuantity())
->setAppend(true)
->setNewness(true)
;

$this->eventDispatcher->dispatch($event, TheliaEvents::CART_ADDITEM);
}
}

Expand Down

0 comments on commit 4f23d15

Please sign in to comment.