【问题标题】:How To add Custom field in WooCommerce Product [duplicate]如何在 WooCommerce 产品中添加自定义字段 [重复]
【发布时间】:2020-06-18 11:21:18
【问题描述】:

我需要在 woo-commerce 产品中添加输入字段,以便我可以在前端显示它

【问题讨论】:

标签: wordpress woocommerce


【解决方案1】:

您可以将以下代码添加到您的活动 function.php 文件中

它将为您提供三个自定义字段

//this code for adding field in product backend
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields1');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields1()
{
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    // Custom Product Text Field
    woocommerce_wp_text_input(array(
        'id' => '_custom_product_text_field',
        'placeholder' => 'Custom Product Text Field',
        'label' => __('Zoom Meeting Url ', 'woocommerce'),
        'desc_tip' => 'true'
    ));
    //Custom Product Number Field
    woocommerce_wp_text_input(array(
        'id' => '_custom_product_number_field',
        'placeholder' => 'Custom Product Text Field',
        'label' => __('Meeting ID', 'woocommerce'),
        'type' => 'text',
        'custom_attributes' => array(
            // 'step' => 'any',
            // 'min' => '0'
        )
    ));
    //Custom Product  Textarea
    woocommerce_wp_textarea_input(array(
        'id' => '_custom_product_textarea',
        'placeholder' => 'Custom Product Text',
        'label' => __('Password', 'woocommerce')
    ));
    echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
    // Custom Product Text Field
    $woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
    if (!empty($woocommerce_custom_product_text_field))
        update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));
    // Custom Product Number Field
    $woocommerce_custom_product_number_field = $_POST['_custom_product_number_field'];
    if (!empty($woocommerce_custom_product_number_field))
        update_post_meta($post_id, '_custom_product_number_field', esc_attr($woocommerce_custom_product_number_field));
    // Custom Product Textarea Field
    $woocommerce_custom_procut_textarea = $_POST['_custom_product_textarea'];
    if (!empty($woocommerce_custom_procut_textarea))
        update_post_meta($post_id, '_custom_product_textarea', esc_html($woocommerce_custom_procut_textarea));
}

【讨论】:

    【解决方案2】:

    并且为了在前端显示该值,您可以遵循代码

    <?php
    $id = $product->id;
    echo get_post_meta($id, '_custom_product_text_field', true);
    echo get_post_meta($id, '_custom_product_number_field', true);
    echo get_post_meta($id, '_custom_product_textarea', true);
    ?>
    

    【讨论】:

      【解决方案3】:

      如果您希望将该值发送到电子邮件以便在客户购买产品后通过电子邮件发送给他们

      我们可以使用 WooCommerce 钩子在 function.php 中添加以下代码

      add_action( 'woocommerce_email_after_order_table', 'wdm_add_shipping_method_to_order_email', 10, 2 );
      function wdm_add_shipping_method_to_order_email( $order, $is_admin_email ) {
          echo '<h4>Zoom Meeting Details</h4>';
          $order_id = $order->get_id();
          $order = wc_get_order( $order_id ); //returns WC_Order if valid order 
          $items = $order->get_items();   //returns an array of WC_Order_item or a child class (i.e. WC_Order_Item_Product)
          foreach( $items as $item ) {
          $type = $item->get_type();
          $product_id = $item->get_product_id();
          echo "<p>" . get_the_title($product_id) . "</p>";
          echo "<p><strong>Zoom Meeting Url:</strong>" . get_post_meta($product_id, '_custom_product_text_field', true) . "</p>";
          echo "<p><strong>Meeting ID:</strong>" . get_post_meta($product_id, '_custom_product_number_field', true) . "</p>";
          echo "<p><strong>Password:</strong>" . get_post_meta($product_id, '_custom_product_textarea', true) . "</p>";
      
      
          //more code
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-25
        相关资源
        最近更新 更多