我在 WooCommerce 中有一些产品自定义字段,它运行良好。这是代码:
add_action( 'woocommerce_product_options_advanced', 'meta_data_custom' );
function meta_data_custom() {
// 1. Partner ID
woocommerce_wp_text_input( array(
'id' => '_idpartner',
'label' => __( 'Partner ID', 'woocommerce' ),
'description' => __( 'Fill in your partner ID.', 'woocommerce' ),
'desc_tip' => true,
'placeholder' => __( 'Ex : P1', 'woocommerce' )
) );
// 2. Referral ID
woocommerce_wp_text_input( array(
'id' => '_idreferral',
'label' => __( 'Referral ID', 'woocommerce' ),
'description' => __( 'Fill in your referral ID.', 'woocommerce' ),
'desc_tip' => true,
'placeholder' => __( 'Ex : R1', 'woocommerce' )
) );
}
// Save custom field
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object_meta_data_custom', 10, 3 );
function action_woocommerce_admin_process_product_object_meta_data_custom($product){
// 1. ID Partner
if ( isset( $_POST['_idpartner'] ) ) {
// Update
$product->update_meta_data( '_idpartner', sanitize_text_field( $_POST['_idpartner'] ) );
}
// 1. ID Referral
if ( isset( $_POST['_idreferral'] ) ) {
// Update
$product->update_meta_data( '_idreferral', sanitize_text_field( $_POST['_idreferral'] ) );
}
}
产品发布前必须填写合作伙伴 ID 和推荐 ID。除(合作伙伴 ID == P1)外,所有合作伙伴 ID 均已具有推荐 ID。所以,我想自动填写推荐 ID,而不是合作伙伴 ID P1。
到目前为止我已经尝试过:
function update_meta_data_custom(){
global $product;
$idpartner = $product->get_meta('_idpartner');
$idreferral = $product->get_meta('_idreferral');
//Set referral id Obtained from user meta dinamically
$idreferral = "R13";//examples only
if(!empty($idpartner)){
if($idpartner !== "P1"){
//$product->update_meta_data( '_idreferral', sanitize_text_field( $_POST['_idreferral'] ) );
//update _idreferral with $idreferral
}
else{
if(!$idreferral){
//Give a message to update referral ID or no need to do anything
//Just in case something by human error
}
else{
//No need to do anything or keep the data
}
}
}
else{
//Give a message to update partner ID or no need to do anything
//Just in case something by human error
}
}
但不知道如何实现它。
有人可以指导我如何做到这一点吗?
请注意,您需要将下面的第二个函数
$referral_id = "R13";
替换为从用户元动态获取的推荐 ID 代码。
因此,您可以根据某些条件替换当前代码来实现自定义元数据更新,如下所示:
// Display custom input fields settings to admin single product
add_action( 'woocommerce_product_options_advanced', 'admin_product_display_custom_input_fields' );
function admin_product_display_custom_input_fields() {
## 1. Partner ID
woocommerce_wp_text_input( array(
'id' => '_idpartner',
'label' => __( 'Partner ID', 'woocommerce' ),
'description' => __( 'Fill in your partner ID.', 'woocommerce' ),
'desc_tip' => true,
'placeholder' => __( 'Ex : P1', 'woocommerce' )
) );
## 2. Referral ID
woocommerce_wp_text_input( array(
'id' => '_idreferral',
'label' => __( 'Referral ID', 'woocommerce' ),
'description' => __( 'Fill in your referral ID.', 'woocommerce' ),
'desc_tip' => true,
'placeholder' => __( 'Ex : R1', 'woocommerce' )
) );
}
// Save custom input fields values for admin single product
add_action( 'woocommerce_admin_process_product_object', 'admin_product_save_custom_fields_values' );
function admin_product_save_custom_fields_values( $product ){
## 1. Partner ID
$partner_id = isset($_POST['_idpartner']) ? sanitize_text_field($_POST['_idpartner']) : '';
$product->update_meta_data( '_idpartner', $partner_id ); // Update
## 2. Referral ID
if ( $partner_id === 'P1' ) {
// Replace with the code for referral ID obtained dynamically from user meta
$referral_id = "R13"; // <== Your code HERE
}
else {
$referral_id = isset($_POST['_idreferral']) ? sanitize_text_field($_POST['_idreferral']) : '';
}
$product->update_meta_data( '_idreferral', $referral_id ); // Update
}
// Display product custom field admin notice
add_action( 'admin_notices', 'throw_error_admin_notice_Product_custom_fields' );
function throw_error_admin_notice_Product_custom_fields(){
global $pagenow, $typenow, $post_id;
if( $pagenow === 'post.php' && $typenow === 'product' && $post_id > 0 ) {
$product = wc_get_product($post_id);
$partner_id = $product->get_meta('_idpartner');
$referral_id = $product->get_meta('_idreferral');
if ( empty($partner_id) && empty($referral_id) ) {
printf( '<div class="notice error is-dismissible"><p>%s</p></div>',
__( 'The Partner ID and Referral ID have not been saved yet.', 'woocommerce' ) );
} elseif ( empty($partner_id) ) {
printf( '<div class="notice error is-dismissible"><p>%s</p></div>',
__( 'The Partner ID has not been saved yet.', 'woocommerce' ) );
} elseif ( empty($referral_id) ) {
printf( '<div class="notice error is-dismissible"><p>%s</p></div>',
__( 'The Referral ID has not been saved yet.', 'woocommerce' ) );
}
}
}
代码位于子主题的functions.php 文件中(或插件中)。应该有效