magento2 – 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="MagentoCheckoutModelCart">
    <plugin disabled="false" name="Vendor_Module_Plugin_Magento_Checkout_Cart_BeforeAddToCart" type="VendorModulePluginMagentoCheckoutCartBeforeAddToCart"/>
</type>

vendor/module/Plugin/Magento/Checkout/Cart/BeforeAddToCart.php

<?php
declare(strict_types=1);

namespace VendorModulePluginMagentoCheckoutCart;
use MagentoCheckoutModelCart;
use MagentoCatalogModelProduct;
use MagentoCheckoutModelSessionProxy as SessionProxy;
use MagentoFrameworkMessageManagerInterface;
use MagentoConfigurableProductModelProductTypeConfigurable;

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 MagentoFrameworkExceptionLocalizedException(
                    __("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 MagentoFrameworkExceptionLocalizedException(
                            __("This product is already in the cart. Testing, testing : ". $product->getSku())
                        );
                    }

                }


        }

        return ($productInfo, $requestInfo);

    }

    /**
     * Get request for product add to cart procedure
     *
     * @param MagentoFrameworkDataObject|int|array $requestInfo
     * @return MagentoFrameworkDataObject
     * @throws MagentoFrameworkExceptionLocalizedException
     */
    private function _getProductRequest($requestInfo)
    {
        if ($requestInfo instanceof MagentoFrameworkDataObject) {
            $request = $requestInfo;
        } elseif (is_numeric($requestInfo)) {
            $request = new MagentoFrameworkDataObject(('qty' => $requestInfo));
        } elseif (is_array($requestInfo)) {
            $request = new MagentoFrameworkDataObject($requestInfo);
        } else {
            throw new MagentoFrameworkExceptionLocalizedException(
                __('We found an invalid request for adding product to quote.')
            );
        }
        return $request;
    }
}

magento2 – Only certain customer can save to cart?

I’m using magento 2.1. I have a strange problem where some customer cannot add product to cart. There are no error message. I had a feeling that it has to do with some sort of database issue because I in the admin panel I cannot save customer information.

However when I went straight to the mysql table customer_entity and delete that portion, the account can now add product to cart, however, obviously this is not a solution because now all the informtion such as address are now gone?

Has anyone else experience these issue or have a clue as to how to fix or work around this weird issue? thank you much!

woocommerce offtopic – Product Variations with Quantity and Add to Cart Buttonns

I am attempting to add an additional section to the content-single-product.php page that displays a table of the possible variations of the product. I am running into an issue with the quantity and add to cart button functionality. Is there a function or action that I can utilize that will allow for me to add the add to cart button with the quantity for each of the product variations? Thank you and advance

<form 
  action="<?php echo esc_url(get_permalink($product->ID)).'?add-to-cart='.$id.'&variation_id='.$prod_id.'$attribute_pa_size='.$prod_slug.'$quanitiy='.$_GET('qty'); ?>"  
  method="post" 
  class="variaitons__form">

  <input type='number' inputmode="numeric" class="qty" name="qty" value="1" />
  <button type="submit" class="btn btn--primary ajax_add_to_cart" name="go" value="Add To Cart" />
  <?php echo __('Add To Cart'); ?></button>
 </form>

woocommerce offtopic – Add your own text field next to the Add to Cart button in products page

I’m using leto woocommerce theme and product details page there add to cart -button is next to quantity -+ selection in default view.

I’m now wondering how I can place the Add to Cart button on its own line and add a “See more products” link-text field next to it. Can someone help me how I can do that?

This is how it is now:

enter image description here

This is what I would like it to look like:

enter image description here

session – Cart lost for logged in customer. Retrieved only after adding item

We run a B2B webshop in Magento 2.3.5 and customers have to log in to be able to add items to the cart (Amasty Hide Price-module). However we are experiencing problems with disappearing carts:

  1. Customer logs in
  2. Customer places several items in cart.
  3. Customer logs out / is logged out after some time.
  4. Customer logs back in: cart is empty.
  5. Customer places random item in cart: this item is in the cart together with the previously entered items.

I think it is a session / cookie issue but I can’t quite figure out what / why this is happening and how to solve it. Any help would be greatly appreciated!

magento2 – Magento 2: How to unset existing custom option and set new custom option and add product in cart programmatically

I am using Magento ver. 2.3.1, and I have products which have one custom option that is required.

Now I have create a custom controller to add product in cart without existing custom options as sample Order, when I add product in cart it give me error “The product’s required option(s) weren’t entered. Make sure the options are entered and try again”. So I want to unset the existing custom option and set my custom value as custom option and add product in cart.

I am using below code to add product in cart:

<?php
namespace VendorModuleControllerIndex;

use MagentoFrameworkAppActionContext;
use MagentoFrameworkDataFormFormKey;
use MagentoCheckoutModelCart as cartModel;
use MagentoCatalogModelProduct;
use MagentoFrameworkSerializeSerializerInterface;
use MagentoCatalogModelProductRepository;
use MagentoFrameworkControllerResultRedirectFactory; 
use MagentoFrameworkAppRequestHttp;
use MagentoCatalogModelProductOption;

class Cart extends MagentoFrameworkAppActionAction
{
    protected $formKey;   
    protected $cart;
    protected $product;

    protected $serializer;

    protected $_productRepository;

    protected $resultRedirectFactory;

    protected $request;

    protected $productOptions;


    public function __construct(
        Context $context,
        FormKey $formKey,
        cartModel $cart,
        Product $product,
        SerializerInterface $serializer,
        ProductRepository $productRepository,
        RedirectFactory $resultRedirectFactory,
        Http $request,
        Option $productOptions

        ) {
            $this->formKey = $formKey;
            $this->cart = $cart;
            $this->product = $product;
            $this->serializer = $serializer;
            $this->_productRepository = $productRepository;
            $this->resultRedirectFactory = $resultRedirectFactory;
            $this->request = $request;
            $this->productOptions = $productOptions;
            parent::__construct($context);
    }
    public function execute()
     { 
        $productId = $this->request->getParam('productid');
        
        $_product = $this->product->load($productId);

        $customOptionss = $this->productOptions->getProductOptionCollection($_product);        

        $additionalOptions() = array(
            'label' => "Is order Sample",
            'value' => "Yes",
        );

        $params = array(
                'form_key' => $this->formKey->getFormKey(),
                'product' => $productId,
                'qty' => 1,
            );

        $_product = $this->_productRepository->getById($productId);
        $_product->addCustomOption('additional_options', $this->serializer->serialize($additionalOptions));
        $customPrice = 0.0;
        $_product->setPrice($customPrice);
        $_product->setIsSuperMode(true);
        $this->cart->addProduct($_product, $params);
        $this->cart->save();

        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('checkout/cart');
        return $resultRedirect;
     }
} 

Please let me know how to do this.
Thanks

sort – Product sorting order in the shopping cart

What do you think is the best way to sort products in the shopping cart: newest added on top or at the bottom?

Especially on the mobile version of the website, I think that newest on top will give the user more assurance that the latest product was really added to the cart.

Does anybody know if there has been some testing done on this topic before?

magento2 – Order is creating Automatically and after purchase also still product is in cart

For few days we are facing a different issue Once we place the Order successfully it is showing the older Order id (Order id 000000960 but our current Order id 1000+) Success page.
For reference please find the attached image.
enter image description here

  • it is happening only for few customers.

The second issue is.
After placing the order. New Order is creating automatically with different order numbers.
For reference please find the attached images.
enter image description here
enter image description here
The third issue is.
After placing the order the product is still in the cart only.
enter image description here