我正在开发一个Shopware 6插件,并在其产品详细信息页面上设置产品的运费CustomsProductDetailRoute
。
我制定了一个运送到法国的规则。现在,当我:
- 用我的ID登录Shopware商店。
- 将法国地址设置为默认送货地址。
- 我看到了到法国的运费。
这不应该发生。我想显示到销售渠道默认国家/地区的运费。
/**
* @Entity("product")
* @Route("/store-api/product/{productId}", name="store-api.detail", methods={"POST"})
*/
public function load(string $productId, Request $request, SalesChannelContext $context, Criteria $criteria): ProductDetailRouteResponse
{
// We must call this function when using the decorator approach
$productDetailResponse = $this->decorated->load($productId, $request, $context, $criteria);
//Get the current Cart(CartService) and add a product as a new line item
$cart = $this->cartService->getCart(self::DUMMY_TOKEN, $context);
// dd($cart->getShippingCosts());
// To avoid the cart from being persisted after the calculation, you can set a dummy value as token for the cart
$cart->setToken(self::DUMMY_TOKEN);
// Get the default shipping address for the current sales channel from the SalesChannelContext
// dd($context->getSalesChannel()->getCountryId());
// Create a new lineItem of product
$productLineItem = new LineItem($productId, LineItem::PRODUCT_LINE_ITEM_TYPE, $productId);
$productLineItem->setGood(true);
$productLineItem->setStackable(true);
$productLineItem->setRemovable(true);
$cart->add($productLineItem);
// Setting the isRecalculation flag of CartBehavior should prevent cart to be persisted
$behaviorContext = new CartBehavior([], true, true);
$cartLoaded = $this->cartRuleLoader->loadByCart($context, $cart, $behaviorContext)->getCart();
// Access calculated shipping cost from the cart for the specific product
$expectedShippingCosts = $cartLoaded->getShippingCosts();
// Set the expected shipping cost to the extension of the $productDetailResponse's object variable
// dd($productDetailResponse->getResult());
$productDetailResponse->getProduct()->addExtension(
'expectedShippingCosts', $expectedShippingCosts);
return $productDetailResponse;
}
我正在尝试使用$context->getSalesChannel()->getCountryId()
从SalesMailContext获取当前销售渠道的默认送货地址。我不确定如何将其与购物车关联。由于$cart
没有设置送货国家/地区的方法。我如何修改代码以始终显示为销售渠道的默认国家/地区计算的运费?我希望它忽略用户的默认送货地址国家/地区。
问候,Lodhi
1条答案
按热度按时间pepwfjgg1#
您可以将当前发货方式id与销售渠道默认发货方式id进行比较,如果不一致,使用注入的
shipping_method.repository
获取ShippingMethodEntity
,然后克隆SalesChannelContext
的示例(以避免为堆栈的其余部分改变对象)在克隆的上下文中,您可以通过调用assign
方法来替换要使用的发送方法。如果发货地点的国家/地区也应该设置为默认国家/地区,则可以扩展上述行为,将发货地点的国家/地区也替换为分配给销售渠道的默认国家/地区。