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

在电子商务管理电子邮件通知中显示产品自定义字段值

公西培
2023-03-14

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

//1.1 Display field in admin
add_action('woocommerce_product_options_inventory_product_data', 'woocommerce_product_custom_fields');
function woocommerce_product_custom_fields(){
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    woocommerce_wp_text_input(
        array(
            'id' => '_custom_product_text_field',
            'placeholder' => 'Name',
            'label' => __('customtext', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );    
    echo '</div>';
}

//1.2 Save field
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields_save($post_id){
    $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));

}

共有2个答案

厍和颂
2023-03-14

这是我的密码

//1.1 Display custom field 
add_action('woocommerce_product_options_inventory_product_data', 'woocommerce_product_custom_fields');
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields(){
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    woocommerce_wp_text_input(
        array(
            'id' => '_custom_product_text_field',
            'placeholder' => 'text',
            'label' => __('Custom', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );    
    echo '</div>';
}

// 1.2 Save this field in admin
function woocommerce_product_custom_fields_save($post_id){
    $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));

}

// 1.3 Save custom field value in order items meta data
add_action( 'woocommerce_add_order_item_meta', 'add_custom_field_to_order_item_meta', 20, 3 );
function add_custom_field_to_order_item_meta( $item_id, $values, $cart_item_key ) {

    $custom_field_value = get_post_meta( $values['data']->get_id(), '_custom_product_text_field', true );
    if ( ! empty($$custom_field_value) )
        wc_add_order_item_meta( $item_id, __('Custom label', 'woocommerce'), $custom_field_value );
}
欧阳洲
2023-03-14

更新2有两个非常相似的备选方案(第一个更好):

若要在电子邮件中轻松显示此产品自定义字段值(也在订单中),您可以使用以下代码,这将在订单项中保存此数据,一旦订单被放置。

第一个备选代码(使用WC 3最新CRUD方法的最佳新代码):

// 1.3 Save custom field value in order items meta data (only for Woocommerce versions 3+)
add_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_field_to_order_item_meta', 20, 4 );
function add_custom_field_to_order_item_meta( $item, $cart_item_key, $values, $order ) {
    $custom_field_value = get_post_meta( $item->get_product_id(), '_custom_product_text_field', true );
    if ( ! empty($custom_field_value) ){
            $item->update_meta_data( __('Custom label', 'woocommerce'), $custom_field_value );
    }
}

……或者…

另一个备选方案(旧方案已更新):

// 1.3 Save custom field value in order items meta data
add_action( 'woocommerce_add_order_item_meta', 'add_custom_field_to_order_item_meta', 20, 3 );
function add_custom_field_to_order_item_meta( $item_id, $values, $cart_item_key ) {

    $custom_field_value = get_post_meta( $values['data']->get_id(), '_custom_product_text_field', true );
    if ( ! empty($custom_field_value) )
        wc_add_order_item_meta( $item_id, __('Custom label', 'woocommerce'), $custom_field_value );
}

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

根据订单,您将获得(两者):

在电子邮件中,您将看到(两种情况下):

 类似资料:
  • 我可以在购物车和结账页面中获取元字段,但在电子邮件中,我尝试使用数组获取元字段,但在电子邮件中找不到任何自定义字段。 如何在电子邮件通知中获取自定义元字段(管理员、客户)? 我的代码如下: 任何帮助都很感激。

  • 因此,当我在mytheme/functions.php文件中编写这段代码时,这里有一个交易 它只在后端工作(产品编辑页面),它保存了变量,但没有显示在meta或add to card按钮之前或之后,我找不到解决方案 我尝试了echo,但仍然没有结果 所以我以为我是我的主题?不,我在其他vps上安装了wordpress,但我仍然没有显示文本字段text nothing 如果你知道这个问题,请友好地告

  • 我无法将产品属性添加到新订单电子邮件中。我已经将下面的代码片段添加到email-order-items.php(在//SKU..part之后),但是什么也没有发生。甚至滴度“位置:”也不可见。有什么想法吗?

  • 我需要在管理订单页面、谢谢和电子邮件通知中显示我的自定义签出字段。 我正在使用验证和保存WooCommerce回答代码中特定支付网关的额外结账字段来显示,验证和保存我的自定义字段。 从显示管理订单详细信息上的自定义字段数据,我试图显示自定义字段保存的输入值。 这是我到目前为止的代码: 但它不起作用。该字段不会附加到数据库中,但不会显示在任何位置: 如何正确显示字段?

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

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