How can I redirect to the cart page programmatically?
문제
I'm using this plugin in order to prevent a product be added more than one time and it works perfectly but I'd like to add a feature: instead to stay on the product page once the product is added more than one time, I'd like to check if the backend option Stores->Configuration->Sales->Checkout->Shopping Cart->After Adding a Product Redirect to Shopping Cart
is enabled and redirect if it is.
di.xml
<type name="Magento\Checkout\Model\Cart">
<plugin disabled="false" name="Vendor_Module_Plugin_Magento_Checkout_Cart_BeforeAddToCart" type="Vendor\Module\Plugin\Magento\Checkout\Cart\BeforeAddToCart"/>
</type>
vendor/module/Plugin/Magento/Checkout/Cart/BeforeAddToCart.php
<?php
declare(strict_types=1);
namespace Vendor\Module\Plugin\Magento\Checkout\Cart;
use Magento\Checkout\Model\Cart;
use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\Session\Proxy as SessionProxy;
use Magento\Framework\Message\ManagerInterface;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
class BeforeAddToCart {
protected $messageManager;
protected $cartSession;
protected $configurableProduct;
public function __construct(
Configurable $configurableProduct,
ManagerInterface $messageManager,
SessionProxy $cartSession
) {
$this->messageManager = $messageManager;
$this->cartSession = $cartSession;
$this->configurableProduct = $configurableProduct;
}
public function beforeAddProduct(Cart $subject, $productInfo, $requestInfo=null)
{
$enableProductCartControl=true;
$product = null;
$parentProduct=null;
if ($productInfo instanceof Product) {
$product = $productInfo;
if (!$product->getId()) {
throw new \Magento\Framework\Exception\LocalizedException(
__("The product wasn't found. Verify the product and try again.")
);
}
}
if ($product)
{
if ($product->getTypeId()==='configurable')
{
if (isset($requestInfo['super_attribute']))
{
$parentProduct=$product;
$childProduct = $this->configurableProduct->getProductByAttributes($requestInfo['super_attribute'] ,$product);
// change $product to child
$product=$childProduct;
}
}
if ($product->getTypeId()==='grouped')
{
if (isset($requestInfo['super_group']))
{
$parentProduct=$product;
//$childProduct = // get child from grouped...
// change $product to child
//$product=$childProduct;
}
}
if ($product && $enableProductCartControl)
{
// check for existence of product in cart...
//
if($this->cartSession->getQuote()->hasProductId($product->getId())){
throw new \Magento\Framework\Exception\LocalizedException(
__("This product is already in the cart. Testing, testing : ". $product->getSku())
);
}
}
}
return [$productInfo, $requestInfo];
}
/**
* Get request for product add to cart procedure
*
* @param \Magento\Framework\DataObject|int|array $requestInfo
* @return \Magento\Framework\DataObject
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function _getProductRequest($requestInfo)
{
if ($requestInfo instanceof \Magento\Framework\DataObject) {
$request = $requestInfo;
} elseif (is_numeric($requestInfo)) {
$request = new \Magento\Framework\DataObject(['qty' => $requestInfo]);
} elseif (is_array($requestInfo)) {
$request = new \Magento\Framework\DataObject($requestInfo);
} else {
throw new \Magento\Framework\Exception\LocalizedException(
__('We found an invalid request for adding product to quote.')
);
}
return $request;
}
}
해결책
In this case the plugin is working with Magento\Checkout\Model\Cart
you can simply set a redirect url in the session
$this->session->setRedirectUrl($this->url->getUrl('checkout/cart/index'));
Inject the following classes
Magento\Checkout\Model\Session $session
Magento\Framework\UrlInterface $url
다른 팁
- You can use the
vendor/magento/module-checkout/Helper/Cart.php
helper's methodgetShouldRedirectToCart()
to check if your configuration is enabled. - You can use the same helper's
getCartUrl()
method to retrieve the URL of the cart page. - Inject in your plugin constructor the
\Magento\Framework\Controller\Result\RedirectFactory
class and store it in a private property called$resultFactory
, then use it like this to redirect to cart the desirec URL (just store the cart url in the $redirectUrl variable):
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($redirectUrl);
제휴하지 않습니다 magento.stackexchange