Skip to content

Commit

Permalink
[NamedCarts] rewrite selector for lists
Browse files Browse the repository at this point in the history
  • Loading branch information
dpfaffenbauer committed May 10, 2024
1 parent a3cf299 commit 7ecf153
Show file tree
Hide file tree
Showing 33 changed files with 370 additions and 577 deletions.
89 changes: 88 additions & 1 deletion src/CoreShop/Bundle/FrontendBundle/Controller/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@
use CoreShop\Bundle\OrderBundle\DTO\AddToCartInterface;
use CoreShop\Bundle\OrderBundle\Factory\AddToCartFactoryInterface;
use CoreShop\Bundle\OrderBundle\Form\Type\AddToCartType;
use CoreShop\Bundle\OrderBundle\Form\Type\CartListChoiceType;
use CoreShop\Bundle\OrderBundle\Form\Type\CartListType;
use CoreShop\Bundle\OrderBundle\Form\Type\CartType;
use CoreShop\Bundle\OrderBundle\Form\Type\CreatedNamedCartType;
use CoreShop\Bundle\OrderBundle\Form\Type\ShippingCalculatorType;
use CoreShop\Bundle\ResourceBundle\Pimcore\Repository\StackRepositoryInterface;
use CoreShop\Bundle\StorageListBundle\Form\Type\StorageListChoiceType;
use CoreShop\Bundle\WorkflowBundle\Manager\StateMachineManagerInterface;
use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Context\ShopperContextInterface;
use CoreShop\Component\Core\Currency\CurrencyStorageInterface;
use CoreShop\Component\Core\Order\Modifier\CartItemQuantityModifier;
use CoreShop\Component\Order\Cart\CartModifierInterface;
use CoreShop\Component\Order\Cart\Rule\CartPriceRuleProcessorInterface;
Expand All @@ -42,28 +48,105 @@
use CoreShop\Component\Resource\Repository\RepositoryInterface;
use CoreShop\Component\Shipping\Calculator\TaxedShippingCalculatorInterface;
use CoreShop\Component\Shipping\Resolver\CarriersResolverInterface;
use CoreShop\Component\StorageList\Factory\StorageListFactory;
use CoreShop\Component\StorageList\Provider\ContextProviderInterface;
use CoreShop\Component\StorageList\Storage\StorageListStorageInterface;
use CoreShop\Component\StorageList\StorageListItemQuantityModifierInterface;
use CoreShop\Component\Store\Context\StoreContextInterface;
use CoreShop\Component\Tracking\Tracker\TrackerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Service\Attribute\SubscribedService;

class CartController extends FrontendController
{
public function widgetAction(Request $request): Response
public function widgetAction(Request $request, ShopperContextInterface $shopperContext): Response
{
$form = $this->container->get('form.factory')->createNamed('coreshop', CartListType::class, ['cart' => $this->getCart()], [
'context' => $shopperContext->getContext(),
]);

return $this->render($this->getTemplateConfigurator()->findTemplate('Cart/_widget.html'), [
'cart' => $this->getCart(),
'form' => $form->createView(),
]);
}

public function createNamedCartAction(Request $request, ShopperContextInterface $shopperContext)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');

$form = $this->container->get('form.factory')->createNamed('coreshop', CreatedNamedCartType::class);

if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'])) {
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$storageList = $this->container->get('coreshop.factory.order')->createNewNamed($data['name']);

$this->container->get('coreshop.storage_list.context_provider.order')->provideContextForStorageList($storageList);

$this->getCartManager()->persistCart($storageList);

$storageListStorage = $this->container->get('coreshop.storage_list.storage.order');
$storageListStorage->setForContext($shopperContext->getContext(), $storageList);

$this->addFlash('success', $this->container->get('translator')->trans('coreshop.ui.cart_added'));

if ($request->isXmlHttpRequest()) {
return new JsonResponse([
'success' => true,
]);
}

return $this->redirect($request->getUri());
}
}

return $this->render($this->getTemplateConfigurator()->findTemplate('Cart/created_named.html'), [
'form' => $form->createView(),
]);
}

public function selectNamedCartAction(Request $request, ShopperContextInterface $shopperContext)
{
$this->denyAccessUnlessGranted('CORESHOP_CURRENCY_SWITCH');

$form = $this->container->get('form.factory')->createNamed('coreshop', CartListType::class, ['cart' => $this->getCart()->getId()], [
'context' => $shopperContext->getContext(),
]);

if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'])) {
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$cart = $form->getData()['cart'];

if ($cart->getCustomer()?->getUser()?->getId() !== $this->getUser()?->getId()) {
throw new AccessDeniedException();
}

$storageListStorage = $this->container->get('coreshop.storage_list.storage.order');
$storageListStorage->setForContext($shopperContext->getContext(), $cart);

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


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

public function createQuoteAction(Request $request, StateMachineManagerInterface $machineManager)
{
$this->denyAccessUnlessGranted('CORESHOP_QUOTE_CREATE');
Expand Down Expand Up @@ -393,6 +476,7 @@ public static function getSubscribedServices(): array
[
new SubscribedService('coreshop.repository.stack.purchasable', StackRepositoryInterface::class, attributes: new Autowire(service: 'coreshop.repository.stack.purchasable')),
new SubscribedService('coreshop.factory.order_item', OrderItemFactoryInterface::class, attributes: new Autowire(service: 'coreshop.factory.order_item')),
new SubscribedService('coreshop.factory.order', StorageListFactory::class, attributes: new Autowire(service: 'coreshop.factory.order')),
new SubscribedService('coreshop.repository.order_item', RepositoryInterface::class, attributes: new Autowire(service: 'coreshop.repository.order_item')),
new SubscribedService(CartItemQuantityModifier::class, CartItemQuantityModifier::class),
new SubscribedService(AddToCartFactoryInterface::class, AddToCartFactoryInterface::class),
Expand All @@ -403,6 +487,9 @@ public static function getSubscribedServices(): array
new SubscribedService('coreshop.repository.cart_price_rule_voucher_code', CartPriceRuleVoucherRepositoryInterface::class),
new SubscribedService(CartPriceRuleProcessorInterface::class, CartPriceRuleProcessorInterface::class),
new SubscribedService(CartPriceRuleUnProcessorInterface::class, CartPriceRuleUnProcessorInterface::class),
new SubscribedService('coreshop.storage', CartPriceRuleUnProcessorInterface::class),
new SubscribedService('coreshop.storage_list.context_provider.order', ContextProviderInterface::class, attributes: new Autowire(service: 'coreshop.storage_list.context_provider.order')),
new SubscribedService('coreshop.storage_list.storage.order', StorageListStorageInterface::class, attributes: new Autowire(service: 'coreshop.storage_list.storage.order')),
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12286,7 +12286,7 @@ hr {
}

/* Cart Style Starts */
#cart .btn {
#cart > .btn {
color: #383838;
background: none;
border: 1px solid var(--primary);
Expand All @@ -12302,25 +12302,25 @@ hr {
box-shadow: none;
}

#cart .btn .fa-shopping-cart {
#cart > .btn .fa-shopping-cart {
color: var(--primary);
font-size: 22px;
margin-right: 10px;
vertical-align: top;
}

#cart .btn span {
#cart > .btn span {
color: #252a2f;
font-size: 16px;
text-transform: uppercase;
}

#cart .btn span#cart-total {
#cart > .btn span#cart-total {
color: var(--primary);
padding: 0 8px 0 5px;
}

#cart .btn i.fa-caret-down {
#car > .btn i.fa-caret-down {
color: var(--primary);
margin-left: 5px;
}
Expand Down Expand Up @@ -13923,7 +13923,7 @@ hr {
padding-top: 0.8em;
}

#cart .btn {
#cart > .btn {
color: var(--primary);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,99 @@
<span class="cart-total" {{ coreshop_test_html_attribute('cart-total') }}> {{ currency.convertAndFormat(cart.total) }}</span>
</span>
</button>
{{ form_start(form, {action: path('coreshop_cart_select')}) }}
<ul class="dropdown-menu pull-right" aria-labelledby="dropdownMenuButton">
<li>
{% if cart.hasItems %}
<table class="table hcart cart-items">
{% for item in cart.items %}
<tr>
<td class="text-center">
{% if item.product %}
<a href="{{ pimcore_object_path(item.product) }}">
{% if item.product and item.product.image is pimcore_asset_image %}
{{ item.product.image|pimcore_image_thumbnail_html('coreshop_productCartPreview', {'imgAttributes': {'class': 'img-thumbnail img-fluid'}, 'title': item.product.name, 'alt': item.product.name}) }}
{% endif %}
</a>
{% endif %}
</td>
<td class="text-left">
{% if item.product %}
<a href="{{ pimcore_object_path(item.product) }}">
{{ item.product.name }}
</a>
{% endif %}
{% for key, cart_form in form.cart %}
<tr>
{% set cart = form.cart.vars.choices[key].data %}

<td>
{{ form_widget(cart_form, coreshop_test_form_attribute('cart-select')|coreshop_merge_recursive({'label': false})) }}
</td>
<td class="text-right">x {{ item.quantity }}</td>
<td class="text-right">{{ currency.convertAndFormat(item.total) }}</td>
<td class="text-center">
{% if not item.isGiftItem %}
<a href="{{ path('coreshop_cart_remove', {cartItem: item.id|coreshop_string}) }}" class="removeFromCart" data-id="{{ item.id }}" data-refresh="true">
<i class="fa fa-times"></i>
</a>
{% endif %}
{{ cart.name ?: 'My Cart' }}
</td>
</tr>

<td>
{{ currency.convertAndFormat(cart.total) }}
</td>
</tr>
{% endfor %}
</table>
{% endif %}
</li>
<li>
<table class="table table-bordered total">
<tbody>
<tr>
<td class="text-right"><strong>{{ 'coreshop.ui.subtotal'|trans }}</strong></td>
<td class="text-left cart-subtotal">{{ currency.convertAndFormat(cart.subtotal) }}</td>
</tr>
<tr>
<td class="text-right"><strong>{{ 'coreshop.ui.total'|trans }}</strong></td>
<td class="text-left cart-total">{{ currency.convertAndFormat(cart.total) }}</td>
</tr>
</tbody>
</table>
<p class="text-right btn-block1">
{% if is_granted('CORESHOP_CART_SUMMARY') %}
<a href="{{ path('coreshop_cart_summary') }}">
{{ 'coreshop.ui.cart'|trans }}
</a>
{% endif %}

{% if is_granted('CORESHOP_CHECKOUT') %}
<a href="{{ path('coreshop_checkout', {'stepIdentifier': coreshop_checkout_steps_get_first()}) }}">
{{ 'coreshop.ui.checkout'|trans }}
</a>
{% endif %}
</p>
<button type="submit" class="btn btn-success w-100 mb-2">
Select as current Cart
</button>
<a href="{{ path('coreshop_cart_create_named') }}" class="btn btn-secondary w-100">
Create New Cart
</a>
</li>
{# <li>#}
{# {% if cart.hasItems %}#}
{# <table class="table hcart cart-items">#}
{# {% for item in cart.items %}#}
{# <tr>#}
{# <td class="text-center">#}
{# {% if item.product %}#}
{# <a href="{{ pimcore_object_path(item.product) }}">#}
{# {% if item.product and item.product.image is pimcore_asset_image %}#}
{# {{ item.product.image|pimcore_image_thumbnail_html('coreshop_productCartPreview', {'imgAttributes': {'class': 'img-thumbnail img-fluid'}, 'title': item.product.name, 'alt': item.product.name}) }}#}
{# {% endif %}#}
{# </a>#}
{# {% endif %}#}
{# </td>#}
{# <td class="text-left">#}
{# {% if item.product %}#}
{# <a href="{{ pimcore_object_path(item.product) }}">#}
{# {{ item.product.name }}#}
{# </a>#}
{# {% endif %}#}
{# </td>#}
{# <td class="text-right">x {{ item.quantity }}</td>#}
{# <td class="text-right">{{ currency.convertAndFormat(item.total) }}</td>#}
{# <td class="text-center">#}
{# {% if not item.isGiftItem %}#}
{# <a href="{{ path('coreshop_cart_remove', {cartItem: item.id|coreshop_string}) }}" class="removeFromCart" data-id="{{ item.id }}" data-refresh="true">#}
{# <i class="fa fa-times"></i>#}
{# </a>#}
{# {% endif %}#}
{# </td>#}
{# </tr>#}
{# {% endfor %}#}
{# </table>#}
{# {% endif %}#}
{# </li>#}
{# <li>#}
{# <table class="table table-bordered total">#}
{# <tbody>#}
{# <tr>#}
{# <td class="text-right"><strong>{{ 'coreshop.ui.subtotal'|trans }}</strong></td>#}
{# <td class="text-left cart-subtotal">{{ currency.convertAndFormat(cart.subtotal) }}</td>#}
{# </tr>#}
{# <tr>#}
{# <td class="text-right"><strong>{{ 'coreshop.ui.total'|trans }}</strong></td>#}
{# <td class="text-left cart-total">{{ currency.convertAndFormat(cart.total) }}</td>#}
{# </tr>#}
{# </tbody>#}
{# </table>#}
{# <p class="text-right btn-block1">#}
{# {% if is_granted('CORESHOP_CART_SUMMARY') %}#}
{# <a href="{{ path('coreshop_cart_summary') }}">#}
{# {{ 'coreshop.ui.cart'|trans }}#}
{# </a>#}
{# {% endif %}#}

{# {% if is_granted('CORESHOP_CHECKOUT') %}#}
{# <a href="{{ path('coreshop_checkout', {'stepIdentifier': coreshop_checkout_steps_get_first()}) }}">#}
{# {{ 'coreshop.ui.checkout'|trans }}#}
{# </a>#}
{# {% endif %}#}
{# </p>#}
{# </li>#}
</ul>
{{ form_widget(form._token) }}
{{ form_end(form) }}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends '@CoreShopFrontend/layout.html.twig' %}
{% form_theme form 'bootstrap_4_layout.html.twig' %}

{% block content %}

{{ form_start(form) }}
{{ form_row(form.name, coreshop_test_form_attribute('cart-name')) }}

<div class="form-group">
<button type="submit" class="btn btn-success" {{ coreshop_test_html_attribute('cart-create-save') }} >
{{ 'coreshop.ui.save'|trans }}
</button>
</div>

{{ form_end(form) }}

{% endblock %}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7ecf153

Please sign in to comment.