Skip to content

Commit

Permalink
CC-13685: Updated product quantity and total in Mini Cart Widget from…
Browse files Browse the repository at this point in the history
… catalog AJAX add-to-cart action. (#2517)

Updated product quantity and total in Mini Cart Widget from catalog AJAX add-to-cart action.
  • Loading branch information
dmiseev authored Sep 4, 2024
1 parent d3ba5f6 commit 501a907
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/SprykerShop/Yves/CartPage/CartPageConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class CartPageConfig extends AbstractBundleConfig
*/
protected const IS_CART_ACTIONS_ASYNC_MODE_ENABLED = false;

/**
* @var string|null
*/
protected const CART_BLOCK_MINI_CART_VIEW_TEMPLATE_PATH = null;

/**
* Specification:
* - Enables loading the cart items via AJAX.
Expand Down Expand Up @@ -78,4 +83,17 @@ public function isCartActionsAsyncModeEnabled(): bool
{
return static::IS_CART_ACTIONS_ASYNC_MODE_ENABLED;
}

/**
* Specification:
* - Returns the template path for the cart block mini cart view.
*
* @api
*
* @return string|null
*/
public function getCartBlockMiniCartViewTemplatePath(): ?string
{
return static::CART_BLOCK_MINI_CART_VIEW_TEMPLATE_PATH;
}
}
27 changes: 27 additions & 0 deletions src/SprykerShop/Yves/CartPage/CartPageDependencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ class CartPageDependencyProvider extends AbstractBundleDependencyProvider
*/
public const SERVICE_REQUEST_STACK = 'request_stack';

/**
* @var string
*/
public const TWIG_ENVIRONMENT = 'TWIG_ENVIRONMENT';

/**
* @uses \Spryker\Yves\Twig\Plugin\Application\TwigApplicationPlugin::SERVICE_TWIG
*
* @var string
*/
protected const SERVICE_TWIG = 'twig';

/**
* @var string
*/
Expand Down Expand Up @@ -142,6 +154,7 @@ public function provideDependencies(Container $container)
$container = $this->addRequestStack($container);
$container = $this->addUtilNumberService($container);
$container = $this->addAddToCartFormWidgetParameterExpanderPlugins($container);
$container = $this->addTwigService($container);

return $container;
}
Expand Down Expand Up @@ -430,4 +443,18 @@ protected function addUtilNumberService(Container $container): Container

return $container;
}

/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
protected function addTwigService(Container $container): Container
{
$container->set(static::TWIG_ENVIRONMENT, function (Container $container) {
return $container->getApplicationService(static::SERVICE_TWIG);
});

return $container;
}
}
22 changes: 22 additions & 0 deletions src/SprykerShop/Yves/CartPage/CartPageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use SprykerShop\Yves\CartPage\Dependency\Client\CartPageToQuoteClientInterface;
use SprykerShop\Yves\CartPage\Dependency\Client\CartPageToZedRequestClientInterface;
use SprykerShop\Yves\CartPage\Dependency\Service\CartPageToUtilNumberServiceInterface;
use SprykerShop\Yves\CartPage\Expander\MiniCartViewExpander;
use SprykerShop\Yves\CartPage\Expander\MiniCartViewExpanderInterface;
use SprykerShop\Yves\CartPage\Form\FormFactory;
use SprykerShop\Yves\CartPage\Handler\CartItemHandler;
use SprykerShop\Yves\CartPage\Mapper\CartItemsAttributeMapper;
Expand All @@ -26,6 +28,7 @@
use Symfony\Cmf\Component\Routing\ChainRouterInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Twig\Environment;

/**
* @method \SprykerShop\Yves\CartPage\CartPageConfig getConfig()
Expand Down Expand Up @@ -245,6 +248,17 @@ public function createCartPageFormFactory(): FormFactory
return new FormFactory();
}

/**
* @return \SprykerShop\Yves\CartPage\Expander\MiniCartViewExpanderInterface
*/
public function createMiniCartViewExpander(): MiniCartViewExpanderInterface
{
return new MiniCartViewExpander(
$this->getTwigEnvironment(),
$this->getConfig(),
);
}

/**
* @return \Symfony\Component\Security\Csrf\CsrfTokenManagerInterface
*/
Expand All @@ -268,4 +282,12 @@ public function getAddToCartFormWidgetParameterExpanderPlugins(): array
{
return $this->getProvidedDependency(CartPageDependencyProvider::PLUGINS_ADD_TO_CART_FORM_WIDGET_PARAMETER_EXPANDER);
}

/**
* @return \Twig\Environment
*/
public function getTwigEnvironment(): Environment
{
return $this->getProvidedDependency(CartPageDependencyProvider::TWIG_ENVIRONMENT);
}
}
59 changes: 59 additions & 0 deletions src/SprykerShop/Yves/CartPage/Expander/MiniCartViewExpander.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerShop\Yves\CartPage\Expander;

use Generated\Shared\Transfer\MiniCartViewTransfer;
use Generated\Shared\Transfer\QuoteTransfer;
use SprykerShop\Yves\CartPage\CartPageConfig;
use Twig\Environment;

class MiniCartViewExpander implements MiniCartViewExpanderInterface
{
/**
* @var \Twig\Environment
*/
protected Environment $twigEnvironment;

/**
* @var \SprykerShop\Yves\CartPage\CartPageConfig
*/
protected CartPageConfig $cartPageConfig;

/**
* @param \Twig\Environment $twigEnvironment
* @param \SprykerShop\Yves\CartPage\CartPageConfig $cartPageConfig
*/
public function __construct(Environment $twigEnvironment, CartPageConfig $cartPageConfig)
{
$this->twigEnvironment = $twigEnvironment;
$this->cartPageConfig = $cartPageConfig;
}

/**
* @param \Generated\Shared\Transfer\MiniCartViewTransfer $miniCartViewTransfer
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
*
* @return \Generated\Shared\Transfer\MiniCartViewTransfer
*/
public function expand(
MiniCartViewTransfer $miniCartViewTransfer,
QuoteTransfer $quoteTransfer
): MiniCartViewTransfer {
$cartBlockMiniCartViewTemplatePath = $this->cartPageConfig->getCartBlockMiniCartViewTemplatePath();
if ($cartBlockMiniCartViewTemplatePath && !$miniCartViewTransfer->getCounterOnly()) {
$content = $this->twigEnvironment->render(
$cartBlockMiniCartViewTemplatePath,
['quote' => $quoteTransfer],
);

$miniCartViewTransfer->setContent($content);
}

return $miniCartViewTransfer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerShop\Yves\CartPage\Expander;

use Generated\Shared\Transfer\MiniCartViewTransfer;
use Generated\Shared\Transfer\QuoteTransfer;

interface MiniCartViewExpanderInterface
{
/**
* @param \Generated\Shared\Transfer\MiniCartViewTransfer $miniCartViewTransfer
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
*
* @return \Generated\Shared\Transfer\MiniCartViewTransfer
*/
public function expand(
MiniCartViewTransfer $miniCartViewTransfer,
QuoteTransfer $quoteTransfer
): MiniCartViewTransfer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerShop\Yves\CartPage\Plugin\CartPage;

use Generated\Shared\Transfer\MiniCartViewTransfer;
use Generated\Shared\Transfer\QuoteTransfer;
use Spryker\Yves\Kernel\AbstractPlugin;
use SprykerShop\Yves\CartPageExtension\Dependency\Plugin\MiniCartViewExpanderPluginInterface;

/**
* @method \SprykerShop\Yves\CartPage\CartPageConfig getConfig()
* @method \SprykerShop\Yves\CartPage\CartPageFactory getFactory()
*/
class CartBlockMiniCartViewExpanderPlugin extends AbstractPlugin implements MiniCartViewExpanderPluginInterface
{
/**
* {@inheritDoc}
* - Expands the provided `MiniCartView.content` with mini cart view.
*
* @api
*
* @param \Generated\Shared\Transfer\MiniCartViewTransfer $miniCartViewTransfer
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
*
* @return \Generated\Shared\Transfer\MiniCartViewTransfer
*/
public function expand(
MiniCartViewTransfer $miniCartViewTransfer,
QuoteTransfer $quoteTransfer
): MiniCartViewTransfer {
return $this->getFactory()
->createMiniCartViewExpander()
->expand($miniCartViewTransfer, $quoteTransfer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
productAbstract: _widget.productAbstract,
buttonClasses: 'button button--success button--expand',
buttonExtraClasses: '',
eventRevealer: null,
} %}

{% set extraClasses = 'spacing-left' %}
Expand All @@ -20,5 +21,8 @@
buttonExtraClasses: data.buttonExtraClasses,
extraAttributes: not addToCartSku ? {disabled: true},
},
attributes: {
'event-revealer': data.eventRevealer,
},
} only %}
{% endblock %}

0 comments on commit 501a907

Please sign in to comment.