当前位置: 首页 > 知识库问答 >
问题:

在WooCommerce电子邮件通知中显示产品自定义字段

穆招
2023-03-14

因此,当我在mytheme/functions.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));
}

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

它只在后端工作(产品编辑页面),它保存了变量,但没有显示在meta或add to card按钮之前或之后,我找不到解决方案
我尝试了echo,但仍然没有结果

所以我以为我是我的主题?不,我在其他vps上安装了wordpress,但我仍然没有显示文本字段text nothing

如果你知道这个问题,请友好地告诉我我做错了什么!谢啦

共有1个答案

岳劲
2023-03-14

自WooCommerce 3以来,此代码有点过时。此外,您正在使用textarea字段作为密码,它应该是一个简单的文本输入字段。

我已完全重新查看了您的代码:

// Custom function that handle your custom fields settings
function product_custom_fields_settings(){
    // Fieds Id key | Field label name (pairs)
    return array(
        'zoom_meeting_url'  => __('Meeting Url', 'woocommerce'),
        'zoom_meeting_id'   => __('Meeting Id', 'woocommerce'),
        'zoom_password'     => __('Meeting password', 'woocommerce'),
    );
}


add_action('woocommerce_product_options_general_product_data', 'display_product_custom_fields');
function display_product_custom_fields(){
    echo '<div class="product_custom_field">';

    // Loop through settings Fieds Id key | Field label name (pairs)
    foreach ( product_custom_fields_settings() as $key => $label ) {
        // Input text fields
        woocommerce_wp_text_input(array(
            'id' => '_'.$key,
            'label' => $label,
        ));
    }

    echo '</div>';
}

add_action('woocommerce_admin_process_product_object', 'save_product_custom_fields');
function save_product_custom_fields( $product ){
    foreach ( product_custom_fields_settings() as $key => $label ) {
        if ( isset($_POST['_'.$key]) ) {
            $product->update_meta_data( '_'.$key, sanitize_text_field( $_POST['_'.$key] ) );
        }
    }
}

add_action( 'woocommerce_email_after_order_table', 'product_custom_fields_to_email_notification', 10, 4 );
function product_custom_fields_to_email_notification( $order, $sent_to_admin, $plain_text, $email ) {
     // The HTML Structure
    $html_output = '<h3>'.__('Zoom meeting Details', 'woocommerce').'</h3>
    <div class="meeting-info"><table cellspacing="0" cellpadding="6"><tbody>';

    // Loop through order items
    foreach( $order->get_items() as $item ) {
        $product = $item->get_product();

        $html_output .= '<tr><th colspan="2">'.$item->get_name().'</th></tr>'; // Product name

        // Loop through settings Fieds Id key | Field label name (pairs)
        foreach ( product_custom_fields_settings() as $key => $label ) {
            if( $value = $product->get_meta('_'.$key) ) {
                $html_output .= '<tr><th>'.$label.':</th><td>'.$value.'</td></tr>';
            }
        }

        // … More code …
    }

    // The CSS styling
    $styles = '<style>
    .meeting-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
        color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
    .meeting-info table th, table.teilnehmer-info td{text-align: left; border-top-width: 4px;
        color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
    .meeting-info table td{text-align: left; border-top-width: 4px; color: #737373;
        border: 1px solid #e4e4e4; padding: 12px; word-break: break-all;}
    .meeting-info table tr.subtitle h3{margin: 0;}
    </style>';

    // The Output CSS + HTML
    echo $styles . $html_output . '</tbody></table></div><br>';
}

代码functions.php活动子主题(或活动主题)的文件中。测试和工作。

 类似资料:
  • 我正在尝试修改自动生成的WooCommerce订单电子邮件中的产品图像。请注意,我尝试使用钩子来实现这一点,而不是创建一个修改过的电子邮件模板。 首先,我在单一产品页面上有一个隐藏的输入。我有一些JS设置图像的ID,它基于产品的颜色(我用ACF制作的自定义属性)。 为了便于参考,我在购物车页面上做了以下工作: 这很好,但我很难按顺序复制电子邮件。到目前为止,我遇到了以下几点。这会在电子邮件中显示图

  • 在Woocommerce上,我已将电子邮件订单详细信息php模板文件中的变量更改为,但我仍然无法在电子邮件通知中显示图像: 我需要添加链接以及产品图像。一旦用户点击图像,它应该重定向到特定的页面。 将消息从假更改为真,但图像仍未显示在网站中。

  • 我有这个代码在我的function.php,我怎么能在管理邮件订单中分配这个字段值?谢谢!

  • 我在wordpress网站上安装了签出字段编辑器。我创建了自定义字段。但是,使用以下代码,PCCustomer字段将同时出现在发送给我和客户的“新订单”电子邮件中。相反,我希望它完全属于我。我试着编辑代码,但仍然不起作用。

  • 试图在woocommerce产品中获取高级自定义字段,以传递给管理员的新订单电子邮件。它仅用于管理员参考,并且特定于每个产品。我已经尝试过了,这让它在后端,但不是在电子邮件中。 我试着用它来获取电子邮件,但它扼杀了订购过程。它在后台运行,但在点击PlaceOrder后,它只刷新结帐页面,而不会转到感谢页面或生成电子邮件。 任何建议或帮助都将不胜感激。

  • 在Woocommerce中,我需要在完成的订单电子邮件上打印一个自定义域。 该模板在我的主题Woocommerce/Emails文件夹中是完全唯一的,因为默认模板不能满足我的需要。 因此,我试图显示以下内容:标题、数量和两个自定义字段 这需要包含在customer-completed-order.php电子邮件模板中,但我不确定打印它需要什么语法。 我在谷歌上找到了一些东西,但它不起作用: