79422948

Date: 2025-02-08 09:57:06
Score: 0.5
Natty:
Report link

I would suggest using a plugin like Custom Product Type for WooCommerce (Add-Ons, Data, Options, Booking, and Appointments).

For the code method use this:

// #1 Add New Product Type to Select Dropdown
  
add_filter( 'product_type_selector', 'wpsaad_add_custom_product_type' );
  
function wpsaad_add_custom_product_type( $types ){
    $types[ 'custom' ] = 'Custom product';
    return $types;
}
  
// --------------------------
// #2 Add New Product Type Class
  
add_action( 'init', 'wpsaad_create_custom_product_type' );
  
function wpsaad_create_custom_product_type(){
    class WC_Product_Custom extends WC_Product {
      public function get_type() {
         return 'custom';
      }
    }
}
  
// --------------------------
// #3 Load New Product Type Class
  
add_filter( 'woocommerce_product_class', 'wpsaad_woocommerce_product_class', 10, 2 );
  
function wpsaad_woocommerce_product_class( $classname, $product_type ) {
    if ( $product_type == 'custom' ) {
        $classname = 'WC_Product_Custom';
    }
    return $classname;
}
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Mohamed Saad