php - Woocommerce custom downloadable product type -
i have create custom wc product type, defined follow:
class wc_product_my_product extends wc_product_simple { public function __construct( $product ) { $this->product_type = 'my_product'; $this->virtual = 'yes'; $this->downloadable = 'yes'; $this->manage_stock = 'no'; } }
as can see product is, how intended, virtual product consisting in downloadable files. using jquery define settings displayed when product selected. showing fields displayed simple product virtual , downloadable selected.
/* * apply same settings virtual / downloadable files */ jquery( '.options_group.show_if_downloadable' ).addclass( 'show_if_my_product' ); jquery( '.hide_if_virtual' ).addclass( 'hide_if_my_product' ); jquery( 'body' ).on( 'woocommerce-product-type-change', function( event, select_val, select ) { if ( select_val == 'my_product' ) { jquery( '.show_if_my_product' ).show(); jquery( '.hide_if_my_product' ).hide(); } else { jquery( '.show_if_my_product' ).show(); jquery( '.hide_if_my_product' ).hide(); }
now, problem "virtual" , "downloadable" checkboxes not selected when selecting product dropdown list, since not default simple products. woo commerce uses jquery show / hide different fields different product types (but fields still there), when new product added wordpress stores values among posts meta. thus, unless remember first check on virtual , downloadable before switching default simple product product, download settings , links not stored.
now, solution rather simple (and working). manually (err... using jquery) checking "virtual" , "downloadable" checkbox. so:
jquery( '#_virtual' ).attr( 'checked' , true ); jquery( '#_downloadable' ).attr( 'checked' , true );
now, said, works, there nothing "fix". however, not satisfied solution looks work around. understanding (perhaps wrong?) product class defines them being virtual , downloadable, , shouldn't follow hidden input field there other products saying. perhaps there missing in php defined product? or perhaps misunderstanding entirely "virtual" , "downloadable" properties of wc_product subclass mean?
if proper solution, should remove definition virtual , downloadable in php class?
in end, solution works , can't see major problem it, still seems missing , shouldn't done way. being paranoid?
i found solution fix problem. need add code
add_action( 'product_type_options', 'wc_custom_product_type_options' ); function wc_custom_product_type_options($options){ $options['downloadable']['wrapper_class'] = 'show_if_simple show_if_my_product'; $options['virtual']['wrapper_class'] = 'show_if_simple show_if_my_product'; return $options; }
Comments
Post a Comment