tombroucke / otomaties-woocommerce-extra-checkout-steps
此软件包最新版本(1.0.0)没有可用的许可信息。
1.0.0
2024-02-14 13:58 UTC
Requires
- illuminate/config: ^10.43
- illuminate/container: ^10.43
- illuminate/support: ^10.43
- stoutlogic/acf-builder: ^1.12
README
此插件提供了一种向结账过程添加额外步骤的方法
先决条件
- PHP 8.x
- ACF PRO
安装
composer require tombroucke/otomaties-events
用法
- 激活插件
- 添加一个或多个额外的结账页面,例如 checkout/billing,checkout/shipping,checkout/overview
- 在“额外结账步骤”选项页中将这些页面按正确顺序添加。
- 为每个额外页面创建一个短代码。例如:
add_shortcode('checkout_billing_information', function () { return view('shortcodes.checkout-personal-details', [ 'adminPostUrl' => admin_url('admin-post.php'), 'data' => WooCommerceExtraCheckoutSteps()->make(\Otomaties\WooCommerceExtraCheckoutSteps\Helpers\Steps::class)->find(get_the_ID())->getData(), ])->toHtml(); });
- 添加短代码的视图。确保添加额外的步骤字段,并为其添加一个唯一的名称(
WooCommerceExtraCheckoutSteps()->make('form-fields', ['name' => 'personal_details'])
)。@if(function_exists('WooCommerceExtraCheckoutSteps')) {!! woocommerce_output_all_notices() !!} <div> {!! woocommerce_checkout_login_form() !!} </div> <form method="POST" action="{!! $adminPostUrl !!}"> <div class="mb-3"> <label for="custom_field" class="form-label">Custom field</label> <input type="text" name="custom_field" class="form-control" id="custom_field" value="{!! $data['custom_field'] ?? '' !!}"> </div> {!! wc_get_template('checkout/form-billing.php', array('checkout' => WC()->checkout)) !!} {!! WooCommerceExtraCheckoutSteps()->make('form-fields', ['name' => 'personal_details']) !!} <button type="submit" class="btn btn-primary"> {!! __('Continue', 'text-domain') !!} </button> </form> @endif
- 对于每个页面:如有需要,添加一些验证逻辑
add_action('woocommerce_extra_checkout_steps_verify_fields', function($stepName, $currentStep, $nextStep, $steps) { if ($stepName === 'personal_details') { $fields = [ 'billing_first_name' => [ 'label' => __('First name', 'text-domain'), 'validate' => [ 'required', [ 'function' => function ($name) { return strlen($name) > 2; }, 'message' => __('First name should be at least 2 letters', 'text-domain') ] ] ], 'billing_last_name' => [ 'label' => __('Last name', 'text-domain'), 'validate' => ['required'] ], 'billing_postcode' => [ 'label' => __('Postcode', 'remmerie'), 'validate' => [ 'required', [ 'function' => function ($postcode) { return \WC_Validation::is_postcode($postcode, isset($_POST['billing_country']) ? $_POST['billing_country'] : null); }, 'message' => sprintf(__( '%s is not a valid postcode / ZIP.', 'woocommerce' ), '<strong>' . __('Postcode', 'remmerie') . '</strong>') ] ] ], 'billing_phone' => [ 'label' => __('Phone', 'remmerie'), 'validate' => [ 'required', [ 'function' => function ($phone) { return \WC_Validation::is_phone($phone); }, 'message' => sprintf(__( '%s is not a valid phone number.', 'woocommerce' ), '<strong>' . __('Phone', 'remmerie') . '</strong>') ] ] ], 'billing_email' => [ 'label' => __('Email', 'remmerie'), 'validate' => [ 'required', [ 'function' => function ($email) { return \WC_Validation::is_email($email); }, 'message' => sprintf(__( '%s is not a valid email address.', 'woocommerce' ), '<strong>' . __('Email', 'remmerie') . '</strong>') ] ] ], // ... ]; $currentStep->verifyFields($fields); } }, 10, 4)
- 数据保存到
WC()->session
中的步骤。如有需要,您可以使用钩子将数据保存到其他地方add_action('woocommerce_extra_checkout_steps_data_saved', function($callback, $data, $currentStep, $nextStep) { // save the data }, 10, 4)
- 默认的 WooCommerce 字段由 WooCommerce 保存。可能您需要将自定义字段保存到您的订单中。您可以使用
woocommerce_checkout_order_processed
来实现这一点。add_action('woocommerce_checkout_order_processed', function ($orderId, $postedData, $order) { WooCommerceExtraCheckoutSteps()->make(\Otomaties\WooCommerceExtraCheckoutSteps\Helpers\Steps::class) ->each(function($step) { $stepData = $step->getData(); $order->update_meta_data('custom_field', $stepData['custom_field']); $order->save(); }); });