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.

Solutions

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)

Similar questions

Magento Upgrade 1.5.1 to 1.9.1 now Cannot add the item to shopping cart
I upgraded my Magento platform and everything is working in the admin and the front end except the shopping cart. Here is my exception log. When I try to add an item to cart, the cart won't add the product. I upgraded everything on the front end but am using the 1.5.1 database with some columns added to the salescoupon table 21 {main}
Shopping Cart You have no items in your shopping cart
I reintsalled and now i dont have any problems except this when i add some items. What can be reason? I removed inside of debuglog and systemlog and other logs . Also, i registered an account and when i add to wishlist, it is same again. now i have somelogs for debug.lo: [2016-09-06 16:14:07] main.DEBUG: cache_invalidate: {"method":"POST","url":"ht...
I installed Full Page Cache and now after I click on "Add to Cart" - it says: There are 0 items in your cart
Magento 1.9.1 Full Page Cache program from Amasty Our website was often extremely slow so I installed a Full Page program from Amasty and now sometimes (like right now) when I go through the order process - after I click on the "Add to cart" button - it says: And right below that it says: I think it's a reason why we are getting only about 10% of t...
How to find products being viewed right now
One of our clients has a specific requirement to show some products(let's say 4 or 5) in the home page that are being viewed at the moment by other users. Your suggestions about how to begin with this would be greatly appreciated. Thanks.
Magento success page (1.9.3.9) is showing weird message (Item has been added to your shopping cart)
The code message is shown between 'Your order has been received' and 'Thank you for your purchase'. The template success.phtml looks like this: Why is this showing this message? What could be going wrong? Where I should look for?
Magento 2.0.2 Upgrade: Sorry, we can't take that action right now
I get this message when I try to upgrade my Magento 2.0.2 Sorry, we can't take that action right now.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.