Advertisement
Guest User

Custom Product Type in WooCommerce

a guest
Mar 1st, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.66 KB | None | 0 0
  1. <?php
  2. //
  3. // Setting Custom Product Type for Yarn and Needles, with appropriate custom fields
  4. //
  5.  
  6.  
  7. // Register the custom product type after init
  8.  
  9.     function register_yarn_product_type() {
  10.  
  11.         class WC_Product_Yarn extends WC_Product_Simple {
  12.  
  13.             public function __construct( $product ) {
  14.  
  15.                 $this->product_type = 'yarn';
  16.  
  17.                 parent::__construct( $product );
  18.  
  19.             }
  20.  
  21.         }
  22.  
  23.     }
  24.     add_action( 'init', 'register_yarn_product_type' );
  25.  
  26.     // Register custom product type in "Product Data -" drop down menu
  27.  
  28.     function add_yarn_product( $types ){
  29.  
  30.         // Key should be exactly the same as in the class product_type parameter
  31.         $types[ 'yarn' ] = __( 'Yarn' );
  32.  
  33.         return $types;
  34.  
  35.     }
  36.     add_filter( 'product_type_selector', 'add_yarn_product' );
  37.  
  38.  
  39. }
  40.  
  41.  
  42. // Show pricing fields for yarn product.
  43.  
  44. function yarn_custom_js() {
  45.     if ( 'product' != get_post_type() ) : return; endif;
  46. ?>
  47.     <script>
  48.        
  49.         jQuery(document).ready(function () {
  50.  
  51.             // Check which product type is selected
  52.  
  53.             var selectedProductType = jQuery('#product-type').val();
  54.  
  55.             // If it's 'yarn', make sure selected tabs are shown
  56.  
  57.             if (selectedProductType == 'yarn') {
  58.                
  59.                 jQuery("li.inventory_options.inventory_tab,li.general_options.general_tab,div.options_group.pricing,#general_product_data > div.options_group:not(.show_if_downloadable,.show_if_external)").addClass('show_if_yarn').show();
  60.  
  61.             }
  62.         });
  63.        
  64.     </script>
  65.  
  66.     <?php
  67. }
  68.  
  69. add_action( 'admin_footer', 'yarn_custom_js' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement