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

标签 php wordpress woocommerce

我的代码显示在所有产品类型中,例如简单产品、可变产品、自定义类型...... 对所有人都可用的方法,但我只想将其限制为我的自定义类型。

如何将我的自定义字段类型限制为英语类(class)产品类型

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. 将自定义字段添加到产品数据框中自己的 Pane 中。或者 2. 继续将它们添加到您正在使用的通用数据 Hook 中,然后使用 JS 隐藏它们以用于其他产品类型。

方法 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
}

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

关于php - 如何仅为自定义产品类型定义字段 - Woo Commerce Hook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26338834/

相关文章:

php - Mysql连接3个表并输出第一个表中的所有记录,而不管另外2个表记录为空

javascript - 在一天的特定时间刷新网页

mysql - 将 ExpressionEngine 成员导出到 WordPress 用户

php - 无法在单个产品页面中重新排序产品标签?

php - 仅对 Woocommerce 中的特定自定义字段值启用免费送货方式

php - 如何使用 For 循环将数组的数据插入不同的行

php - 如何在 PHP 中以这种方式修改数组的键?

wordpress - 如何获取更新的 WooCommerce 订单数据?

php - Woocommerce 优惠券添加自定义复选框

Php 转换时间为 gmt 格式