magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.56k stars 9.32k forks source link

Can't delete last item in cart if Minimum Order is Enable #6151

Closed philipvandebriel closed 7 years ago

philipvandebriel commented 8 years ago

Preconditions

Update magento 2.0.8 to Magento 2.1 Debian Jessie Mysql 5.6 Php 5.6

  1. Put item in cart
  2. Try delete item , after popup error

    Steps to reproduce

  1. Go to admin -> SALES -> sales
  2. Enable -> Minimum Order Amount
  3. Minimum Amount = 10 or something

    Expected result

  4. After remove product from mini cart then the cart need to be empty

    Actual result

  5. Last product stay in the cart can't remove Try to remove the item then we get this error :

Recoverable Error: Argument 1 passed to Magento\Framework\Exception\InputException::__construct() must be an instance of Magento\Framework\Phrase, string given, called in /var/www/clients/client1/web1/web/vendor/magento/module-quote/Model/ShippingAddressManagement.php on line 121 and defined in /var/www/clients/client1/web1/web/vendor/magento/framework/Exception/InputException.php on line 51

If disable Minimum Order Amount then everything is fine.

stripcurtains commented 8 years ago

I'm having the exactly same issue with 2.1

stripcurtains commented 8 years ago

@wert2all Is there a temporary fix for it?

AVoskoboinikov commented 8 years ago

Hi, guys.

Thanks for reporting this, I've created ticket #MAGETWO-57354 in our internal Jira. This issue will be closed after fix.

csd-rhuang commented 8 years ago

Hi,

I experienced the same issue. And even worse, I will get a http 500 error when I add the 3rd item to the cart when the minimum order is enabled.

It is Magento 2.1. Bitnami package on AWS.

Thanks

Rex

vijay-wagento commented 8 years ago

For my ongoing project in Magento 2.1 I have done below things to come out from this problem,

1) Create Plugin

 <type name="Magento\Quote\Model\Quote">
        <plugin name="min-orderamount-skip-delete" type="Namespace\ModuleName\Model\QuoteAfter" sortOrder="10"/>
    </type>

2) in QuoteAfter class

public function afterValidateMinimumAmount()
    {
        $actionName = $this->request->getActionName();
        if($actionName === 'delete') {
            return true;
        }
    }

This solved my problem and I got success to delete item.

ivankaranjac commented 8 years ago

magento 2.1.2 Problem with deleting, updating and adding to cart once an item is added to the cart

traced the problem to vendor/magento/module-quote/Model/ShippingAddressManagement.php#120

commented out that if and it all seams to work fine now.

Also InputException takes \Magento\Framework\Phrase for first param but in this class string is used, so 500 error.

ritschi72 commented 7 years ago

I also got problems with minimum order amount, cannot delet item from cart anymore, but there is no error message, only blank page. I disabled minimum order amount, and everythin works fine. Any solution for this?

chassotl-IFT commented 7 years ago

For the one who want it, I did a module, based on the solution of @ivankaranjac , which override the function of the original file : mediafire.com Put the "BugsFix" folder into "app/code". Note : tested on Magento 2.1.3

ritschi72 commented 7 years ago

Still got that issue in 2.1.4, any updates on that?

hatimeria-artur-jewula commented 7 years ago

Fatal error happens if sales/minimum_order/description is provided. This is because Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::getMessage() returns just this string and not a Phrase object.

ishakhsuvarov commented 7 years ago

Closing as issue is fixed in the develop branch with the linked commits.

ishakhsuvarov commented 7 years ago

Reopening, as 2.1 fix is not available yet.

magento-team commented 7 years ago

Internal ticket to track issue progress: MAGETWO-69375

magento-team commented 7 years ago

Internal ticket to track issue progress: MAGETWO-64899

magento-team commented 7 years ago

Internal ticket to track issue progress: MAGETWO-71761

coachdesign commented 7 years ago

I've updated Magento to version 2.2 the problem is still there

DavaGordon commented 7 years ago

Simply modify vendor/magento/module-quote/Model/ShippingAddressManagement.php to include the RequestInterface and check to see if the update_cart_action was posted, my code is below and seems to work fine tested on 2.1.8 and 2.2.0 RC3

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Quote\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Psr\Log\LoggerInterface as Logger;

/**
 * Quote shipping address write service object.
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class ShippingAddressManagement implements \Magento\Quote\Model\ShippingAddressManagementInterface
{
    /**
     * Quote repository.
     *
     * @var \Magento\Quote\Api\CartRepositoryInterface
     */
    protected $quoteRepository;

    /**
     * Logger.
     *
     * @var Logger
     */
    protected $logger;

    /**
     * Validator.
     *
     * @var QuoteAddressValidator
     */
    protected $addressValidator;

    /**
     * @var \Magento\Customer\Api\AddressRepositoryInterface
     */
    protected $addressRepository;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * @var Quote\TotalsCollector
     */
    protected $totalsCollector;

    /**
     * @var \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage
     */
    private $minimumAmountErrorMessage;

    /**
     * @var \Magento\Framework\App\RequestInterface
     */
    protected $requestInterface;

    /**
     * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
     * @param QuoteAddressValidator $addressValidator
     * @param Logger $logger
     * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param Quote\TotalsCollector $totalsCollector
     *
     */
    public function __construct(
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
        QuoteAddressValidator $addressValidator,
        Logger $logger,
        \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
        \Magento\Framework\App\RequestInterface $requestInterface,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->addressValidator = $addressValidator;
        $this->logger = $logger;
        $this->addressRepository = $addressRepository;
        $this->request = $requestInterface;
        $this->scopeConfig = $scopeConfig;
        $this->totalsCollector = $totalsCollector;
    }

    /**
     * {@inheritDoc}
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address)
    {
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->quoteRepository->getActive($cartId);
        if ($quote->isVirtual()) {
            throw new NoSuchEntityException(
                __('Cart contains virtual product(s) only. Shipping address is not applicable.')
            );
        }

        $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
        $sameAsBilling = $address->getSameAsBilling() ? 1 : 0;
        $customerAddressId = $address->getCustomerAddressId();
        $this->addressValidator->validate($address);
        $quote->setShippingAddress($address);
        $address = $quote->getShippingAddress();

        if ($customerAddressId === null) {
            $address->setCustomerAddressId(null);
        }

        if ($customerAddressId) {
            $addressData = $this->addressRepository->getById($customerAddressId);
            $address = $quote->getShippingAddress()->importCustomerAddressData($addressData);
        } elseif ($quote->getCustomerId()) {
            $address->setEmail($quote->getCustomerEmail());
        }
        $address->setSameAsBilling($sameAsBilling);
        $address->setSaveInAddressBook($saveInAddressBook);
        $address->setCollectShippingRates(true);

        $postData = $this->request->getParam('update_cart_action');
        if(!$postData) {
            if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
                throw new InputException(__($this->getMinimumAmountErrorMessage()->getMessage()));
            }
        }

        try {
            $address->save();
        } catch (\Exception $e) {
            $this->logger->critical($e);
            throw new InputException(__('Unable to save address. Please check input data.'));
        }
        return $quote->getShippingAddress()->getId();
    }

    /**
     * {@inheritDoc}
     */
    public function get($cartId)
    {
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->quoteRepository->getActive($cartId);
        if ($quote->isVirtual()) {
            throw new NoSuchEntityException(
                __('Cart contains virtual product(s) only. Shipping address is not applicable.')
            );
        }
        /** @var \Magento\Quote\Model\Quote\Address $address */
        return $quote->getShippingAddress();
    }

    /**
     * @return \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage
     * @deprecated
     */
    private function getMinimumAmountErrorMessage()
    {
        if ($this->minimumAmountErrorMessage === null) {
            $objectManager = ObjectManager::getInstance();
            $this->minimumAmountErrorMessage = $objectManager->get(
                \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class
            );
        }
        return $this->minimumAmountErrorMessage;
    }
}
erlisdhima commented 6 years ago

I am having the same issue with 2.1.10. To verify if this was fixed, I setup a clean Magento 2.1.12 instance, and the issue is still there. When I try to remove an item from the cart:

screenshot from 2018-03-01 10-17-03

When updating the QTY:

screenshot from 2018-03-01 10-17-17

The only way it updates is when the price x qty equals the minimum amount.

I commented line 120-122 (as mentioned also by @ivankaranjac ) to get this work:

if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
    throw new InputException($this->getMinimumAmountErrorMessage()->getMessage());
}

EDIT: I tried @DavaGordon's solution but that fixes only cart update or clear cart. It does not fix deleting individual items in the cart, adding products from the category/product page, deleting or updating items from the minicart.

bgronek commented 6 years ago

It doesn't look like this is fixed. Will you be re-opening it?

teokolo commented 5 years ago

Bug still present in Magento 2.1.17.

Here is the error log:

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Magento\\Framework\\Exception\\InputException::__construct() must be an instance of Magento\\Framework\\Phrase, string given, called in PATH/vendor/magento/module-quote/Model/ShippingAddressManagement.php on line 121 and defined in PATH/vendor/magento/framework/Exception/InputException.php:51, referer: https://www.elioworld.com/checkout/cart/

Stack trace:, referer: https://www.elioworld.com/checkout/cart/

#0 PATH/vendor/magento/module-quote/Model/ShippingAddressManagement.php(121): Magento\\Framework\\Exception\\InputException->__construct('Attenzione: l'o...'), referer: https://www.elioworld.com/checkout/cart/

#1 PATH/vendor/magento/module-quote/Model/Quote/ShippingAssignment/ShippingProcessor.php(66): Magento\\Quote\\Model\\ShippingAddressManagement->assign('22', Object(Magento\\Quote\\Model\\Quote\\Address)), referer: https://www.elioworld.com/checkout/cart/

#2 PATH/vendor/magento/module-quote/Model/Quote/ShippingAssignment/ShippingAssignmentProcessor.php(110): Magento\\Quote\\Model\\Quote\\ShippingAssignment\\ShippingProcessor->save(Object(Magento\\Quot in PATH/vendor/magento/framework/Exception/InputException.php on line 51, referer: https://www.elioworld.com/checkout/cart/