Skip to content

Commit

Permalink
Improvements to the add wishlist to cart
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Nov 5, 2024
1 parent d91a6fc commit 386c434
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
31 changes: 22 additions & 9 deletions src/Controller/WishlistController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twig\Environment;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -170,19 +171,31 @@ public function addToCart(
throw new NotFoundHttpException(sprintf('Wishlist with id %s not found', $uuid));
}

$cart = $cartContext->getCart();
Assert::isInstanceOf($cart, OrderInterface::class);
try {
$cart = $cartContext->getCart();
Assert::isInstanceOf($cart, OrderInterface::class);

foreach ($wishlist->getItems() as $item) {
$cartItem = $this->cartItemFactory->createForCart($cart);
$cartItem->setVariant($item->getVariant());
foreach ($wishlist->getItems() as $item) {
if ($item->getVariant() === null) {
throw new \RuntimeException(sprintf('You have not selected a variant for the product "%s"', $item->getProduct()?->getName()));

Check failure on line 180 in src/Controller/WishlistController.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: highest | SF~6.4.0)

PossiblyNullArgument

src/Controller/WishlistController.php:180:113: PossiblyNullArgument: Argument 2 of sprintf cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 180 in src/Controller/WishlistController.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.3 | Deps: lowest | SF~6.4.0)

PossiblyNullArgument

src/Controller/WishlistController.php:180:113: PossiblyNullArgument: Argument 2 of sprintf cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 180 in src/Controller/WishlistController.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.3 | Deps: highest | SF~6.4.0)

PossiblyNullArgument

src/Controller/WishlistController.php:180:113: PossiblyNullArgument: Argument 2 of sprintf cannot be null, possibly null value provided (see https://psalm.dev/078)
}

$orderItemQuantityModifier->modify($cartItem, $item->getQuantity());
$cartItem = $this->cartItemFactory->createForCart($cart);
$cartItem->setVariant($item->getVariant());

$orderModifier->addToOrder($cart, $cartItem);
}
$orderItemQuantityModifier->modify($cartItem, $item->getQuantity());

$orderModifier->addToOrder($cart, $cartItem);
}

$this->getManager($cart)->flush();
$this->getManager($cart)->persist($cart);
$this->getManager($cart)->flush();
} catch (\Throwable $e) {
$session = $request->getSession();
if ($session instanceof FlashBagAwareSessionInterface) {
$session->getFlashBag()->add('error', $e->getMessage());
}
}

return new RedirectResponse($request->headers->get('referer'));

Check failure on line 200 in src/Controller/WishlistController.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: highest | SF~6.4.0)

PossiblyNullArgument

src/Controller/WishlistController.php:200:37: PossiblyNullArgument: Argument 1 of Symfony\Component\HttpFoundation\RedirectResponse::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 200 in src/Controller/WishlistController.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.3 | Deps: lowest | SF~6.4.0)

PossiblyNullArgument

src/Controller/WishlistController.php:200:37: PossiblyNullArgument: Argument 1 of Symfony\Component\HttpFoundation\RedirectResponse::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 200 in src/Controller/WishlistController.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.3 | Deps: highest | SF~6.4.0)

PossiblyNullArgument

src/Controller/WishlistController.php:200:37: PossiblyNullArgument: Argument 1 of Symfony\Component\HttpFoundation\RedirectResponse::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)
}
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
setono_sylius_wishlist:
ui:
add_wishlist_to_cart: Add wishlist to cart
copy: Copy
wishlist_header: "Wishlist: %name%"
wishlist_subheader: Here you can edit the quantity of the products in your wishlist. You can also select the variants you want and remove products from your wishlist. Finally you can share your wishlist with others by copying the URL directly.
wishlist_subheader: Here you can edit the quantity of the products in your wishlist. You can also select the variants you want and remove products from your wishlist. Finally you can share your wishlist with others by copying the URL below.
wishlists: Wishlists
wishlists_header: Wishlists
wishlists_subheader: Here you can manage your wishlists. As a logged in user you can create, edit and delete wishlists. You can also add and remove products from your wishlists. If you're not logged in, you can only add and remove products from your single wishlist.
Expand Down
42 changes: 40 additions & 2 deletions src/Resources/views/shop/wishlist/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@
<div class="sub header">{{ 'setono_sylius_wishlist.ui.wishlist_subheader'|trans }}</div>
</h1>

<div class="wishlist-url">
<input type="text" value="{{ url('setono_sylius_wishlist_shop_wishlist_show', { 'uuid' : wishlist.uuid }) }}">
<button class="ui icon labeled button" type="button">
<i class="copy icon"></i>
{{ 'setono_sylius_wishlist.ui.copy'|trans }}
</button>
</div>

<div>
<a href="{{ path('setono_sylius_wishlist_shop_wishlist_add_to_cart', { 'uuid' : wishlist.uuid }) }}">Add all to cart</a>
<a href="{{ path('setono_sylius_wishlist_shop_wishlist_add_to_cart', { 'uuid' : wishlist.uuid }) }}">{{ 'setono_sylius_wishlist.ui.add_wishlist_to_cart'|trans }}</a>
</div>

<table class="ui table">
Expand Down Expand Up @@ -80,7 +88,7 @@
<style>
h1 {
max-width: 800px !important;
margin: 50px auto 120px auto !important;
margin: 50px auto 50px auto !important;
text-align: center !important;
border: 0 !important;
}
Expand All @@ -89,5 +97,35 @@
margin-top: 20px !important;
line-height: 1.7 !important;
}
.wishlist-url {
margin: 0 auto 40px auto !important;
max-width: 800px !important;
display: flex;
flex-wrap: nowrap;
gap: 20px;
}
</style>
{% endblock %}

{% block javascripts %}
{{ parent() }}

<script>
document.querySelector('.wishlist-url button').addEventListener('click', function() {
const input = document.querySelector(".wishlist-url input");
input.select();
input.setSelectionRange(0, 99999); // For mobile devices
navigator.clipboard.writeText(input.value)
.then(() => {
// Optional: Notify the user that the text has been copied
alert("Copied to clipboard: " + input.value);
})
.catch((error) => {
console.error("Could not copy text: ", error);
});
});
</script>
{% endblock %}

0 comments on commit 386c434

Please sign in to comment.