php – 在WooCommerce的电子邮件和订单页面中保存并显示产品自定义数据

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

这是我目前的代码:

/* 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']);
    }
}

以下是单个产品页面上显示的内容:

这是在购物车中:

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

任何提示将不胜感激.

解决方法:

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

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文件中.

经过测试和工作.

在Order-received下面填充了4个自定义字段(所以一个空):

enter image description here

和后端订单编辑页面:

enter image description here

原文地址:https://codeday.me/bug/20191008/1874926.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


我想将wordpress的默认接口路由改掉,愿意是默认的带一个 wp-json,我想换成 api,直接简单明了。默认路由https://www.xxzhuti.com/wp-json/wp/v2/posts/1新的路由https://www.xxzhuti.com/api/wp/v2/posts/1路由修改wordpress提供了一个钩子来修改 wp-json 的地址,代码入汛function r
wordpress自定义分类法之后,我看到链接都自动在后面添加了一个斜杠,如下图所示打开链接后都是先从一个链接301后到另外一个链接,我这个纳闷,然后一顿找原因,最后竟然是因为固定链接看我后台的固定链接是 /%post_id%/ 这么写的,然后每次都会301自动跳转。正确的写法是 /%post_id%.html 这样写才不会301跳转。一个小问题,往往一个小问题就会折腾好长时间,在这记录一下吧..
事情是这样的,我用 get_post_type 函数创建了一个自定义分类法类型,然后自定义了文件的访问路径代码如下。function tblog_chat_template( $template_path ){    if ( get_post_type() == 'aichat' ) {         if ( is_single() )         {             $temp
最近网站莫名其妙的被顶上了,过一个多小时,就注册一个账号,虽然不多,但是也挺烦,时间一长垃圾注册的就多了。由于我前台把注册关掉了,只允许用第三方登录,经过查找发现是通过后台注册的,后台无须邮箱验证即可注册,也是就想着在后台加个验证码。效果如下实现方法一将下面的代码加入到 function.php 中,刷新页面就会看到。//WordPress新用户注册随机数学验证码function add_s...
最近服务器要到期了,就想着把网站转移到另外一台服务器,本来打算要续费的,但是腾讯云的服务器有了新的规定,域名必须在腾讯云备案才能使用,在别的地方备案的域名需要转到腾讯云,在腾讯云在走一遍备案流程,若是备案简单,时间短也就无所谓了,就转一下,可是这个备案大家也知道,至少得一个月,于是就只能放弃腾讯云了,将站点转回备案的阿里云,以后就用阿里云了,贵点就贵点吧。这不网站迁移过去了,死活图片不显示,各..
今天在写wordpress的接口,然后碰到个奇怪的问题,怎么访问都提示 rest_no_route 的错误,然后就一直跟踪代码,终于找到原因所在。报错信息{'code': 'rest_no_route','message': '未找到匹配URL和请求方式的路由。','data': {'status': 404}}原因分析register_rest_route( string $namespace,
今天看到群友突然滴滴滴的找我QQ,好像出什么大事情一样。原来他由于误操作导致网站文章被删除。
今天遇到一个网友提示网站访问速度比较慢,然后通知日志看到有他的网站WordPress默认登录地址wp-login.php一直在被爬虫抓取,因为是有被破解后台账户信息,导致的占用数据库资源使得网站资源带宽不足后访问降低导致
今天老左在帮助客户升级WordPress的时候我看他网站也比较简单就直接在后台手工升级的。升级后打开网站有出现"There has been a critical error on your website."的错误提示。
我们在使用WordPress搭建的网站是否有留意查看源代码的时候看到标题中的原本"-",被转义成"&#8211"。其实这样并不影响用户阅读体验和搜索引擎,搜索引擎在收录后也会反向转义的。就是感觉看
WordPress默认是无法上传.webp格式的文件,如果要上传需要对上传文件限制进行解除后才能上传。
GTranslate 是一个流行的翻译插件,在免费和付费版本中提供不同的体验。 使用免费版本,您可以轻松提供任何语言的动态翻译,而付费版本提供可索引、可编辑的翻译版本,就像 Weglot 一样。
时下主流的浏览器都自带了广告屏蔽功能,还有ADSafe、ADB等软件插件,不断更新,屏蔽网站代码!
您是否正在寻找可用于显示数据的 WordPress 表格插件? WordPress 表格插件可让您以表格格式存储和呈现数据,以方便访问者阅读。
wordPress数据库除了本身的表以外,部分插件会增加表来存储数据。如果插件已经删除了,这些插件生成的表还在数据库中,那么怎么删除呢?
WordPress几乎是世界上最安全的开源程序,但其后台登录注册的地址始终是众所周知的,所以不少人觉得隐藏WordPress的登录地址后可能会更加安全。下面浩子就针对这个问题来简单说明一下方法。WordPress隐藏后台登录地址
自 WordPress 5.8 正式版发布 以后,后台的小工具就默认是块编辑器的界面样式,很多老用户还没有来得及学习就一脸茫然了,今天浩子就来教大家如何恢复WordPress经典小工具。
您想用您的语言翻译 WordPress 插件吗?许多 WordPress 插件是完全可翻译的,世界上任何人都可以轻松翻译。在本文中,我们将向您展示如何轻松地将 WordPress 插件翻译成您的语言(无需任何代码)。
WordPress新手不清楚WordPress调试模式怎么开启,今天来教大家认识和开启。什么是WordPress的调试模式
最近不少用户反馈说WordPress后台的外观-自定义点开后直接报错,错误信息如下: