php - WooCommerce Custom fields for custom Product Type -
i'm creating woocommerce theme have multiple product type: mobile, tablet, camera,...
i created products types this:
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 ); } } }
and created fields this:
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>'; }
the problem fields shown on product types, how show fields mobile , others tablet. there way?
i couldn't find easy way in php, thought add javascript hide field when needed.
therefore mobile_custom_fields
function became:
function mobile_custom_fields() { global $woocommerce, $post; $prefix = 'mobile_'; echo '<div class="options_group" id="sim_count_select">'; 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>'; ?> <script> jquery(document).ready(function($){ $('#product-type').change(function() { // check if it's selected mobile or tablet product type var is_mobile = $.inarray( $(this).val(), ['mobile', 'tablet'] ) > -1; // toggle div visibility based on previous information $('#sim_count_select').toggle( is_mobile ); }).trigger('change'); }); </script> <?php }
note id="sim_count_select"
added div
easy targeting.
not elegant solution, should work.
Comments
Post a Comment