如何仅为自定义产品类型定义字段-Woo Commerce Hook


How to define fields for custom product type only - Woo Commerce Hook

我的代码显示了所有产品类型的文件,如简单产品、可变产品、自定义类型。。。。意味着所有人都可以使用,但我只想将其限制为我的自定义类型。

如何将我的自定义字段类型限制为英语课程产品类型

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

我认为有两种方法可以实现这一点。1.将自定义字段添加到产品数据框中它们自己的窗格中。或2。继续将它们添加到您正在使用的通用数据挂钩中,然后使用JS为其他产品类型隐藏它们。

方法1

// Creates the admin panel tab
add_action( 'woocommerce_product_write_panel_tabs', 'so_26338834_product_write_panel_tab' );
function so_26338834_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 so_26338834_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
}

方法2

使用show_if_{$product_type}类将您的输入封装在div中。我认为WooCommerce将为您处理隐藏/显示方面的问题。

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>';
}

编辑:我检查了自己的代码,发现您必须编写用于隐藏/显示自己的javascript。这很容易做到,因为WooCommerce在产品类型输入更改时触发一个自定义事件,所以我们可以编写一个触发该事件的处理程序。

// 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();
    }
});

添加自定义元框,然后将字段添加到元框

/**
* 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' );

您可以使用woocommerce_product_options_general_product_data操作挂钩将自定义字段添加到特定的产品类型,使用woocommerce_process_product_meta过滤器挂钩保存自定义字段。有关详细信息,请参阅链接-http://wisdmlabs.com/blog/create-product-type-custom-settings-woocommerce/.这将对你有所帮助。

  1. 在Woocommerce中创建一个自定义产品类别;称之为CCD_ 4
  2. 将以下代码添加到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. english_course_custom_fields()的开头添加以下行:

    if (!isWyDayProduct($post->ID)) {
        return;
    }
    

您应该创建一个新文件并将您的类放入其中。

class WC_Product_Wdm extends WC_Product
{
    require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/wp-config.php';
public function __construct( $product ) {
    $this->product_type = 'wdm_custom_product';
    parent::__construct( $product );
    // add additional functions here
    }
}

并在另一个文件中传递其他函数。并将类放在文件的开头

 require_once class.php';
add_filter( 'product_type_selector', 'english_course_wc_cpt' );
function english_course_wc_cpt( $types ){
$types[ 'wdm_custom_product' ] = __( 'English Courses' );
return $types;
}


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