gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:50:10 29 4
gpt4 key购买 nike

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

如何将我的自定义字段类型限制为英语类(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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com