英文:
Check if promotion valid on product page
问题
我正在尝试在产品详情页面上添加一些内容,以显示是否有促销活动适用于该产品。让我们假设我有促销活动的ID或代码。是否有现成的机制可以实现这一点?
由于我搜索了但没有找到现成的机制,我考虑创建一个临时购物车,将产品添加到其中,添加促销活动,重新计算并查看促销活动是否生效。我的尝试如下:
$tempCart = $this->cartService->createNew(Uuid::randomHex());
$lineItem = $this->lineItemFactoryRegistry->create([
'type' => LineItem::PRODUCT_LINE_ITEM_TYPE,
'referencedId' => $productId,
'quantity' => 1,
], $salesChannelContext);
$this->cartService->add($tempCart, $lineItem, $salesChannelContext);
$promotionItem = $this->promotionLineItemFactory->create([
'referencedId' => 'a74cdde1e8bf41a3b2997ff3d5b61ed9',
], $salesChannelContext);
$this->cartService->add($tempCart, $promotionItem, $salesChannelContext);
$this->promotionProcessor->process($tempCart->getData(), $tempCart, $tempCart, $salesChannelContext, new CartBehavior());
$tempCart->markModified();
$tempCart2 = $this->cartService->recalculate($tempCart, $salesChannelContext);
但是什么都没有发生。换句话说,购物篮已重新计算,但促销活动的行消失了。尽管促销活动肯定是有效的并且可用,因为除了销售渠道之外没有任何条件。
有人可以给我一些如何解决这个问题的提示吗?也许我开始的方式不对?
英文:
I am trying to add something on the product detail page to show if a promotion will be active for it. Let's assume I have the promotion id or its code. Is there any ready mechanism for this?
Due to the fact that I searched but didn't find one I thought in that case I could create a temporary shopping cart, add the product to it, add the promotion, recalculate and see if the promotion is active or not. My attempt is as follows:
$tempCart = $this->cartService->createNew(Uuid::randomHex());
$lineItem = $this->lineItemFactoryRegistry->create([
'type' => LineItem::PRODUCT_LINE_ITEM_TYPE,
'referencedId' => $productId,
'quantity' => 1,
], $salesChannelContext);
$this->cartService->add($tempCart, $lineItem, $salesChannelContext);
$promotionItem = $this->promotionLineItemFactory->create([
'referencedId' => 'a74cdde1e8bf41a3b2997ff3d5b61ed9',
], $salesChannelContext);
$this->cartService->add($tempCart, $promotionItem, $salesChannelContext);
$this->promotionProcessor->process($tempCart->getData(), $tempCart, $tempCart, $salesChannelContext, new CartBehavior());
$tempCart->markModified();
$tempCart2 = $this->cartService->recalculate($tempCart, $salesChannelContext);
But nothing happens. In the sense the basket is recalculated, but the line with the promotion disappears. Even though the promotion is certainly valid and available, because there are no conditions except the sales channel.
Can anyone give me a hint on how to approach this? Maybe I have started wrong?
答案1
得分: 0
你可以尝试在购物车中添加一个占位促销码,然后使用PromotionCollector
服务,看看是否会出现错误,试图根据该代码收集实际的促销活动。理论上,只要代码有效,收集器也应考虑所有限制和条件。
$uniqueKey = Uuid::randomHex();
$code = 'foobar';
$item = new LineItem($uniqueKey, PromotionProcessor::LINE_ITEM_TYPE);
$item->setLabel($uniqueKey);
$item->setGood(false);
$item->setReferencedId($code);
$item->setPriceDefinition(new PercentagePriceDefinition(0));
$cart->add($item);
$behavior = new CartBehavior();
$container->get(PromotionCollector::class)->collect($cart->getData(), $cart, $salesChannelContext, $behavior);
var_dump($cart->getErrors()->count());
var_dump($cart->getErrors()->first()->getMessage());
// int(1)
// string(37) "促销活动 foobar 未找到!"
英文:
You could try the PromotionCollector
service with a placeholder promotion added to the cart, to see if it yields errors trying to collect the actual promotion based on the code. In theory the collector should also consider all limitations and conditions, given the code is valid.
$uniqueKey = Uuid::randomHex();
$code = 'foobar';
$item = new LineItem($uniqueKey, PromotionProcessor::LINE_ITEM_TYPE);
$item->setLabel($uniqueKey);
$item->setGood(false);
$item->setReferencedId($code);
$item->setPriceDefinition(new PercentagePriceDefinition(0));
$cart->add($item);
$behavior = new CartBehavior();
$container->get(PromotionCollector::class)->collect($cart->getData(), $cart, $salesChannelContext, $behavior);
var_dump($cart->getErrors()->count());
var_dump($cart->getErrors()->first()->getMessage());
// int(1)
// string(37) "Promotion with code foobar not found!"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论