将元自定义字段复制到其他自定义字段

问题描述 投票:1回答:1

如何在一篇文章中将值从后元复制到其他归档元?对于前:

copy value from custom_field_1 (if exist) or custom_field_2 (if exist) to custom_ field_3

只有field1或field2中的一个具有值,而字段3总是具有从fiel1或field2复制的e值。

所有自定义字段都在一个帖子(woo product)元素中。

php wordpress methods woocommerce custom-fields
1个回答
2
投票

你可以用两种方式做到:

1)来自$product_id的旧方式是动态产品ID(或订单ID):

if( ( $value = get_post_meta( $product_id, 'custom_field_1', true ) || $value = get_post_meta( $product_id, 'custom_field_2', true ) ) && ! get_post_meta( $product_id, 'custom_field_3', true ) ){
    update_post_meta( $product_id, 'custom_field_3', $value );
}

2)来自WooCommerce 3, CRUD methods$product对象(或来自WC_Product$order对象)的新方式(自WC_Order):

if( ( $value = $product->get_meta( 'custom_field_1' ) || $value = $product->get_meta( 'custom_field_2') ) && ! $product->get_meta( 'custom_field_3' ) ){
    $product->update_meta_data( 'custom_field_3', $value );
}

代码位于活动子主题(或活动主题)的function.php文件中。两种方式都有效。

© www.soinside.com 2019 - 2024. All rights reserved.