首页 文章

自定义选择结帐页面Woocommerce

提问于
浏览
0

在结帐页面上的Woocommerce中使用ACF,我在其中创建了一个自定义结帐字段,用于选择交付方式 .

如何使它,以便当您单击“确认订单”按钮时,所选的选择字段将传递到总订单本身,并在订单信函中发送 .

我在checkout-form.php文件中创建了这个转发器字段:

/**
 * Checkout Form
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.3.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

wc_print_notices();

do_action( 'woocommerce_before_checkout_form', $checkout );

// If checkout registration is disabled and not logged in, the user cannot checkout
if ( ! $checkout->is_registration_enabled() && $checkout->is_registration_required() && ! is_user_logged_in() ) {
    echo apply_filters( 'woocommerce_checkout_must_be_logged_in_message', __( 'You must be logged in to checkout.', 'woocommerce' ) );
    return;
}

?>

<form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( wc_get_checkout_url() ); ?>" enctype="multipart/form-data">

    <?php if ( $checkout->get_checkout_fields() ) : ?>

        <?php do_action( 'woocommerce_checkout_before_customer_details' ); ?>

        <div class="col2-set" id="customer_details">
            <div class="col-12">
                <?php do_action( 'woocommerce_checkout_billing' ); ?>
            </div>
                <?php if( have_rows('shipping_methods') ): ?>
                    <div class="shipp-method">
                        <label for="shipping_method">Способ доставки:</label>
                        <select name="shipping_method" id="shipping_method" class="select_shipping">
                             <?php while( have_rows('shipping_methods') ): the_row(); 
                             $method = get_sub_field('shipping_method');
                             ?> 
                                <option><?php echo $method; ?></option>
                           <?php endwhile; ?>
                        </select>
                    </div>    
                <?php endif; ?>
            <div class="col-12">
                <?php do_action( 'woocommerce_checkout_shipping' ); ?>
            </div>
        </div>

        <?php do_action( 'woocommerce_checkout_after_customer_details' ); ?>

    <?php endif; ?>

    <h3 id="order_review_heading"><?php _e( 'Your order', 'woocommerce' ); ?></h3>

    <?php do_action( 'woocommerce_checkout_before_order_review' ); ?>

    <div id="order_review" class="woocommerce-checkout-review-order">
        <?php do_action( 'woocommerce_checkout_order_review' ); ?>
    </div>

    <?php do_action( 'woocommerce_checkout_after_order_review' ); ?>

</form>

<?php do_action( 'woocommerce_after_checkout_form', $checkout ); ?>

1 回答

  • 0

    创建 ACF 字段不足以向Woocommerce结帐添加自定义字段 .

    Woocommerce提供了几个过滤器,因此可以修改其结帐表单字段,最突出的是: woocommerce_checkout_fields . 虽然您可以使用ACF为Woocommerce结帐表单中的其他选择字段创建一组选项,但您需要使用过滤器注册它们 . 例如:

    add_filter( 'woocommerce_checkout_fields', function($fields) {
         // Get the field you need here and mutate $fields
         ...
         return $fields;
    });
    

    这样做可确保将数据传递到服务器 . 您还需要使用 woocommerce_checkout_update_order_meta 操作将它们作为 order meta 保存到数据库中 . 例:

    add_action( 'woocommerce_checkout_update_order_meta', function( $order_id) {
       update_post_meta( $order_id, [Your custom field name], sanitize_text_field( $_POST['your_custom_field_name']));
    });
    

    此外,这里的一个注意事项是,默认情况下,Woocommerce已经提供开箱即用的运输功能,这意味着您可能不想重新发明轮子 . 如果您想添加自己的其他送货方式,可能需要了解如何创建自定义送货方式 .

    有关如何自定义Woocommerce结帐的更多信息:

    https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

    和定制运输:

    https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098

相关问题