Shopware 6在订单提交中以编程方式更改运输方法

发布于 2025-01-29 10:20:20 字数 2675 浏览 0 评论 0原文

我有两种具有不同价格矩阵的相同运输方法,具体取决于客户距离,应选择两种运输方法之一。这必须在结帐订单确认书上以编程方式进行,我查看了购物车收集器,但这是关于更改物品价格的。我没有发现有关运输方法更改的文档中涵盖的任何内容。

以下是我尝试更改它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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的巷 2025-02-05 10:20:20

对于所有找不到解决方案的方法:

我创建了一项服务来处理它。此功能将$ shippingmethodid设置为给定的功能。

public function changeShippingMethod(string $shippingMethodId, SalesChannelContext $context)
    {
        $contextToken = $context->getToken();

        $dataBag = new DataBag([
            'shippingMethodId' => $shippingMethodId,
        ]);

        $this->contextSwitcher->update($dataBag, $context);
    }

不要忘记添加使用语句

use Shopware\Core\System\SalesChannel\SalesChannel\SalesChannelContextSwitcher;
use Shopware\Core\Framework\Validation\DataBag\DataBag;

和约束者

private $contextSwitcher;

public function __construct(
    SalesChannelContextSwitcher $contextSwitcher
) {
    $this->contextSwitcher = $contextSwitcher;
}

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.

public function changeShippingMethod(string $shippingMethodId, SalesChannelContext $context)
    {
        $contextToken = $context->getToken();

        $dataBag = new DataBag([
            'shippingMethodId' => $shippingMethodId,
        ]);

        $this->contextSwitcher->update($dataBag, $context);
    }

Don't forget to add the use statements

use Shopware\Core\System\SalesChannel\SalesChannel\SalesChannelContextSwitcher;
use Shopware\Core\Framework\Validation\DataBag\DataBag;

and a constructer

private $contextSwitcher;

public function __construct(
    SalesChannelContextSwitcher $contextSwitcher
) {
    $this->contextSwitcher = $contextSwitcher;
}
糖果控 2025-02-05 10:20:20

我完全采用另一种方法,以上所有方法似乎都对我有用。项目价格更新正在发生,但没有发货。

因此,这是您需要的,此上下文switcher依赖关系I注入了工作,并且可以完成工作。 update()可用于切换装运方法的方法。

SalesChannelContextSwitcher $contextSwitcher,

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.

SalesChannelContextSwitcher $contextSwitcher,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文