Wordpress 在商业订单详细信息和所有电子邮件中显示高级自定义字段值
我使用ACF插件创建了一个高级自定义字段,以便在Woocommerce产品编辑页面上添加其他信息。这是一个区分t恤模板的副标题。我能够使该值显示在产品页面前端,但我需要将该信息显示在订单详细信息和所有电子邮件中,可能作为订单元数据。提前感谢。请在functions.php文件中尝试下面的代码Wordpress 在商业订单详细信息和所有电子邮件中显示高级自定义字段值,wordpress,woocommerce,advanced-custom-fields,Wordpress,Woocommerce,Advanced Custom Fields,我使用ACF插件创建了一个高级自定义字段,以便在Woocommerce产品编辑页面上添加其他信息。这是一个区分t恤模板的副标题。我能够使该值显示在产品页面前端,但我需要将该信息显示在订单详细信息和所有电子邮件中,可能作为订单元数据。提前感谢。请在functions.php文件中尝试下面的代码 add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_field', 0 ); function add_custom_fie
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_field', 0 );
function add_custom_field() {
global $product; // Changed this
// Added this too (compatibility with WC +3)
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
echo "<div class='produto-informacoes-complementares'>";
echo get_field( 'woo_title', $product_id );
echo "</div>";
return true;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_product_field', 10, 2 );
function save_my_custom_product_field( $cart_item_data, $product_id ) {
$custom_field_value = get_field( 'woo_title', $product_id, true );
if( !empty( $custom_field_value ) )
{
$cart_item_data['woo_title'] = $custom_field_value;
// below statement make sure every add to cart action as unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
$custom_items = array();
// Woo 2.4.2 updates
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['woo_title'] ) ) {
$custom_items[] = array( "name" => "ACF value:", "value" => $cart_item['woo_title'] );
}
return $custom_items;
}
function add_order_item_meta_acf( $item_id, $values ) {
wc_add_order_item_meta( $item_id, 'acf', $values [ 'woo_title' ] );
}
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta_acf' , 10, 2);
add_操作('woocommerce_之前的'add_to_cart_按钮','add_custom_字段',0);
函数添加\自定义\字段(){
全局$product;//已更改此项
//也添加了此功能(与WC+3兼容)
$product\U id=方法\U存在($product,'get\U id')?$product->get\U id():$product->id;
回声“;
echo get_字段('woo_title',$product_id);
回声“;
返回true;
}
添加过滤器('woocommerce\u add\u cart\u item\u data','save\u my\u custom\u product\u field',10,2);
函数保存\我的\自定义\产品\字段($cart\项目\数据,$product\ id){
$custom_field_value=get_字段('woo_title',$product_id,true);
如果(!空($custom_field_value))
{
$cart\u item\u data['woo\u title']=$custom\u field\u value;
//下面的语句确保每个“添加到购物车”操作都是唯一的行项目
$cart_item_data['unique_key']=md5(microtime().rand());
}
返回$cart\u item\u数据;
}
添加过滤器('woocommerce\u get\u item\u data','render\u meta\u on\u cart\u and \u checkout',10,2);
函数render\u meta\u on\u cart\u和\u checkout($cart\u data,$cart\u item){
$custom_items=array();
//Woo 2.4.2更新
如果(!空($cart_数据)){
$custom\u items=$cart\u数据;
}
如果(isset($cart_item['woo_title'])){
$custom_items[]=array(“name”=>“ACF value:”,“value”=>$cart_items['woo_title']);
}
返回$custom_项目;
}
函数add_order_item_meta_acf($item_id,$values){
wc_添加_订单_项目_元($item_id,'acf',$values['woo_title']);
}
添加操作('woocommerce'u添加订单项目'u meta','add订单项目'u meta'acf',10,2);
在订单表格后的订单详细信息页面中显示Acf值
add_action ('woocommerce_order_details_after_order_table', 'order_detail_page', 20);
function order_detail_page($order_id) {
// global $post;
// $order = wc_get_order( $post->ID );
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
}
if (get_field('woo_title', $product_id)) {
?>
<p class="WooCommerce">ACF value: <?php the_field('woo_title', $product_id); ?><p>
<?php
}
}```
add_action('woocommerce_order_details_后,'u order_table','order_details_page',20);
功能订单详细信息页面($order\U id){
//全球$员额;
//$order=wc\u get\u order($post->ID);
$order=新WC\U订单($order\U id);
$items=$order->get_items();
foreach($items作为$item){
$product_id=$item['product_id'];
}
if(获取字段($woo\U标题,$product\U id)){
?>
ACF值:
请在functions.php文件中尝试以下代码
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_field', 0 );
function add_custom_field() {
global $product; // Changed this
// Added this too (compatibility with WC +3)
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
echo "<div class='produto-informacoes-complementares'>";
echo get_field( 'woo_title', $product_id );
echo "</div>";
return true;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_product_field', 10, 2 );
function save_my_custom_product_field( $cart_item_data, $product_id ) {
$custom_field_value = get_field( 'woo_title', $product_id, true );
if( !empty( $custom_field_value ) )
{
$cart_item_data['woo_title'] = $custom_field_value;
// below statement make sure every add to cart action as unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
$custom_items = array();
// Woo 2.4.2 updates
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['woo_title'] ) ) {
$custom_items[] = array( "name" => "ACF value:", "value" => $cart_item['woo_title'] );
}
return $custom_items;
}
function add_order_item_meta_acf( $item_id, $values ) {
wc_add_order_item_meta( $item_id, 'acf', $values [ 'woo_title' ] );
}
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta_acf' , 10, 2);
add_操作('woocommerce_之前的'add_to_cart_按钮','add_custom_字段',0);
函数添加\自定义\字段(){
全局$product;//已更改此项
//也添加了此功能(与WC+3兼容)
$product\U id=方法\U存在($product,'get\U id')?$product->get\U id():$product->id;
回声“;
echo get_字段('woo_title',$product_id);
回声“;
返回true;
}
添加过滤器('woocommerce\u add\u cart\u item\u data','save\u my\u custom\u product\u field',10,2);
函数保存\我的\自定义\产品\字段($cart\项目\数据,$product\ id){
$custom_field_value=get_字段('woo_title',$product_id,true);
如果(!空($custom_field_value))
{
$cart\u item\u data['woo\u title']=$custom\u field\u value;
//下面的语句确保每个“添加到购物车”操作都是唯一的行项目
$cart_item_data['unique_key']=md5(microtime().rand());
}
返回$cart\u item\u数据;
}
添加过滤器('woocommerce\u get\u item\u data','render\u meta\u on\u cart\u and \u checkout',10,2);
函数render\u meta\u on\u cart\u和\u checkout($cart\u data,$cart\u item){
$custom_items=array();
//Woo 2.4.2更新
如果(!空($cart_数据)){
$custom\u items=$cart\u数据;
}
如果(isset($cart_item['woo_title'])){
$custom_items[]=array(“name”=>“ACF value:”,“value”=>$cart_items['woo_title']);
}
返回$custom_项目;
}
函数add_order_item_meta_acf($item_id,$values){
wc_添加_订单_项目_元($item_id,'acf',$values['woo_title']);
}
添加操作('woocommerce'u添加订单项目'u meta','add订单项目'u meta'acf',10,2);
在订单表格后的订单详细信息页面中显示Acf值
add_action ('woocommerce_order_details_after_order_table', 'order_detail_page', 20);
function order_detail_page($order_id) {
// global $post;
// $order = wc_get_order( $post->ID );
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
}
if (get_field('woo_title', $product_id)) {
?>
<p class="WooCommerce">ACF value: <?php the_field('woo_title', $product_id); ?><p>
<?php
}
}```
add_action('woocommerce_order_details_后,'u order_table','order_details_page',20);
功能订单详细信息页面($order\U id){
//全球$员额;
//$order=wc\u get\u order($post->ID);
$order=新WC\U订单($order\U id);
$items=$order->get_items();
foreach($items作为$item){
$product_id=$item['product_id'];
}
if(获取字段($woo\U标题,$product\U id)){
?>
ACF值:
这是一个非常棒的地方,也是我唯一找到这个答案的地方!我可能不得不把它传播开来!:)
我也在开发一个网站,让它工作——代码没有bug
只是代码上的几个附加指针:
- 在哪里写着“woo_title”,也就是你把你所在领域的名称放在哪里
- 在显示“ACF值:”和“ACF”的代码中,将这些更改为对您试图向客户显示的数据有意义的标题(如日期或样式)
- 除最后一点外,无需添加“:”字段标题后,Woocommerce将插入冒号
- 当您将测试产品添加到购物车并进行测试签出时,如果您正在测试和更改字段标题,正如我上面所说的,您无法通过简单地刷新购物车屏幕看到您的更改,您必须清空购物车,更改功能代码,更改您在ACF字段的自定义字段中输入的内容,以使数据库中的产品数据将更新,然后点击产品/帖子上的更新,然后完成将其添加到购物车并签出以查看您的过程