I've created a custom WooCommerce product type that's based on the built-in variable product type. I want to have both product variations and custom characteristics and fields specific to my product type. This is all working and displaying fine in the admin and my custom front-end product page template.
Here's my custom product, for reference:
add_action( 'init', 'register_book_type' );
function register_book_type () {
class WC_Product_Book extends WC_Product_Variable {
public function __construct( $product ) {
parent::__construct( $product );
}
public function get_type() {
return 'book';
}
}
}
However, when I post the product with its variation to the cart, the behavior messes up because the code $adding_to_cart->get_type() in add_to_cart_action() (in the WC_Form_Handler class) is identifying the product as my custom "book" type and not treating it as a "variable" product and falling back to treating it as a "simple" product type by default.
Here's that built-in WooCommerce area that's causing me trouble:
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->get_type(), $adding_to_cart );
if ( 'variable' === $add_to_cart_handler || 'variation' === $add_to_cart_handler ) {
$was_added_to_cart = self::add_to_cart_handler_variable( $product_id );
} elseif ( 'grouped' === $add_to_cart_handler ) {
$was_added_to_cart = self::add_to_cart_handler_grouped( $product_id );
} elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ) {
do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url ); // Custom handler.
} else {
$was_added_to_cart = self::add_to_cart_handler_simple( $product_id );
}
The problem seems to be that my own get_type() method returns "book" when this code is expecting "variable." I need it to return "book" so the product edit page will recognize the type properly.
I know I can remove and replace the add_to_cart_action() function in my own code to override this behavior and add my custom type, but then I'm unable to call all the other private methods in the WC_Form_Handler class. Or is it okay to just override that entire class..?
Any other way to bypass all this to get my variable-based custom product into the shopping cart?