如何在WooCommerce产品中添加自定义字段

时间:2020-06-18 11:21:18

标签: wordpress woocommerce

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

3 个答案:

答案 0 :(得分:0)

您可以将以下代码添加到活动的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));
}

答案 1 :(得分:0)

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

<?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);
?>

答案 2 :(得分:0)

如果您希望将该值发送到电子邮件中,以便在客户购买产品后向客户发送电子邮件

我们可以使用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
}
}