Sylius - 如何以编程方式执行结帐步骤?

时间:2017-02-19 02:08:08

标签: sylius

今天我遇到了一个我手册无法解决的问题。

我有一个案例,如果客户选择货到付款运输方式,我想跳过付款步骤。

到目前为止,我已经使用onCashOnDeliveryShipmentSelect方法在sylius.order.post_select_shipping上创建了一个事件监听器。

public function onCashOnDeliveryShipmentSelect(ResourceControllerEvent $event)
{
    /** @var Order $order */
    $order = $event->getSubject();

    /** @var Shipment $shipment */
    foreach ($order->getShipments() as $shipment) {
        /** @var ShippingMethod $method */
        $method = $shipment->getMethod();
        if ($method->getCashOnDelivery()) {
            $order = $this->selectPayment($order);
            $this->getStateMachine($order)->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);

            $this->container->get('sylius.manager.order')->flush();
        }
    }
}

private function selectPayment(Order $order)
{

    $this->getStateMachine($order)->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);

    $paymentMethod = $this->container
        ->get('sylius.repository.payment_method')
        ->findOneByCode('cash_on_delivery');

    foreach ($order->getPayments() as $payment) {
        $payment->setMethod($paymentMethod);
    }

    $this->flush();
    return $order;
}

private function getStateMachine(Order $order)
{
    $stateMachineFactory = $this->container->get('sm.factory');
    $stateMachine = $stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH);

    return $stateMachine;
}

private function flush()
{
    $this->container->get('sylius.manager.order')->flush();
}

一旦我完成结账流程并选择cashOnDelivey标志设置为true的方法,即使订单处理正确,我也会收到重定向循环错误。

1 个答案:

答案 0 :(得分:0)

您的业务假设是错误的。

货到付款 - 这是付款方式,而不是送货方式。

重新思考你想做什么,然后再回来。如果您想要一种只能在交货时付款的送货方式,那么当订单总数为0时,您必须执行类似于我们的跳过付款步骤。)