Ich erstelle dir ein vollständiges Plugin für Shopware 6.5, das die Warenkorbmenge direkt in der Produkt-Listing Box anzeigt. Hier ist eine Schritt-für-Schritt-Implementierung:
- Zunächst die Plugin-Grundstruktur:
SwagCartQuantityDisplay/
├── src/
│ ├── Resources/
│ │ ├── app/
│ │ │ └── storefront/
│ │ │ └── src/
│ │ │ └── scss/
│ │ │ └── base.scss
│ │ ├── config/
│ │ │ └── services.xml
│ │ └── views/
│ │ └── storefront/
│ │ └── component/
│ │ └── product/
│ │ └── card/
│ │ └── box.html.twig
│ ├── Subscriber/
│ │ └── CartSubscriber.php
│ └── SwagCartQuantityDisplay.php
├── composer.json
└── manifest.xml
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/App/Manifest/Schema/manifest-1.0.xsd">
<meta>
<name>SwagCartQuantityDisplay</name>
<label>Cart Quantity Display</label>
<label lang="de-DE">Warenkorb-Mengenanzeige</label>
<description>Displays the cart quantity in product listing boxes</description>
<description lang="de-DE">Zeigt die Warenkorbmenge in Produkt-Listing Boxen an</description>
<author>Your Name</author>
<copyright>(c) by Your Name</copyright>
<version>1.0.0</version>
<license>MIT</license>
</meta>
</manifest>
composer.json:
{
"name": "swag/cart-quantity-display",
"description": "Displays the cart quantity in product listing boxes",
"version": "1.0.0",
"type": "shopware-platform-plugin",
"license": "MIT",
"autoload": {
"psr-4": {
"SwagCartQuantityDisplay\\": "src/"
}
},
"extra": {
"shopware-plugin-class": "SwagCartQuantityDisplay\\SwagCartQuantityDisplay",
"label": {
"de-DE": "Warenkorb-Mengenanzeige",
"en-GB": "Cart Quantity Display"
}
},
"require": {
"shopware/core": "^6.5"
}
}
SwagCartQuantityDisplay.php:
<?php declare(strict_types=1);
namespace SwagCartQuantityDisplay;
use Shopware\Core\Framework\Plugin;
use Shopware\Storefront\Framework\ThemeInterface;
class SwagCartQuantityDisplay extends Plugin implements ThemeInterface
{
}
CartSubscriber.php:
<?php declare(strict_types=1);
namespace SwagCartQuantityDisplay\Subscriber;
use Shopware\Core\Checkout\Cart\Event\CartChangedEvent;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
class CartSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $cartRepository;
public function __construct(EntityRepositoryInterface $cartRepository)
{
$this->cartRepository = $cartRepository;
}
public static function getSubscribedEvents(): array
{
return [
CartChangedEvent::class => 'onCartChanged'
];
}
public function onCartChanged(CartChangedEvent $event): void
{
// Implementierung der Logik für Cart-Updates
}
}
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 http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="SwagCartQuantityDisplay\Subscriber\CartSubscriber">
<argument type="service" id="cart.repository"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
</container>
- Template-Override für die Produkt-Box (
box.html.twig):
{% sw_extends '@Storefront/storefront/component/product/card/box.html.twig' %}
{% block component_product_box %}
{{ parent() }}
{% if page.cart.lineItems %}
{% for lineItem in page.cart.lineItems %}
{% if lineItem.referencedId == product.id %}
<div class="cart-quantity-badge">
{{ lineItem.quantity }}
</div>
{% endif %}
{% endfor %}
{% endif %}
{% endblock %}
- SCSS für das Styling (
base.scss):
.cart-quantity-badge {
position: absolute;
top: 10px;
right: 10px;
background: #ff3333;
color: white;
border-radius: 50%;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-weight: bold;
}
Um das Plugin zu installieren:
- Für self-hosted Shops:
bin/console plugin:refresh
bin/console plugin:install --activate SwagCartQuantityDisplay
bin/console cache:clear
- Für Cloud-Shops:
- Stelle sicher, dass das Plugin als App verpackt ist
- Lade es in den Shopware Store hoch
- Installiere es über den Plugin Manager
Wichtige Hinweise:
- Das Plugin verwendet Event Subscriber für Cart-Updates
- Es erweitert die Standard-Produkt-Box-Template
- Es fügt CSS für das Styling hinzu
- Es ist sowohl für self-hosted als auch Cloud-Shops kompatibel
Möchtest du weitere Details zu einem bestimmten Teil der Implementierung?