dongyi5425 2018-07-10 14:29
浏览 96

使自定义字段成为必需的WooCommerce

Please bear with me if I am not asking this question correctly. I am trying to modify a WooCommerce custom field to make it required.

The function is below:

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>';
    }

This section saves the fields.

// 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条回答 默认 最新

  • doufu9947 2018-09-23 22:03
    关注

    For the required HTML5 attribute and other custom attributes woocommerce function has the custom_attributes option.

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

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程