【发布时间】:2015-07-04 01:43:30
【问题描述】:
我正在创建一个具有多种产品类型的 woocommerce 主题:手机、平板电脑、相机……
我创建了这样的产品类型:
add_filter( 'product_type_selector', 'add_mobile_product_type' );
function add_mobile_product_type( $types ){
$types[ 'mobile' ] = __( 'Mobile' );
$types[ 'tablet' ] = __( 'Tablet' );
return $types;
}
add_action( 'plugins_loaded', 'create_mobile_product_type' );
function create_mobile_product_type(){
class WC_Product_Mobile extends WC_Product{
public function __construct( $product ) {
$this->product_type = 'mobile';
parent::__construct( $product );
}
}
class WC_Product_Tablet extends WC_Product{
public function __construct( $product ) {
$this->product_type = 'tablet';
parent::__construct( $product );
}
}
}
我创建了一些额外的字段,如下所示:
add_action( 'woocommerce_product_options_general_product_data', 'mobile_custom_fields' );
function mobile_custom_fields() {
global $woocommerce, $post;
$prefix = 'mobile_';
echo '<div class="options_group">';
woocommerce_wp_select(
array(
'id' => $prefix . 'sim',
'label' => __( 'Sim Count', 'faitell' ),
'options' => array(
'1' => __( '1 Sim', 'faitell' ),
'2' => __( '2 Sim', 'faitell' ),
'3' => __( '3 Sim', 'faitell' )
)
)
);
echo '</div>';
}
问题是这个字段将显示在所有产品类型上,如何为移动设备显示一些字段,而为平板电脑显示其他一些字段。有什么办法吗?
【问题讨论】:
标签: php wordpress woocommerce custom-fields