【问题标题】:How to add my custom field to the Woocommerce order process (Check out form, order details and email process)如何将我的自定义字段添加到 Woocommerce 订单流程(查看表单、订单详细信息和电子邮件流程)
【发布时间】:2019-10-23 03:42:33
【问题描述】:

我为我的 woocommerce 产品创建了客户数据字段。我想在预订的每一步都显示此信息(日期、时间和地点)。在结帐页面,订单详情和电子邮件。请你帮助我好吗 ?在下面找到我用于 md 自定义字段的代码。非常感谢

add_action( 'woocommerce_product_options_inventory_product_data', 'woocom_inventory_product_data_custom_field' );


function woocom_Inventory_product_data_custom_field() {
  // Create a custom text field

  // Text Field
  woocommerce_wp_text_input( 
    array( 
      'id' => 'venue', 
      'label' => __( 'Venue', 'woocommerce' ), 
      'placeholder' => 'Venue',
      'desc_tip' => 'true',
      'description' => __( 'Enter the location here.', 'woocommerce' ) 
    )
  );

  woocommerce_wp_text_input( 
    array( 
      'id' => 'date', 
      'label' => __( 'Date', 'woocommerce' ), 
      'placeholder' => 'date',
      'desc_tip' => 'true',
      'description' => __( 'Enter the date here.', 'woocommerce' ) 
    )
  );

woocommerce_wp_text_input( 
    array( 
      'id' => 'time', 
      'label' => __( 'Time', 'woocommerce' ), 
      'placeholder' => 'time',
      'desc_tip' => 'true',
      'description' => __( 'Enter the time here.', 'woocommerce' ) 
    )
  );        
}



// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_save_inventory_proddata_custom_field' );

/** Hook callback function to save custom fields information */
function woocom_save_inventory_produdata_custom_field( $post_id ) {
  // Save Text Field
  $text_field = $_POST['venue'];
  if( ! empty( $text_field ) ) {
     update_post_meta( $post_id, 'venue', esc_attr( $text_field ) );
  }

    $text_field = $_POST['date'];
  if( ! empty( $text_field ) ) {
     update_post_meta( $post_id, 'date', esc_attr( $text_field ) );
  }

     $text_field = $_POST['time'];
  if( ! empty( $text_field ) ) {
     update_post_meta( $post_id, 'time', esc_attr( $text_field ) );
  }

}

【问题讨论】:

    标签: wordpress email woocommerce custom-fields


    【解决方案1】:

    在后端添加自定义字段:

    这将在 WooCommerce 产品的产品数据部分添加一个新字段。

     /**
     * Display the custom text field
     * @since 1.0.0
     */
    function cfwc_create_custom_field() {
     $args = array(
     'id' => 'custom_text_field_title',
     'label' => __( 'Custom Text Field Title', 'cfwc' ),
     'class' => 'cfwc-custom-field',
     'desc_tip' => true,
     'description' => __( 'Enter the title of your custom text field.', 'ctwc' ),
     );
     woocommerce_wp_text_input( $args );
    }
    add_action( 'woocommerce_product_options_general_product_data', 'cfwc_create_custom_field' );
    

    挂钩自定义字段函数:

    为确保自定义字段显示在正确的位置——在这种情况下,它只是在常规选项卡上——我们需要将我们的函数挂钩到正确的操作:woocommerce_product_options_general_product_data。

    如果您想将自定义字段添加到不同的选项卡,您可以尝试以下操作:

    • woocommerce_product_options_inventory_product_data
    • woocommerce_product_options_shipping

    保存自定义字段值

    使用此代码,我们有一种非常简单的方法可以使用标准 WooCommerce 功能和操作向产品添加自定义字段。我们需要做的就是在产品更新时保存该字段的值。

    /**
     * Save the custom field
     * @since 1.0.0
     */
    function cfwc_save_custom_field( $post_id ) {
     $product = wc_get_product( $post_id );
     $title = isset( $_POST['custom_text_field_title'] ) ? $_POST['custom_text_field_title'] : '';
     $product->update_meta_data( 'custom_text_field_title', sanitize_text_field( $title ) );
     $product->save();
    }
    add_action( 'woocommerce_process_product_meta', 'cfwc_save_custom_field' );
    

    此功能在产品首次发布或更新时运行。它在我们的自定义字段中查找一个值,对其进行清理,然后使用几个版本前引入 WooCommerce 的 CRUD 方法将其保存为产品元数据。

    该函数与 woocommerce_process_product_meta 操作挂钩。

    向购物车添加自定义值

    假设产品成功验证,它将被添加到 WooCommerce 购物车对象中。我们可以将我们自己的元数据添加到这个对象,以便我们可以在稍后的过程中使用它,例如在购物车和结帐页面以及订单和电子邮件中。

    /**
     * Add the text field as item data to the cart object
     * @since 1.0.0
     * @param Array         $cart_item_data Cart item meta data.
     * @param Integer   $product_id     Product ID.
     * @param Integer   $variation_id   Variation ID.
     * @param Boolean   $quantity           Quantity
     */
    function cfwc_add_custom_field_item_data( $cart_item_data, $product_id, $variation_id, $quantity ) {
     if( ! empty( $_POST['cfwc-title-field'] ) ) {
     // Add the item data
     $cart_item_data['title_field'] = $_POST['cfwc-title-field'];
     }
     return $cart_item_data;
    }
    add_filter( 'woocommerce_add_cart_item_data', 'cfwc_add_custom_field_item_data', 10, 4 );
    

    此函数与 woocommerce_add_cart_item_data 挂钩,该函数会在将产品添加到购物车时过滤传递给购物车对象的数据。所以在这里我们检查 cfwc-title-field 是否有一个值,然后将其添加到 $cart_item_data。

    在购物车和结帐中显示自定义字段

    确保我们的自定义字段在购物车和结帐表单中对用户可见,现在我们需要在用户结帐时将其值传递给订单。

    /**
     * Add custom field to order object
     */
    function cfwc_add_custom_data_to_order( $item, $cart_item_key, $values, $order ) {
     foreach( $item as $cart_item_key=>$values ) {
     if( isset( $values['title_field'] ) ) {
     $item->add_meta_data( __( 'Custom Field', 'cfwc' ), $values['title_field'], true );
     }
     }
    }
    add_action( 'woocommerce_checkout_create_order_line_item', 'cfwc_add_custom_data_to_order', 10, 4 );
    

    我们可以使用 woocommerce_checkout_create_order_line_item 操作非常轻松地做到这一点。

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 2019-07-02
      • 2021-10-23
      • 1970-01-01
      • 2018-08-15
      • 2022-01-10
      相关资源
      最近更新 更多