Hier ist die Zusammenfassung der Implementierung des Plugins:
Das Plugin zeigt die Anzahl der Produkte im Warenkorb direkt in der Produktliste an. Es besteht aus mehreren Komponenten, darunter eine Hauptklasse, JavaScript, CSS und ein Twig-Template. Das Plugin ist kompatibel mit Shopware 6.5 und funktioniert sowohl für selbstgehostete Shops als auch für die Cloud-Version.
Plugin-Struktur
Die grundlegende Verzeichnisstruktur des Plugins sieht wie folgt aus:
CartQuantityInListing/
├── composer.json
├── manifest.xml
├── src/
│ ├── CartQuantityInListing.php
│ ├── Resources/
│ │ ├── app/
│ │ │ └── storefront/
│ │ │ ├── src/
│ │ │ │ ├── main.js
│ │ │ │ └── scss/
│ │ │ │ └── base.scss
│ │ │ └── dist/
│ │ │ ├── main.js
│ │ │ └── base.css
│ │ └── views/
│ │ └── storefront/
│ │ └── product/
│ │ └── listing.html.twig
├── Resources/
│ ├── config/
│ │ └── services.xml
│ └── views/
│ └── storefront/
│ └── product/
│ └── listing.html.twig
Hauptdateien
- composer.json:
{
"name": "cart-quantity-in-listing",
"description": "Shows the quantity of products in the cart directly in the product listing.",
"type": "shopware-platform-plugin",
"license": "MIT",
"authors": [
{
"name": "Your Name",
"email": "your.email@example.com"
}
],
"require": {
"php": ">=7.4",
"shopware/core": "^6.5"
},
"autoload": {
"psr-4": {
"CartQuantityInListing\\": "src/"
}
},
"extra": {
"shopware-plugin-class": "CartQuantityInListing\\CartQuantityInListing",
"label": {
"de-DE": "Warenkorb-Anzahl in der Liste",
"en-GB": "Cart Quantity in Listing"
}
}
}
- manifest.xml:
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/trunk/src/Core/Framework/Plugin/Schema/manifest-1.0.xsd">
<meta>
<name>CartQuantityInListing</name>
<label>Warenkorb-Anzahl in der Liste</label>
<description>Zeigt die Anzahl der Produkte im Warenkorb direkt in der Produktliste an.</description>
<author>Your Name</author>
<copyright>(c) by Your Name</copyright>
<license>MIT</license>
<version>1.0.0</version>
<icon>icon.png</icon>
</meta>
<setup>
<compatibility>
<shopwareVersion>6.5.0.0</shopwareVersion>
</compatibility>
</setup>
</manifest>
- Hauptklasse (CartQuantityInListing.php):
<?php declare(strict_types=1);
namespace CartQuantityInListing;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
class CartQuantityInListing extends Plugin
{
public function install(InstallContext $installContext): void
{
// Hier können Installationsroutinen hinzugefügt werden
}
public function uninstall(UninstallContext $uninstallContext): void
{
// Hier können Deinstallationsroutinen hinzugefügt werden
}
}
- JavaScript (main.js):
import Plugin from 'src/plugin-system/plugin.class';
import DomAccess from 'src/helper/dom-access.helper';
export default class CartQuantityInListingPlugin extends Plugin {
init() {
this._registerEvents();
}
_registerEvents() {
this._getCartQuantity();
}
_getCartQuantity() {
const productBoxes = document.querySelectorAll('.product-box');
const cartQuantity = this._getCartQuantityFromStorage();
productBoxes.forEach(box => {
const productId = box.dataset.productId;
const quantityInCart = cartQuantity[productId] || 0;
if (quantityInCart > 0) {
const badge = document.createElement('div');
badge.className = 'cart-quantity-badge';
badge.textContent = quantityInCart;
box.querySelector('.product-name').appendChild(badge);
}
});
}
_getCartQuantityFromStorage() {
const cartData = localStorage.getItem('sw-cart');
return cartData ? JSON.parse(cartData) : {};
}
}
- CSS (base.scss):
.cart-quantity-badge {
position: absolute;
top: 0;
right: 0;
background-color: #ff0000;
color: #ffffff;
border-radius: 50%;
width: 20px;
height: 20px;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-weight: bold;
}
- Twig-Template (listing.html.twig):
{% sw_extends '@Storefront/storefront/product/listing.html.twig' %}
{% block element_product_box %}
{{ parent() }}
{# Hier fügen wir das Badge für die Warenkorbmenge hinzu #}
<div class="cart-quantity-badge" data-product-id="{{ product.id }}">
{% if product.inCartQuantity > 0 %}
{{ product.inCartQuantity }}
{% endif %}
</div>
{% endblock %}
- services.xml:
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="CartQuantityInListing\CartQuantityInListing">
<tag name="shopware.plugin" />
</service>
</services>
</container>
Installation und Aktivierung
- Kopieren Sie das Plugin-Verzeichnis in das
custom/plugins-Verzeichnis Ihres Shopware-Shops.
- Installieren und aktivieren Sie das Plugin in der Shopware-Administration.
Verwendung
Nach der Installation und Aktivierung des Plugins wird die Anzahl der Produkte im Warenkorb direkt in der Produktliste angezeigt.
Kompatibilität
Dieses Plugin ist kompatibel mit Shopware 6.5 und funktioniert sowohl für selbstgehostete Shops als auch für die Cloud-Version.
Lizenz
Dieses Plugin ist unter der MIT-Lizenz lizenziert.
Mit diesen Schritten haben wir ein vollständiges Plugin für Shopware 6.5 erstellt, das die Anzahl der Produkte im Warenkorb direkt in der Produktliste anzeigt. Das Plugin ist sowohl für selbstgehostete Shops als auch für die Cloud-Version geeignet.