设置自定义字段为必需的WooCommerce

时间:2018-07-10 14:29:09

标签: php wordpress woocommerce checkout shipping

如果我提出的问题不正确,请多多包涵。我正在尝试修改WooCommerce自定义字段以使其为必填字段。

该功能如下:

public function delivery_checkout_field( $checkout )
    {
        echo '<div class="checkout-delivery" id="delivery_checkout_field">';
        echo '<input class="js-picker-object" type="hidden" value="' . esc_attr( json_encode( $this->delivery_get_picker_object() ) ) . '" />';
        echo '<h3 class="with-description">' . __( 'Delivery Schedule', 'gastro-core' ) . '</h3>';
        echo '<p class="description">Enter your desired delivery time or leave blank, if you don\'t have one.</p>';

        woocommerce_form_field( 'delivery_date', array(
            'type'          => 'text',
            'class'         => array( 'delivery-field form-row-first' ),
            'label'         => __( 'Delivery Date' ),
            'placeholder'   => __( 'Enter delivery date.' ),
        ), $checkout->get_value( 'delivery_date' ) );

        woocommerce_form_field( 'delivery_time', array(
            'type'          => 'text',
            'class'         => array( 'delivery-field form-row-last' ),
            'label'         => __( 'Delivery Time' ),
            'placeholder'   => __( 'Enter delivery time.' ),
        ), $checkout->get_value( 'delivery_time' ) );


        echo '</div>';
    }

此部分保存字段。

// Save delivery data
    public function delivery_checkout_field_update( $order_id )
    {
        if ( !empty( $_POST['delivery_date'] ) ) {
            update_post_meta(
                $order_id,
                'Delivery Date',
                sanitize_text_field( $_POST['delivery_date'] )
            );
        }

        if ( !empty( $_POST['delivery_time'] ) ) {
            update_post_meta(
                $order_id,
                'Delivery Time',
                sanitize_text_field( $_POST['delivery_time'] )
            );
        }
    }

1 个答案:

答案 0 :(得分:0)

对于必需的HTML5属性和其他自定义属性,woocommerce函数具有custom_attributes选项。

'custom_attributes' => array( 'required' => 'required' )