craft-admin-bar icon indicating copy to clipboard operation
craft-admin-bar copied to clipboard

Doesn't work with Craft Commerce Product Entries

Open amityweb opened this issue 4 years ago • 16 comments

It doesnt work with Craft Commerce Product Entries

Maybe because products dont have sections by the look of it?

Twig\Error\RuntimeError: Calling unknown method: craft\commerce\elements\Product::section() in /home/mysite/public_html/vendor/wbrowar/adminbar/src/templates/bar.twig:65
Stack trace:
#0 /home/mysite/public_html/storage/runtime/compiled_templates/24/2456f3b86b230aec225eafdef856d3de91e5d9002fdc1041400f5a9a02c8f0be.php(211): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(craft\commerce\elements\Product), 'section', Array)
#1 /home/mysite/public_html/vendor/twig/twig/src/Template.php(407): __TwigTemplate_707e533fd24ffc503550b68daace4d0e8e5b8582b06ca678bc791c8490623566->doDisplay(Array, Array)
#2 /home/mysite/public_html/vendor/craftcms/cms/src/web/twig/Template.php(52): Twig\Template->displayWithErrorHandling(Array, Array)
#3 /home/mysite/public_html/vendor/twig/twig/src/Template.php(380): craft\web\twig\Template->displayWithErrorHandling(Array, Array)
#4 /home/mysite/public_html/vendor/craftcms/cms/src/web/twig/Template.php(34): Twig\Template->display(Array, Array)
#5 /home/mysite/public_html/vendor/twig/twig/src/Template.php(392): craft\web\twig\Template->display(Array)
#6 /home/mysite/public_html/vendor/twig/twig/src/TemplateWrapper.php(45): Twig\Template->render(Array, Array)
#7 /home/mysite/public_html/vendor/twig/twig/src/Environment.php(318): Twig\TemplateWrapper->render(Array)
#8 /home/mysite/public_html/vendor/craftcms/cms/src/web/View.php(343): Twig\Environment->render('admin-bar/bar', Array)
#9 /home/mysite/public_html/vendor/wbrowar/adminbar/src/services/Bar.php(153): craft\web\View->renderTemplate('admin-bar/bar', Array)
#10 /home/mysite/public_html/vendor/wbrowar/adminbar/src/twigextensions/AdminBarTwigExtension.php(96): wbrowar\adminbar\services\Bar->render(Array)
#11 /home/mysite/public_html/storage/runtime/compiled_templates/37/370c22e618a4d1270d4a303f082ed643df26155492043296c9c5b50d151d7200.php(63): wbrowar\adminbar\twigextensions\AdminBarTwigExtension->adminBar(Array)
#12 /home/mysite/public_html/vendor/twig/twig/src/Template.php(184): __TwigTemplate_6600fede84920f222744c12c72b3cb5e904b00b3435869eb8f83c7cdc7b0ff21->block_adminbar(Array, Array)
#13 /home/mysite/public_html/storage/runtime/compiled_templates/67/675c5f3c5882d5f19c7dd451199328c4b593a22b69762365423e4cfc4f0fd386.php(66): Twig\Template->displayBlock('adminbar', Array, Array)
#14 /home/mysite/public_html/vendor/twig/twig/src/Template.php(407): __TwigTemplate_9aa259ab5b816d2e71d75ac5d6a0fc3b91daf69658a5f4a2bed966a3efccc44b->doDisplay(Array, Array)
#15 /home/mysite/public_html/vendor/craftcms/cms/src/web/twig/Template.php(52): Twig\Template->displayWithErrorHandling(Array, Array)
#16 /home/mysite/public_html/vendor/twig/twig/src/Template.php(380): craft\web\twig\Template->displayWithErrorHandling(Array, Array)
#17 /home/mysite/public_html/vendor/craftcms/cms/src/web/twig/Template.php(34): Twig\Template->display(Array, Array)
#18 /home/mysite/public_html/storage/runtime/compiled_templates/37/370c22e618a4d1270d4a303f082ed643df26155492043296c9c5b50d151d7200.php(55): craft\web\twig\Template->display(Array, Array)
#19 /home/mysite/public_html/vendor/twig/twig/src/Template.php(407): __TwigTemplate_6600fede84920f222744c12c72b3cb5e904b00b3435869eb8f83c7cdc7b0ff21->doDisplay(Array, Array)
#20 /home/mysite/public_html/vendor/craftcms/cms/src/web/twig/Template.php(52): Twig\Template->displayWithErrorHandling(Array, Array)
#21 /home/mysite/public_html/vendor/twig/twig/src/Template.php(380): craft\web\twig\Template->displayWithErrorHandling(Array, Array)
#22 /home/mysite/public_html/vendor/craftcms/cms/src/web/twig/Template.php(34): Twig\Template->display(Array, Array)
#23 /home/mysite/public_html/vendor/twig/twig/src/Template.php(392): craft\web\twig\Template->display(Array)
#24 /home/mysite/public_html/vendor/twig/twig/src/TemplateWrapper.php(45): Twig\Template->render(Array, Array)
#25 /home/mysite/public_html/vendor/twig/twig/src/Environment.php(318): Twig\TemplateWrapper->render(Array)
#26 /home/mysite/public_html/vendor/craftcms/cms/src/web/View.php(343): Twig\Environment->render('shop/products/_...', Array)
#27 /home/mysite/public_html/vendor/craftcms/cms/src/web/View.php(393): craft\web\View->renderTemplate('shop/products/_...', Array)
#28 /home/mysite/public_html/vendor/craftcms/cms/src/web/Controller.php(238): craft\web\View->renderPageTemplate('shop/products/_...', Array)
#29 /home/mysite/public_html/vendor/craftcms/cms/src/controllers/TemplatesController.php(95): craft\web\Controller->renderTemplate('shop/products/_...', Array)
#30 [internal function]: craft\controllers\TemplatesController->actionRender('shop/products/_...', Array)
#31 /home/mysite/public_html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#32 /home/mysite/public_html/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#33 /home/mysite/public_html/vendor/craftcms/cms/src/web/Controller.php(186): yii\base\Controller->runAction('render', Array)
#34 /home/mysite/public_html/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('render', Array)
#35 /home/mysite/public_html/vendor/craftcms/cms/src/web/Application.php(298): yii\base\Module->runAction('templates/rende...', Array)
#36 /home/mysite/public_html/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction('templates/rende...', Array)
#37 /home/mysite/public_html/vendor/craftcms/cms/src/web/Application.php(283): yii\web\Application->handleRequest(Object(craft\web\Request))
#38 /home/mysite/public_html/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#39 /home/mysite/public_html/web/index.php(21): yii\base\Application->run()
#40 {main}

Its these lines, it looks like not only is there is no section, but userCanEdit is not applicable to products too. It works if I remove the check for section and userCanEdit. So probably only needs a small tweak to make it work for products.

{% if entry ?? false and ((userCanEdit ?? false) or (entry.section.id in craft.app.sections.editableSectionIds)) %}
{% set sectionName = displayDefaultEditSection ? ' ' ~ entry.section : ''  %}
{% set entryEditUrl = entry.cpEditUrl %}

amityweb avatar Jul 22 '19 12:07 amityweb

Is there any help someone can give me to get this working even if the plugin is not improved for it? I am being pushed to get this, its a real pain not being able to have an Edit for products. Thanks.

amityweb avatar Sep 04 '19 09:09 amityweb

Hey Laurence, sorry to sit on this. I've been pretty swamped lately. I'll take a look at it and try to get support out soon.

wbrowar avatar Sep 04 '19 13:09 wbrowar

Hi. Is there any update to this? Even if you let me know where to change code maybe I can try. My customer is really pushing for this and expressing concern its been so long. Basically we said yes there is an admin bar for Craft but only found out it does not support products after installing, so its a bit embarrassing and something we said he could have (just assumed your plugin would link to all entries not some!). Thanks

amityweb avatar Oct 23 '19 09:10 amityweb

This is all set in 3.1.9. You may now set the Edit Link to any URL by doing something like this:

{{ adminbar({ editLinkLabel: 'Thankgiving Recap', editLinkUrl: 'admin/utilities/php-info' }) }}

For the editLinkUrl parameter, you could set it to something dynamic, so if you can get something like the cpEditUrl from a Commerce Product, you could pass that through.

wbrowar avatar Nov 29 '19 00:11 wbrowar

This is a great addition instead of focusing on just products. Thanks a lot for adding this. Here is my full code for creating an Edit Products link on a product page. I check if the user is admin, and add cache, just to reduce the entry lookups on every page load. Cache probably not as important as its only admins but checking if the user is admin is important else theres the extra entry lookups (in my own code anyway) needed before calling adminbar.

{% if craft.app.user.getIsAdmin() %}
	{% cache %}
	{% set entry = craft.products().slug(craft.request.getLastSegment()).first() %}
	{{ adminbar({ editLinkLabel: 'Product', editLinkUrl: entry.cpEditUrl }) }}
	{% endcache %}
{% endif %}

amityweb avatar Nov 29 '19 09:11 amityweb

Hi. Has there been a recent update, but this fix above not included?

We updated our site the other day, the Edit of products has gone, its version 3.1.8.1. Cant get 3.1.9 or 3.1.10 to show in updates when I include that version in composer.

amityweb avatar Aug 03 '20 17:08 amityweb

@amityweb That's super weird. I'll take a look tonight.

wbrowar avatar Aug 03 '20 23:08 wbrowar

Oh, @amityweb, are you on a recent version of Craft? Admin Bar 3.1.9 requires Craft 3.1.22+

wbrowar avatar Aug 03 '20 23:08 wbrowar

Yes we're on 3.4.27. Thanks.

amityweb avatar Aug 04 '20 08:08 amityweb

@amityweb can you please try installing the latest dev version by changing the version in composer.json to dev-master and let me know if that works? I think it's because I had some really old plugin scaffolding code in there.

wbrowar avatar Aug 04 '20 17:08 wbrowar

I do that but when I run ./craft update its not listed in the updates, nor in the Craft admin.

amityweb avatar Aug 04 '20 19:08 amityweb

I have a similar problem on a custom route. And when I use:

{{ adminbar({ editLinkLabel: 'Thankgiving Recap', editLinkUrl: 'admin/utilities/php-info' }) }}

There does not come any edit link at all.

I tried out different urls but nothing worked for me. Is this a bug or did I something wrong?

MichaelBrauner avatar Aug 16 '20 13:08 MichaelBrauner

Hi. Just wondered if there is any update, anything you can think of thats breaking this? So not only did it stop working as it used to work (I cant recall if it was an edit I made to your code, or we updated to a version that had it), but when I edit the code it doesn't work, nor does Craft see any update needed. We are on Craft Admin Bar 3.1.8.1 and Craft 3.4.27.

Can I just download it from here and overwrite it, or will that mess up composer somehow?

Thanks

amityweb avatar Aug 17 '20 08:08 amityweb

Do you know if this is an issue with your plugin or our site? I just dont know myself, why your update is not showing. Thanks

amityweb avatar Aug 27 '20 09:08 amityweb

I uninstalled it completely, and re-installed it, so on version 3.1.8.1 still, but no Edit Product exists. I guess the means there are no updates if I installed the latest from the plugin store.

I dont want to mess with composer anymore, I get composer errors. I could not install another plugin due to changing adminbar to require dev-master. Once I reinstalled so composer references the right version I can install other plugins.

amityweb avatar Sep 10 '20 10:09 amityweb

It seems like this has stopped working: {{ adminbar({ editLinkLabel: 'Product', editLinkUrl: entry.cpEditUrl }) }}

Because if I put that in instead of just {{ adminbar() }} then nothing happens, it still shows "Edit Home" on the page, its like its being ignored. So on the Product pages its not showing any Edit link.

amityweb avatar Sep 10 '20 10:09 amityweb