Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce Creating a custom variable product type

I'm trying to create a custom variable product type from woocommerce for a booking form, which has different pricing based on a set of variables.

I've successfully added a custom product type. But how do i duplicate the same option that the variable product has for different attribute pricing. Can't seem to find any resources related with variable product types.

// add a product type
add_filter( 'product_type_selector', 'cpt_add_custom_product_type' );
function cpt_add_custom_product_type( $types ){
    $types[ 'booking_product' ] = __( 'Booking Product' );
    return $types;
}
add_action( 'plugins_loaded', 'cpt_create_custom_product_type' );
function cpt_create_custom_product_type(){
     // declare the product class
     class WC_Product_Wdm extends WC_Product_Variable {
        public function __construct( $product ) {
           $this->product_type = 'booking_product';
           parent::__construct( $product );
           // add additional functions here
        }
    }
}
like image 788
mark5 Avatar asked Feb 07 '23 14:02

mark5


2 Answers

I've managed to get part way - by adding back in the variation and attribute tabs. Next step is to add back in "Add as a Variation" option.

add_filter('woocommerce_product_data_tabs', function($tabs) {

            array_push($tabs['attribute']['class'], 'show_if_variable show_if_membership');
            array_push($tabs['variations']['class'], 'show_if_membership');

            return $tabs;

        }, 10, 1);  
like image 98
Dan Needham Avatar answered Feb 09 '23 07:02

Dan Needham


Dan Needham's answer is the first step you should follow and after that you should use this

function producttype_custom_js() {

if ( 'product' != get_post_type() ) :
    return;
endif;

?><script type='text/javascript'>
    jQuery( document ).ready( function() {
        jQuery( '.enable_variation' ).addClass( 'show_if_cus_producttype' ).show();
    });

</script><?php 
} 

add_action( 'admin_footer', 'producttype_custom_js' );

This will add the checkbox option Dan Needham has said is missing.

like image 23
Suvajit Pal Avatar answered Feb 09 '23 06:02

Suvajit Pal