Shopware 6在订单提交中以编程方式更改运输方法
我有两种具有不同价格矩阵的相同运输方法,具体取决于客户距离,应选择两种运输方法之一。这必须在结帐订单确认书上以编程方式进行,我查看了购物车收集器,但这是关于更改物品价格的。我没有发现有关运输方法更改的文档中涵盖的任何内容。
以下是我尝试更改它OnorderValidation
。
private function getShippingMethod(string $id, Context $context): ?ShippingMethodEntity
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter("id", $id));
$criteria->setLimit(1);
$result = $this->shippingMethodRepository->search($criteria, $context);
return $result->getEntities()->first();
}
public function onOrderValidation(BuildValidationEvent $event)
{
$cart = $this->cartService->getCart($this->salesChannelContext->getToken(), $this->salesChannelContext);
$delivery = $cart->getDeliveries()->first();
//test
$shippingMethod = $this->getShippingMethod($this->getShippingMethodZoneXId(), $event->getContext());
$delivery->setshippingMethod($shippingMethod);
$cartDeliveries = new DeliveryCollection();
$cartDeliveries->add($delivery);
$cart->addDeliveries($cartDeliveries);
$this->cartService->setCart($cart);
...
}
在上面的代码中,我有购物车
对象,<代码> velivery 。我得到需要设置的运输方法,但没有更新。
另外,我需要重新计算运输价格,这是正确的方法。任何建议将不胜感激。
更新:我还尝试了Shopware doc 收集/过程,但这也不起作用。
public function collect(CartDataCollection $data, Cart $original, SalesChannelContext $context, CartBehavior $behavior): void
{
$shippingMethod = $this->getShippingMethod($this->getShippingMethodZoneXId(), $context->getContext());
$shippingMethodId = $shippingMethod->getId();
$key = self::buildShippingKey($shippingMethodId);
$data->set($key, $shippingMethod);
}
public function process(CartDataCollection $data, Cart $original, Cart $toCalculate, SalesChannelContext $context, CartBehavior $behavior): void
{
// change delviery
$deliveries = $this->builder->build($toCalculate, $data, $context, $behavior);
$deliveryOriginal = $deliveries->first();
if($deliveryOriginal === null) {
return;
}
$shippingMethod = $this->getShippingMethod($this->getShippingMethodZoneXId(), $context->getContext());
$deliveries->first()->setShippingMethod($shippingMethod);
$this->deliveryCalculator->calculate($data, $toCalculate, $deliveries, $context);
$toCalculate->setDeliveries($deliveries);
}
I have two of same shipping methods with different price matrix, depending on customer distance, one of the two shipping method should be selected. This has to be done programmatically on checkout order confirmation, I looked at the cart collector, but it's about changing price of an item. I didn't find anything covered in documentation regarding shipping method change.
Following is my attempt to change it onOrderValidation
.
private function getShippingMethod(string $id, Context $context): ?ShippingMethodEntity
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter("id", $id));
$criteria->setLimit(1);
$result = $this->shippingMethodRepository->search($criteria, $context);
return $result->getEntities()->first();
}
public function onOrderValidation(BuildValidationEvent $event)
{
$cart = $this->cartService->getCart($this->salesChannelContext->getToken(), $this->salesChannelContext);
$delivery = $cart->getDeliveries()->first();
//test
$shippingMethod = $this->getShippingMethod($this->getShippingMethodZoneXId(), $event->getContext());
$delivery->setshippingMethod($shippingMethod);
$cartDeliveries = new DeliveryCollection();
$cartDeliveries->add($delivery);
$cart->addDeliveries($cartDeliveries);
$this->cartService->setCart($cart);
...
}
In the above code I have cart
object, and delivery
. I get the shipping method which I need to set, but it doesn't get updated.
Also i need to recalculate shipping price, what is the correct way to do it. any suggestion will be appreciated.
Update: I have also tried from shopware doc collect/process, but that also didn't work.
public function collect(CartDataCollection $data, Cart $original, SalesChannelContext $context, CartBehavior $behavior): void
{
$shippingMethod = $this->getShippingMethod($this->getShippingMethodZoneXId(), $context->getContext());
$shippingMethodId = $shippingMethod->getId();
$key = self::buildShippingKey($shippingMethodId);
$data->set($key, $shippingMethod);
}
public function process(CartDataCollection $data, Cart $original, Cart $toCalculate, SalesChannelContext $context, CartBehavior $behavior): void
{
// change delviery
$deliveries = $this->builder->build($toCalculate, $data, $context, $behavior);
$deliveryOriginal = $deliveries->first();
if($deliveryOriginal === null) {
return;
}
$shippingMethod = $this->getShippingMethod($this->getShippingMethodZoneXId(), $context->getContext());
$deliveries->first()->setShippingMethod($shippingMethod);
$this->deliveryCalculator->calculate($data, $toCalculate, $deliveries, $context);
$toCalculate->setDeliveries($deliveries);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于所有找不到解决方案的方法:
我创建了一项服务来处理它。此功能将$ shippingmethodid设置为给定的功能。
不要忘记添加使用语句
和约束者
For all that can't find the solution for this:
I've created a service to handle that. This function is setting the $shippingMethodId to the given one.
Don't forget to add the use statements
and a constructer
我完全采用另一种方法,以上所有方法似乎都对我有用。项目价格更新正在发生,但没有发货。
因此,这是您需要的,此
上下文switcher
依赖关系I注入了工作,并且可以完成工作。update()
可用于切换装运方法的方法。I completely ended up with another approach, none of the above seem to work for me. Item price updation was taking place but not shipment.
So here's what you need, this
contextSwitcher
dependency i injected and it did the job.update()
method that can be used to switch shipment method.