在 WooCommerce 订单和电子邮件上保存和显示产品自定义元数据 [英] Save and display product custom meta on WooCommerce orders and emails

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

问题描述

好的,所以基本上我们在 WooCommerce 商店中使用 ACF 创建了一个自定义字段,以便添加运输延迟".特定产品的通知.

OK, so basically we created a custom field using ACF in our WooCommerce Store in order to add a "Shipping Delay" notice for specific products.

以下是我们取得的成果的演示:https://www.safe-company.com/shop/machines/uvc-disinfection-lamp/

Here is a demonstration of what we achieved: https://www.safe-company.com/shop/machines/uvc-disinfection-lamp/

单个产品页面参考图片

然后我们设法使用 Elementor(一个页面构建器)将此通知放在单个产品页面中,然后将此信息添加到购物车和结帐页面中的项目数据中,并将以下代码添加到我们的 functions.php 中

We managed then to put this notice in the single product page using Elementor (A page builder) and then add this information to the item data in the cart and checkout page with the following code added to our functions.php

// Render the custom product field in cart and checkout
add_filter( 'woocommerce_get_item_data', 'wc_add_shipping_delay', 10, 2 );
function wc_add_shipping_delay( $cart_data, $cart_item ) 
{
    $custom_items = array();

    if( !empty( $cart_data ) )
        $custom_items = $cart_data;

    // Get the product ID
    $product_id = $cart_item['product_id'];

    if( $custom_field_value = get_post_meta( $product_id, 'shipping_delay_for_out_of_stock_items', true ) )
        $custom_items[] = array(
            'name'      => __( 'Shipping Delay', 'woocommerce' ),
            'value'     => $custom_field_value,
            'display'   => $custom_field_value,
        );

    return $custom_items;
}

来自购物车页面的 Item Meta 中的自定义字段

我们现在的问题是,我们需要将此发货延迟通知添加到电子邮件(分别显示在包含此数据的每个项目下方)和订单页面上.那怎么可能呢?由于我已经检查了一堆线程,但所有线程都是使用动态字段完成的(用户在购买时完成),但我们的案例场景完全不同.

Our problem now is that we need to add this shipping delay notice to the email (show it below each item that contains this data respectively) and on the order page too. How could that be done? Since I have checked a bunch of threads on this but all of them are done using dynamic fields (that the user completes when purchasing) but our case scenario is quite different.

请帮忙!!

推荐答案

以下内容会将您的自定义字段保存为订单项元数据并随处显示:

The following will save your custom field as order item meta data and display it everywhere:

// Save and display "shipping delay" on order items everywhere
add_filter( 'woocommerce_checkout_create_order_line_item', 'action_wc_checkout_create_order_line_item', 10, 4 );
function action_wc_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {

    // Get the shipping delay
    $value = $values['data']->get_meta( 'shipping_delay_for_out_of_stock_items' );

    if( ! empty( $value ) ) {
        // Save it and display it
        $item->update_meta_data( __( 'Shipping Delay', 'woocommerce' ), $value );
    }
}   

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

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

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