Magento2 无法将产品添加到购物车 - “我们现在无法将此商品添加到您的购物车” - tempnam() 问题

The*_*kes 3 php shopping-cart magento2

当我想将任何产品(简单)添加到我的购物车时,按钮会显示一条成功消息,但是从我得到的主要消息中:"We can't add this item to your shopping cart right now".我选择哪种产品并不重要,而且我还尝试了隐身模式或缓存刷新后重新加载。

我正在运行新安装的 Magento 2.3.0 版本,并且处于开发人员模式。

从日志中我得到以下信息:

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)"} []

从 Backend.php 的第 203 行开始:

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

由于产品有库存,我希望它们能够正确添加到购物车中。

The*_*kes 5

编辑

这是临时目录的问题。请务必对 var/cache 目录递归设置 777 权限。

但主要问题是 tmp 目录的设置。您必须编辑 php.ini 以显式设置 tmp 路径:

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"
Run Code Online (Sandbox Code Playgroud)

或者,如果您无权访问 php.ini(就像我所做的那样,因为我的网络托管商不允许编辑它),您可以像这样在 .htaccess 中进行更改(如果您的托管商支持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
Run Code Online (Sandbox Code Playgroud)

也可以看看: