保存并在电子邮件中显示产品自定义数据WooCommerce中的订单页面 [英] Save and display product custom data in emails & orders pages in WooCommerce

查看:65
本文介绍了保存并在电子邮件中显示产品自定义数据WooCommerce中的订单页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些代码添加自定义字段,以使客户能够自定义其产品。

I'm using some code to add custom fields to enable my customers to customize their products.

这是我当前的代码:

/* Add meta fields to all products */
/* SRC: https://wisdmlabs.com/blog/add-custom-data-woocommerce-order-2/ */

/* Step 1: Adding Custom Field for Product */
add_action('woocommerce_before_add_to_cart_button','rs_add_custom_fields');
/**
 * Adds custom field for Product
 * @return [type] [description]
 */
function rs_add_custom_fields()
{

    global $product;

    ob_start();

    ?>

        <div class="wdm-custom-fields">
            <input type="text" placeholder="neutral_bag_count" name="neutral_bag_count">
            <input type="text" placeholder="bright_bag_count" name="bright_bag_count">
            <input type="text" placeholder="pastel_bag_count" name="pastel_bag_count">
            <input type="text" placeholder="reg_tendon_count" name="reg_tendon_count">
            <input type="text" placeholder="gid_tendon_count" name="gid_tendon_count">
        </div>
        <div class="clear"></div>

    <?php

    $content = ob_get_contents();
    ob_end_flush();

    return $content;
}

/* Step 2: Add Customer Data to WooCommerce Cart */
add_filter('woocommerce_add_cart_item_data','rs_add_item_data',10,3);

/**
 * Add custom data to Cart
 * @param  [type] $cart_item_data [description]
 * @param  [type] $product_id     [description]
 * @param  [type] $variation_id   [description]
 * @return [type]                 [description]
 */
function rs_add_item_data($cart_item_data, $product_id, $variation_id)
{
    if(isset($_REQUEST['neutral_bag_count']))
    {
        $cart_item_data['neutral_bag_count'] = sanitize_text_field($_REQUEST['neutral_bag_count']);
    }

        if(isset($_REQUEST['bright_bag_count']))
    {
        $cart_item_data['bright_bag_count'] = sanitize_text_field($_REQUEST['bright_bag_count']);
    }

        if(isset($_REQUEST['pastel_bag_count']))
    {
        $cart_item_data['pastel_bag_count'] = sanitize_text_field($_REQUEST['pastel_bag_count']);
    }

        if(isset($_REQUEST['reg_tendon_count']))
    {
        $cart_item_data['reg_tendon_count'] = sanitize_text_field($_REQUEST['reg_tendon_count']);
    }

        if(isset($_REQUEST['gid_tendon_count']))
    {
        $cart_item_data['gid_tendon_count'] = sanitize_text_field($_REQUEST['gid_tendon_count']);
    }

    return $cart_item_data;
}

/* Step 3: Display Details as Meta in Cart */
add_filter('woocommerce_get_item_data','rs_add_item_meta',10,2);

/**
 * Display information as Meta on Cart page
 * @param  [type] $item_data [description]
 * @param  [type] $cart_item [description]
 * @return [type]            [description]
 */
function rs_add_item_meta($item_data, $cart_item)
{

    if(array_key_exists('neutral_bag_count', $cart_item))
    {
        $custom_details = $cart_item['neutral_bag_count'];

        $item_data[] = array(
            'key'   => 'Neutral Bags',
            'value' => $custom_details
        );
    }

    if(array_key_exists('bright_bag_count', $cart_item))
    {
        $custom_details = $cart_item['bright_bag_count'];

        $item_data[] = array(
            'key'   => 'Bright Bags',
            'value' => $custom_details
        );
    }

    if(array_key_exists('pastel_bag_count', $cart_item))
    {
        $custom_details = $cart_item['pastel_bag_count'];

        $item_data[] = array(
            'key'   => 'Pastel Bags',
            'value' => $custom_details
        );
    }

    if(array_key_exists('reg_tendon_count', $cart_item))
    {
        $custom_details = $cart_item['reg_tendon_count'];

        $item_data[] = array(
            'key'   => 'Regular Tendon Sheets',
            'value' => $custom_details
        );
    }

    if(array_key_exists('gid_tendon_count', $cart_item))
    {
        $custom_details = $cart_item['gid_tendon_count'];

        $item_data[] = array(
            'key'   => 'GID Tendon Sheets',
            'value' => $custom_details
        );
    }

    return $item_data;
}

/* Step 4: Add Custom Details as Order Line Items */
add_action( 'woocommerce_checkout_create_order_line_item', 'rs_add_custom_order_line_item_meta',10,4 );

function rs_add_custom_order_line_item_meta($item, $cart_item_key, $values, $order)
{

    if(array_key_exists('neutral_bag_count', $values))
    {
        $item->add_meta_data('_neutral_bag_count',$values['neutral_bag_count']);
    }

    if(array_key_exists('bright_bag_count', $values))
    {
        $item->add_meta_data('_bright_bag_count',$values['bright_bag_count']);
    }

    if(array_key_exists('pastel_bag_count', $values))
    {
        $item->add_meta_data('_pastel_bag_count',$values['pastel_bag_count']);
    }

    if(array_key_exists('reg_tendon_count', $values))
    {
        $item->add_meta_data('_reg_tendon_count',$values['reg_tendon_count']);
    }

    if(array_key_exists('gid_tendon_count', $values))
    {
        $item->add_meta_data('_gid_tendon_count',$values['gid_tendon_count']);
    }
}

在单个产品页面上显示的方式如下:

Here's how that shows on the single product page:

此处在购物车中:

如何在电子邮件模板和订单页面上获取相同的数据以进行渲染?为了清楚起见,我在谈论这些地方:

How do I get the same data to render in my email templates and on the orders page? For clarity, i'm talking about these places:

< a href = https://i.stack.imgur.com/WjNBJ.png rel = nofollow noreferrer>

任何提示将不胜感激。

推荐答案

我重新访问了所有代码,并用新代码替换了最后一个函数。现在,您将在订单接收页面,我的帐户>订单视图页面,订单编辑页面和电子邮件通知中为订单商品显示此自定义字段。

I have revisited all your code and replaced last function by a new one. Now you will get this custom fields displayed for Order items in Order received page, My account > Order view pages, Order edit pages and email notifications…

add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_fields_single_product', 20 );
function add_custom_fields_single_product(){
    global $product;
    ?>
        <div class="product-custom-fields">
            <input type="text" placeholder="<?php _e('Neutral Bags', 'woocommerce'); ?>" name="neutral_bag">
            <input type="text" placeholder="<?php _e('Bright Bags', 'woocommerce'); ?>" name="bright_bag">
            <input type="text" placeholder="<?php _e('Pastel Bags', 'woocommerce'); ?>" name="pastel_bag">
            <input type="text" placeholder="<?php _e('Regular Tendon Sheets', 'woocommerce'); ?>" name="reg_tendon">
            <input type="text" placeholder="<?php _e('GID Tendon Sheets', 'woocommerce'); ?>" name="gid_tendon">
        </div>
        <div class="clear"></div>
    <?php
}


add_filter( 'woocommerce_add_cart_item_data', 'add_custom_fields_cart_item_data', 10, 2 );
function add_custom_fields_cart_item_data( $cart_item_data, $product_id ){
    if(isset($_POST['neutral_bag']))
        $cart_item_data['custom_data']['neutral_bag'] = sanitize_text_field($_POST['neutral_bag']);

    if(isset($_POST['bright_bag']))
        $cart_item_data['custom_data']['bright_bag'] = sanitize_text_field($_POST['bright_bag']);

    if(isset($_POST['pastel_bag']))
        $cart_item_data['custom_data']['pastel_bag'] = sanitize_text_field($_POST['pastel_bag']);

    if(isset($_POST['reg_tendon']))
        $cart_item_data['custom_data']['reg_tendon'] = sanitize_text_field($_POST['reg_tendon']);

    if(isset($_POST['gid_tendon']))
        $cart_item_data['custom_data']['gid_tendon'] = sanitize_text_field($_POST['gid_tendon']);

    $cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
    WC()->session->set( 'custom_data', $cart_item_data['custom_data'] );

    return $cart_item_data;
}

add_filter( 'woocommerce_get_item_data', 'display_custom_fields_cart_item_data', 10, 2 );
function display_custom_fields_cart_item_data($item_data, $cart_item){

    if( ! array_key_exists( 'custom_data', $cart_item ) )
        return $item_data;

    if( array_key_exists( 'neutral_bag', $cart_item['custom_data'] ) )
        $item_data[] = array(
            'key'   => __('Neutral Bags', 'woocommerce'),
            'value' => $cart_item['custom_data']['neutral_bag']
        );

    if(array_key_exists('bright_bag', $cart_item['custom_data']))
        $item_data[] = array(
            'key'   => __('Bright Bags', 'woocommerce'),
            'value' => $cart_item['custom_data']['bright_bag']
        );

    if(array_key_exists('pastel_bag', $cart_item['custom_data']))
        $item_data[] = array(
            'key'   => __('Pastel Bags', 'woocommerce'),
            'value' => $cart_item['custom_data']['pastel_bag']
        );

    if(array_key_exists('reg_tendon', $cart_item['custom_data']))
        $item_data[] = array(
            'key'   => __('Regular Tendon Sheets', 'woocommerce'),
            'value' => $cart_item['custom_data']['reg_tendon']
        );

    if(array_key_exists('gid_tendon', $cart_item['custom_data']))
        $item_data[] = array(
            'key'   => __('GID Tendon Sheets', 'woocommerce'),
            'value' => $cart_item['custom_data']['gid_tendon']
        );

    return $item_data;
}

// Save values as Order item data and display them everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_custom_fields_as_order_item_meta', 20, 4);
function save_custom_fields_as_order_item_meta($item, $cart_item_key, $values, $order) {
    if( ! isset($values['custom_data']) )
        return;

    $text_domain ='woocommerce';

    // Save values:

    if( array_key_exists('neutral_bag', $values['custom_data']) ){
        $item->update_meta_data( __('Neutral Bags', $text_domain), $values['custom_data']['neutral_bag'] );
    }

    if( array_key_exists('bright_bag', $values['custom_data'])){
        $item->update_meta_data( __('Bright Bags', $text_domain), $values['custom_data']['bright_bag'] );
    }

    if( array_key_exists('pastel_bag', $values['custom_data'])){
        $item->update_meta_data( __('Pastel Bags', $text_domain), $values['custom_data']['pastel_bag'] );
    }

    if( array_key_exists('reg_tendon', $values['custom_data'])){
        $item->update_meta_data( __('Regular Tendon Sheets', $text_domain), $values['custom_data']['reg_tendon'] );
    }

    if( array_key_exists('gid_tendon', $values['custom_data'])){
        $item->update_meta_data( __('GID Tendon Sheets', $text_domain), $values['custom_data']['gid_tendon'] );
    }
}

代码进入function.php文件

经过测试并有效。

在以下订单中收到,其中填充了4个自定义字段(所以一个为空)

Below Order-received with 4 custom fields filled (so one empty):

和后端订单编辑页面:

这篇关于保存并在电子邮件中显示产品自定义数据WooCommerce中的订单页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆