我的代码显示在所有产品类型中,例如简单产品、可变产品、自定义类型......
手段适用于所有人,但我想将其限制为仅适用于我的自定义类型。
如何将自定义字段类型限制为英语课程产品类型
add_filter( 'product_type_selector', 'english_course_wc_cpt' );
function english_course_wc_cpt( $types ){
$types[ 'wdm_custom_product' ] = __( 'English Courses' );
return $types;
}
class WC_Product_Wdm extends WC_Product{
public function __construct( $product ) {
$this->product_type = 'wdm_custom_product';
parent::__construct( $product );
// add additional functions here
}
}
add_action( 'woocommerce_product_options_general_product_data', 'english_course_custom_fields' );
function english_course_custom_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Duration: Duration of the course
woocommerce_wp_text_input(
array(
'id' => 'duration_ec_wc_cpt',
'label' => __( 'Duration', 'woocommerce' ),
'placeholder' => 'Enter the Duration of the Course',
'desc_tip' => 'true',
'description' => __( 'Duration of the Course.', 'woocommerce' ),
'type' => 'text'
));
// Fee: Create a number field
woocommerce_wp_text_input(
array(
'id' => 'fee_ec_wc_cpt',
'label' => __( 'Fee', 'woocommerce' ),
'placeholder' => 'Enter the Fee',
'desc_tip' => 'true',
'description' => __( 'Fee of the course.', 'woocommerce' ),
'type' => 'number'
));
// Address: Create a text area field
woocommerce_wp_textarea_input(
array(
'id' => 'address_ec_wc_cpt',
'label' => __( 'Address', 'woocommerce' ),
'placeholder' => 'Enter the Address',
'desc_tip' => 'true',
'description' => __( 'Address of the course.', 'woocommerce' ),
));
echo '</div>';
}
/*
* SAVING DATA - ENGLISH COURSES (wc_cpt)
* postfix/prefix : ec_wc_cpt
*/
add_action( 'woocommerce_process_product_meta', 'save_ec_wc_cpt' );
function save_ec_wc_cpt( $post_id ){
// Save Duration field
$duration_ec_wc_cpt = $_POST['duration_ec_wc_cpt'];
if( !empty( $duration_ec_wc_cpt ) )
update_post_meta( $post_id, 'duration_ec_wc_cpt', esc_attr( $duration_ec_wc_cpt) );
// Save Fee field
$fee_ec_wc_cpt = $_POST['fee_ec_wc_cpt'];
if( !empty( $fee_ec_wc_cpt ) )
update_post_meta( $post_id, 'fee_ec_wc_cpt', esc_attr( $fee_ec_wc_cpt) );
// Save Address field
$address_ec_wc_cpt = $_POST['address_ec_wc_cpt'];
if( !empty( $fee_ec_wc_cpt ) )
update_post_meta( $post_id, 'address_ec_wc_cpt', esc_attr( $address_ec_wc_cpt) );
}