Shopware: Remove Add To Cart for Some products - shopware

Within Shopware 6, has anyone figured out how to remove the “add to cart” button for some products? My goal is to allow the add to cart on most items, but I’d like the option/ability to remove it for select products.
Thanks in advance!
Scott

You can use a custom field for this.
In admin go to Settings / Custom fields and add a new custom field set. Name it for example custom_product and assign Products, save. Then add a new custom field. Name it for example custom_product_remove_buy_button and choose Active switch as type.
Alternatively, you can create the custom field programmatically as described in the documentation.
You will see the new custom field in admin in product detail under Specifications / Custom fields.
Then you can use the custom field in a template to disable the "add to cart" button in the storefront. In order to remove the button on the product detail page, create a new file src/Resources/views/storefront/page/product-detail/buy-widget.html.twig in your theme or plugin and paste the following code:
{% sw_extends '#Storefront/storefront/page/product-detail/buy-widget.html.twig' %}
{% block page_product_detail_buy_form %}
{% if page.product.active and page.product.customFields.custom_product_remove_buy_button != true %}
<div class="product-detail-form-container">
{% sw_include '#Storefront/storefront/page/product-detail/buy-widget-form.html.twig' %}
</div>
{% endif %}
{% endblock %}
To remove the button in the product listing, create a file src/Resources/views/storefront/component/product/card/action.html.twig:
{% sw_extends '#Storefront/storefront/component/product/card/action.html.twig' %}
{% block component_product_box_action_buy %}
{% if product.customFields.custom_product_remove_buy_button != true %}
{{ parent() }}
{% endif %}
{% endblock %}

Setting the Max. order quantity to 0 will result in not showing a buy button on the product detail page. However it is still possible to add the product via the listing. To fix this you can extend this file as mentioned before:
src/Resources/views/storefront/component/product/card/action.html.twig
{% block component_product_box_action_buy %}
{% if product.calculatedMaxPurchase > 0 %}
{{ parent() }}
{% endif %}
{% endblock %}
If the user somehow manages to add the product in the cart Shopware will throw a default error message:
The product "Test" is not available any more

Related

How to add entity extension in Shopware 6 App?

I want to query the categories of the product in the product listing of a Shopware app, so that I can query the customFields of all categories. Is this even possible with an app?
I have already tried via a navigation-page-loaded. And when I override the box-standard.html.twig and access the product, I can't access the categories there.
For each product i want the categories extensions
As the categories association is not loaded for products in the listing you have to fetch the categories using an app script.
As already noted, add the script to the navigation-page-loaded hook, i.e. in Resources/scripts/navigation-page-loaded/category-loader.twig:
{% set products = [] %}
{% if hook.page.cmsPage.type === 'product_list' %}
{% foreach hook.page.cmsPage.sections as section %}
{% foreach section.blocks as sectionBlock %}
{% if sectionBlock.type !== 'product-listing' %}
{% continue %}
{% endif %}
{% foreach sectionBlock.slots as slot %}
{% if slot.type !== 'product-listing' %}
{% continue %}
{% endif %}
{% foreach slot.data.listing.entities as product %}
{% set products = products|merge([product]) %}
{% endforeach %}
{% endforeach %}
{% endforeach %}
{% endforeach %}
{% endif %}
{% set categoryIds = products|reduce((carry, v) => carry|merge(v.categoryIds), []) %}
{% if categoryIds %}
{% set categories = services.repository.search('category', {'ids': categoryIds}) %}
{% foreach products as product %}
{% do product.addArrayExtension('myCategories', {
'categories': categories.entities.getList(product.categoryIds),
}) %}
{% endforeach %}
{% endif %}
Where we first extract all the products, than load the categories of all products at once, and than assign the categories back to the products.
Note that for reading the category entity you need the correct permission, i.e. in the manifest.xml add:
<permissions>
<read>category</read>
<read>category_translation</read>
</permissions>
Now you should be able to access in the box-standard.html.twig template the categories using product.extensions.myCategories.categories.
If I understand your issue correctly, you are trying to access a product's category's custom fields in some piece of storefront logic (like a Twig template).
The way I see it, you would have to add a script that would enable you to query the repository for the categories and their custom fields. The issue is, a SalesChannelProductEntity will not contain the information about all its categories (only the SeoCategory), so you might need to first query the product_category repository.
Generally, it is going to be complicated but it should be doable.

Shopware : Impossible to extend through plugin user-detail

I have Shopware 6.5.3. I was trying to extend "sw-users-permissions-user-detail" like this :
import template from './sw-users-permissions-user-detail.html.twig';
Shopware.Component.override('sw-users-permissions-user-detail', {
template
});
And file 'sw-users-permissions-user-detail.html.twig'
{% block sw_settings_user_detail %}
{% parent %}
{% block test %}
<p>Blabla</p>
{% endblock %}
{% endblock %}
It's not working at all, and I don't know why.
Any help ?
NB : It's working when I'm overriding other templates :
Component.override('sw-dashboard-index', {
template
});
If you want to put the original contents to the block, that you are overriding, you should use the 'parent' statement like this:
{{ parent() }}

How to get article url in a given category?

I'm trying to make a scroll on products in a given category for articles in shopware 6. I've managed to extend the product-detail html.twig file to add the next and previous on the page. The problem I'm facing is how to generate the URL links in a given category.
{% sw_extends '#storefront/page/product-detail/index.html.twig' %}
{% block base_content %}
« Previous
Next »
{{parent()}}
{% endblock %}
With the twig function seoUrl, you can generate links to other pages.
After the URL you can add query strings for the right pagination, see the example:
{{ seoUrl('frontend.navigation.page', { navigationId: 'ID' }) }}

How to add active class if current url matches the given (twig)

I want to write manually a side menu for the store on opencart, and I have a problem - how to make twig add the class "active" to the link for current page
I tried to do it like this
page about something
but it doesnt work
This is exactly what the category module does so you can copy it:
{% if child.category_id == child_id %}
- {{ child.name }}
{% else %}
- {{ child.name }}
{% endif %}
You can find the file above here:
/catalog/view/theme/default/template/extension/module/category.twig
You can find its controller here:
/catalog/controller/extension/module/category.php
They way I would do it is out the route request in by the controller phone file. This would be something like the below:
$urlroute = $this->request->get['route'];
Then in the twig file you could simply check the route variable in an IF query
{% if urlroute == "something" % }
page about something
{% else %}
page about something
{% endif %}

html_entity_decode for twig (opencart)

im trying to output an attribute of a product on my product page (opencart v3).
The attribute is called 'technicaldetails' and it works just fine using this code:
{% if attribute_groups %}
{% for attribute_group in attribute_groups %}
{% if attribute_group.name == 'technicaldetails' %}
{% for attribute in attribute_group.attribute %}
{{ attribute.text }}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
but the technical details field have unstyled list stored in it.. and this outputs the complete html instead of rendering the list.
ive tried using {{ attribute.text|e }} and {{ attribute.text|raw }} and many other alternatives i could find.. but each time is just throws out the html and not render it..
in php this used to work.
<?php echo html_entity_decode($attribute['text']); ?>
so how can i decode the html now as i cant use php in twig and there is no html_entity_decode in twig either :(
looking forward for somehelp :)
much appreciated
thanks.
Just register the html_entity_decode function in twig.
The most simple way is to look where twig is loaded and add the following code,
$twig->addFilter(new \Twig_Simple_Filter, 'html_entity_decode', 'html_entity_decode');
After that you can just do the following in your twig templates
{{ attribute.text|html_entity_decode }}
UPDATE: For Opencart 3.0.3.7 version filter should be like this:
$twig->addFilter(new \Twig\TwigFilter('html_entity_decode','html_entity_decode'));
Find file
document_root/system/library/template/twig.php
Just after
$this->twig = new \Twig_Environment($loader, $config);
add following code
$twig->addFilter(new \Twig_SimpleFilter('html_entity_decode', 'html_entity_decode'));
After doing this, you must went to admin to reload all modifications in menu Extensions -> modifications.
After that you can do the following in all twig files *.twig
{{ attribute.text|html_entity_decode }}

Resources