my code is showing filed in all product types such as Simple Product,variable product,custom type.... Means available for all but I want to restrict it for my custom type only.
How can I restrict my custom field types to English Courses product type
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) );
}
Custom Product
.Add the following code to functions.php:
Add the following line to the beginning of
english_course_custom_fields()
:You can use
woocommerce_product_options_general_product_data
action hook to add custom fields to a specific product type andwoocommerce_process_product_meta
filter hook to save the custom fields. For details please refer the link - http://wisdmlabs.com/blog/create-product-type-custom-settings-woocommerce/. This will help you.Add a custom meta box and then add fields to the meta box
I think there are 2 ways to go about this. 1. Either add the custom fields to their own pane in the product data box. or 2. keep adding them to the general data hook you are using and then use JS to hide them for other product types.
Approach 1
Approach 2
Wrap your inputs in a div with the
show_if_{$product_type}
class. I think WooCommerce will handle the hide/display aspects for you.Edit: I've checked my own code and discovered that you must write the javascript for hiding/showing yourself. This is easily done as WooCommerce triggers a custom event when the product type input is changed, so we can write a handler that fires on this event.