Shopware 6.7 introduces significant enhancements to cart calculation and price rule handling, fundamentally changing how merchants can configure complex pricing strategies. This technical deep dive explores the underlying architecture, implementation details, and performance implications of these new features.
Understanding the New Cart Calculation Architecture
The cart calculation process in Shopware 6.7 has been completely restructured around a new service-oriented architecture. The core CartCalculator service now operates through a pipeline of dedicated calculation steps, each responsible for specific aspects of price determination.
// Core cart calculation flow
class CartCalculator
{
public function calculate(Cart $cart, Context $context): Cart
{
$this->calculateLineItems($cart, $context);
$this->calculateShippingCosts($cart, $context);
$this->applyPriceRules($cart, $context);
$this->calculateTotalAmounts($cart, $context);
return $cart;
}
}
The new architecture implements a CartProcessor interface that allows developers to register custom calculation steps. This modular approach enables better performance tuning and easier maintenance of complex pricing logic.
Price Rule Engine Enhancements
Shopware 6.7 introduces a completely rewritten price rule engine based on the new Rule system. The previous PriceRule class has been deprecated in favor of PriceRuleCollection with enhanced matching capabilities.
class PriceRuleCollection extends Collection
{
public function match(RuleScope $scope): self
{
return $this->filter(function (PriceRule $rule) use ($scope) {
return $rule->getRule()->match($scope);
});
}
}
The new rule engine supports complex logical combinations through a fluent API:
$rule = (new PriceRule())
->setActive(true)
->addCondition(new CustomerGroupRule(['customer_group_ids' => ['1234567890abcdef1234567890abcdef']]))
->addCondition(new ProductManufacturerRule(['manufacturer_ids' => ['1234567890abcdef1234567890abcdef']]))
->setDiscount(10)
->setDiscountType('percentage');
Cart Item Calculation Pipeline
Each cart item now undergoes a multi-stage calculation process:
- Base Price Calculation: Determined by product pricing rules and configured price tiers
- Quantity-Based Discounts: Applied through the new
QuantityPriceRulesystem - Bundle Calculations: Complex bundle pricing with configurable discount structures
- Tax Calculation: Enhanced tax handling with improved rounding precision
class LineItemCalculator
{
public function calculateLineItem(LineItem $item, Context $context): void
{
$this->calculateBasePrice($item, $context);
$this->calculateQuantityDiscounts($item, $context);
$this->calculateBundleDiscounts($item, $context);
$this->applyTaxRules($item, $context);
$this->roundFinalPrice($item);
}
}
Performance Optimizations
The 6.7 release includes significant performance improvements for cart calculations through:
- Caching Layers: Enhanced caching of rule matches and price calculations
- Lazy Loading: Calculation steps are only executed when necessary
- Batch Processing: Multiple items processed in parallel where possible
class CachedCartCalculator extends CartCalculator
{
public function calculate(Cart $cart, Context $context): Cart
{
$cacheKey = $this->generateCacheKey($cart, $context);
if ($this->cache->has($cacheKey)) {
return $this->cache->get($cacheKey);
}
$result = parent::calculate($cart, $context);
$this->cache->set($cacheKey, $result, 3600);
return $result;
}
}
New API Endpoints and Integration Points
Shopware 6.7 exposes several new API endpoints for cart calculation management:
POST /api/_action/cart/calculateGET /api/_action/cart/rules/availablePOST /api/_action/cart/rules/match
These endpoints provide programmatic access to the calculation engine, enabling custom integrations and third-party services.
Database Schema Changes
The upgrade includes several database schema modifications:
-- New price rule tables
CREATE TABLE `price_rule` (
`id` binary(16) NOT NULL,
`name` varchar(255) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '0',
`priority` int(11) NOT NULL DEFAULT '0',
`conditions` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`conditions`)),
`discount` decimal(10,2) NOT NULL,
`discount_type` varchar(50) NOT NULL,
`valid_from` datetime DEFAULT NULL,
`valid_until` datetime DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime DEFAULT NULL
);
Custom Rule Development
Developers can now create custom rules through the Rule system:
class CustomCategoryRule extends Rule
{
protected array $categoryIds;
public function match(RuleScope $scope): bool
{
if (!$scope instanceof ProductScope) {
return false;
}
$product = $scope->getProduct();
return in_array($product->getCategoryId(), $this->categoryIds);
}
public function getName(): string
{
return 'custom_category';
}
}
Configuration and Administration
The administration panel now includes enhanced configuration options for cart calculations, including:
- Real-time rule testing capabilities
- Performance impact monitoring
- Calculation step prioritization
- Advanced discount stacking rules
Impact on Existing Implementations
Merchants upgrading from previous versions should note that existing price rules may require migration. The new system maintains backward compatibility through automatic conversion processes, but performance characteristics have changed significantly.
The new cart calculation engine provides better handling of complex scenarios while maintaining excellent performance for typical use cases. The modular architecture allows for easier extension and customization without affecting core functionality.
Conclusion
Shopware 6.7's cart calculation and price rule system represents a significant advancement in e-commerce pricing capabilities. The new architecture offers improved performance, greater flexibility, and enhanced developer experience while maintaining full backward compatibility. Understanding these technical changes is crucial for developers and merchants looking to leverage the full power of Shopware's pricing engine.
The improvements in 6.7 make it possible to handle increasingly complex pricing scenarios while maintaining optimal performance, positioning Shopware as a robust solution for enterprise-level e-commerce operations with sophisticated pricing requirements.