如何在一篇文章中将值从后元复制到其他归档元?对于前:
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)元素中。
你可以用两种方式做到:
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文件中。两种方式都有效。