craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
226 stars 170 forks source link

[5.x]: Template error on multiple "Store Management" pages #3780

Open mihob opened 4 days ago

mihob commented 4 days ago

What happened?

Description

I get the following error when I try to access different store management pages (for example ‘Discounts’):

Twig\Error\RuntimeError: Impossible to access an attribute ("name") on a null variable. in /app/vendor/craftcms/commerce/src/templates/_layouts/store-management.twig:15
Stack trace:
#0 /app/vendor/craftcms/cms/src/helpers/Template.php(148): Twig\Extension\CoreExtension::getAttribute(Object(craft\web\twig\Environment), Object(Twig\Source), NULL, 'name', Array, 'any', false, false, false, 15)
#1 /app/storage/runtime/compiled_templates/ae/ae5dcacbc229e93207931a05a1570cf2.php(69): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig\Source), NULL, 'name', Array, 'any', false, false, false, 15)
#2 /app/vendor/twig/twig/src/Template.php(393): __TwigTemplate_6dc6dd2cbf5de204ccf19fd3b70169db->doDisplay(Array, Array)
#3 /app/storage/runtime/compiled_templates/b7/b7e454e3eb29748e95fdc2e654fa5489.php(156): Twig\Template->yield(Array, Array)
#4 /app/vendor/twig/twig/src/Template.php(393): __TwigTemplate_ca26b9e245e7d111c94bc80261724057->doDisplay(Array, Array)
#5 /app/vendor/twig/twig/src/Template.php(349): Twig\Template->yield(Array, Array)
#6 /app/vendor/twig/twig/src/Template.php(364): Twig\Template->display(Array)
#7 /app/vendor/twig/twig/src/TemplateWrapper.php(35): Twig\Template->render(Array)
#8 /app/vendor/twig/twig/src/Environment.php(306): Twig\TemplateWrapper->render(Array)
#9 /app/vendor/craftcms/cms/src/web/View.php(539): Twig\Environment->render('commerce/store-...', Array)
#10 /app/vendor/craftcms/cms/src/web/View.php(592): craft\web\View->renderTemplate('commerce/store-...', Array)
#11 /app/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(57): craft\web\View->renderPageTemplate('commerce/store-...', Array, 'cp')
#12 /app/vendor/yiisoft/yii2/web/Response.php(1109): craft\web\TemplateResponseFormatter->format(Object(craft\web\Response))
#13 /app/vendor/craftcms/cms/src/web/Response.php(339): yii\web\Response->prepare()
#14 /app/vendor/yiisoft/yii2/web/Response.php(340): craft\web\Response->prepare()
#15 /app/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#16 /app/web/index.php(13): yii\base\Application->run()
#17 {main}

The shop already existed in Craft 4/Commerce 4 and was migrated to Craft 5/Commerce 5

Steps to reproduce

  1. For example, try to access Commerce => Store Management => Discounts

Craft CMS version

5.5.2

Craft Commerce version

5.2.5

PHP version

8.2.24

Operating system and version

Linux 6.10.14-linuxkit

Database type and version

MariaDB 11.4.2

Image driver and version

Imagick 3.7.0 (ImageMagick 7.1.1-32)

Installed plugins and versions

Asset Locations 3.0.3 BlurHash 3.0.2 Control Panel CSS 3.0.0 CP Clear Cache 2.0.1 CP Field Inspect 2.0.2 Craft Commerce 5.2.5 Default Dashboard 3.0.1 Easy Image 2.0.1 Events dev-craft-5 Expanded Singles 3.0.1 Field Manager 4.0.3 Hyper 2.2.0 Incognito Field 2.0.0 No-Cache 3.0.4 Redactor 4.2.0 Redactor Anchors 1.5.0 Reverse Relations 3.0.0 Schedule 0.6.2 Sentry Logger 5.0.1 SEOMate 3.1.1 Smith 3.1.0 Sprig 3.5.1 Stripe for Craft Commerce 5.0.4.3 Super Table 4.0.2 Vite 5.0.1

linear[bot] commented 4 days ago

PT-2321 [5.x]: Template error on multiple "Store Management" pages

nfourtythree commented 3 days ago

Hi @mihob

Thank you for your message.

Could I ask what your store handle is/what was the URI you were trying to access?

Thanks!

mihob commented 3 days ago

The handle is primaryStore

The url looks like this: https://cms.example.test/commerce/store-management/primaryStore/discounts?site=german

nfourtythree commented 3 days ago

Thank you for your reply.

Could you please send your composer files (JSON and .lock) along with a database backup to support@craftcms.com and reference this ticket.

This will allow us to explore this further.

Thanks!

mihob commented 3 days ago

Is that really necessary? I would have to clean up the database first so as not to disclose any customer data.

This doesn't seem to be an isolated case, maybe you can share the solution from this ticket?

https://github.com/craftcms/commerce/issues/3406

mihob commented 3 days ago

@nfourtythree

The error is probably here (https://github.com/craftcms/commerce/blob/5.x/src/templates/_layouts/store-management.twig):

https://github.com/craftcms/commerce/blob/e598c90e1f9045f6e3b9d9712c8e9e7de9908340/src/templates/_layouts/store-management.twig#L10-L12

I think this is simply wrong:

Image

Since we are not using a cpTrigger, but the CMS is running on a subdomain, the storeHandle should be segment 3 and not segment 2

storeHandle should be defined as follows:

{% set storeHandle = craft.app.request.getSegment((craft.app.config.general.cpTrigger ? 4 : 3)) ?? craft.commerce.stores.getPrimaryStore().handle %}

For some pages (Shipping Categories) it works because store & and storeHandle are correctly determined in the controller and passed to the template.

However, it is not clear to me why this is not a problem for everyone. Maybe I'm missing something

mihob commented 8 hours ago

@nfourtythree Any news on this? We need the discounts feature...

mihob commented 8 hours ago

The problem affects the following store management pages: