Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento2 Can't add products to cart - "We can't add this item to your shopping cart right now" - problem with tempnam()

When I want to add any product (simples) to my cart the button shows a successful message, however from the main message I get: "We can't add this item to your shopping cart right now". Does not matter which product I choose, and I also tried incognito mode or reload after cache flushing.

I am running a freshly installed Magento 2.3.0 version and am in developer mode.

From the log I get the following:

main.CRITICAL: Notice: tempnam(): file created in the system's temporary directory in /www/htdocs/[...]/[...]/vendor/magento/zendframework1/library/Zend/Cache/Backend.php on line 203 {"exception":"[object] (Exception(code: 0): Notice: tempnam(): file created in the system's temporary directory in /www/htdocs/[...]/[...]/vendor/magento/zendframework1/library/Zend/Cache/Backend.php on line 203 at /www/htdocs/[...]/[...]/vendor/magento/framework/App/ErrorHandler.php:61)"} []

Starting at line 203 this is from the Backend.php:

       $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
    if ($tempFile) {
        $dir = realpath(dirname($tempFile));
        unlink($tempFile);
        if ($this->_isGoodTmpDir($dir)) {
            return $dir;
        }
    }

Since the products are on stock, I expect them to be added to the cart properly.

like image 603
TheFrakes Avatar asked Dec 02 '25 15:12

TheFrakes


1 Answers

EDIT

It was an issue with the temp directory. Be sure to set the 777 permission recurrsively to the var/cache directory.

But the main issue was the setting with the tmp directory. You have to edit the php.ini to explicitly set the tmp paths:

sys_temp_dir="/absolute/path/to/magento2/var/tmp"
session.save_path="/absolute/path/to/magento2/var/tmp"
session.cookie_path="/absolute/path/to/magento2/var/tmp"

OR if you do not have access to the php.ini (like I did because of my webhoster not allowing to edit it) you can make the changes in your .htaccess like this (if your hoster supports mod_env):

SetEnv TEMP /www/htdocs/your/path/mage/var/tmp
SetEnv TMP /www/htdocs/your/path/mage/var/tmp
SetEnv TMPDIR /www/htdocs/your/path/mage/var/tmp

See also:

  • https://magento.stackexchange.com/a/233296/44064 (edit php.ini)
  • https://magento.stackexchange.com/a/117520/44064 (.htaccess)
  • https://stackoverflow.com/a/23127346/6782214 (chmod var/cache)
  • http://httpd.apache.org/docs/current/mod/mod_env.html (mod_env)
like image 159
TheFrakes Avatar answered Dec 05 '25 08:12

TheFrakes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!