How to define fields for custom product type only

2019-07-25 17:20发布

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) );

}

4条回答
聊天终结者
2楼-- · 2019-07-25 17:47
  1. Create a custom product category in Woocommerce; call it Custom Product.
  2. Add the following code to functions.php:

    function isCustomProduct($pid) {
        $terms = get_the_terms( $id, 'product_cat' );
        foreach ($terms as $term) {
            if ($term->name == 'Custom Product') {
                return true;
            }
        }
        return false;
    }
    
  3. Add the following line to the beginning of english_course_custom_fields():

    if (!isWyDayProduct($post->ID)) {
        return;
    }
    
查看更多
ら.Afraid
3楼-- · 2019-07-25 17:53

You can use woocommerce_product_options_general_product_data action hook to add custom fields to a specific product type and woocommerce_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.

查看更多
混吃等死
4楼-- · 2019-07-25 17:56

Add a custom meta box and then add fields to the meta box

/**
* adding custom box to show custom fields in product create/edit page
*/
 function add_custom_meta_box() {
        add_meta_box(
            'custom_meta_box', // $id
            'Deal of Day', // $title
            'show_custom_meta_box', // $callback
            'product', // $page  ( or content type)
            'normal', // $context
            'high');// $priority
    }

/*
 * call back functio to render fields in meta box
 */
function show_custom_meta_box() {
        global $post;
        $meta = get_post_meta($post->ID, $field['id'], true);
        echo '<input type="text" name="name-of-field" id="field-od" value="' . $meta . '" size="30" />
                                <br /><span class="description">Label</span>';
}


add_action('add_meta_boxes',  'add_custom_meta_box' );
查看更多
一夜七次
5楼-- · 2019-07-25 17:57

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

// Creates the admin panel tab
add_action( 'woocommerce_product_write_panel_tabs', so_26338834_product_write_panel_tab' );

function product_write_panel_tab() {
    echo '<li class="wdm_custom_product_tab show_if_wdm_custom_product wdm_custom_product_options"><a href="#wdm_custom_product_data">'.__( 'English Courses' ).'</a></li>';
}

// Creates the panel for selecting product options
add_action( 'woocommerce_product_write_panels', so_26338834_product_write_panel' );

function product_write_panel() {
    global $post; ?>

    <div id="wdm_custom_product_data" class="wdm_custom_product panel panel woocommerce_options_panel wc-metaboxes-wrapper">

        <div class="options_group wdm_custom_product">
        put your fields here
        </div> <!-- options group -->

    </div>

<?php
}

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.

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 show_if_wdm_custom_product">';
// your inputs
echo '</div>';
}

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.

// product type specific options
$( 'body' ).on( 'woocommerce-product-type-change', function( event, select_val, select ) {

    if ( select_val == 'wdm_custom_product' ) {
        $( '.show_if_wdm_custom_product' ).show();
    } else {
        $( '.show_if_wdm_custom_product' ).hide();
    }
});
查看更多
登录 后发表回答