在确认订单页面中的Shopware送货地址中自定义验证

时间:2018-08-02 11:50:18

标签: shopware

如何在确认订单页面的商店商品送货地址中添加我的自定义验证,一旦成功完成该过程即可确认订单。请说明如何执行此操作。

1 个答案:

答案 0 :(得分:1)

创建地址时,您是否只能添加其他字段来进行地址处理?

public static function getSubscribedEvents()
{
    return array(
        'Shopware_Form_Builder' => ['onFormBuild', 1000]
    );
}

public function onFormBuild(\Enlight_Event_EventArgs $event)
{
    if (
        (   $event->getReference() !== \Shopware\Bundle\AccountBundle\Form\Account\PersonalFormType::class &&
            $event->getReference() !== \Shopware\Bundle\AccountBundle\Form\Account\AddressFormType::class  )
    ) {
        return;
    }

    /** @var \Symfony\Component\Form\Form $builder */
    $builder = $event->getBuilder();

    $builder->get('additional')
        ->add('yourFieldName', \Symfony\Component\Form\Extension\Core\Type\TextType::class, [
            'constraints' => [
                new NotBlank()
            ]
        ]);
}

如果否,那么您应该订阅checkout postpatch,然后检查您想要的内容:

public static function getSubscribedEvents()
{
    return array(
        'Enlight_Controller_Action_PreDispatch_Frontend_Checkout' => 'onFrontendPreDispatchCheckout',
    );
}

/**
 * @param \Enlight_Controller_ActionEventArgs $args
 */
public function onFrontendPreDispatchCheckout(\Enlight_Controller_ActionEventArgs $args)
{
    /**@var $subject \Shopware_Controllers_Frontend_Checkout */
    $subject = $args->getSubject();
    $request = $subject->Request();
    $response = $subject->Response();
    $action = $request->getActionName();
    $view = $subject->View();

    if (!$request->isDispatched() || $response->isException() || 
    // only in case action is payment or confirm we should chcek it
    ($action != 'payment' && $action != 'confirm') ||
    !$view->hasTemplate()
    ) {
        return;
    }

    $validation = $this->thereWeCheckIt();
    if (
        $validation['message']
    ) {
                $subject->forward('index', 'account', 'frontend', array(
                    'errorVariable' => $validation['message'],
                ));
    }
}

然后,您还需要发布调度帐户控制器并向客户显示errorVariable