I have already searched for the solution and got nothing in this regard.
In Woocommerce, I am registering a custom product type (giftcard) which extends WC_PRODUCT_VARIABLE and then I set attributes, variations and inventory tabs to be shown when the product type is of giftcard.
After that, I copied variable.php template to giftcard.php. The issue is that I am not getting the variations shown on the frontend.
It is always saying:
This product is currently out of stock and unavailable.
Product Type Setup:
add_filter('product_type_selector', 'knlShopCreateGiftcardProductType');
function knlShopCreateGiftcardProductType($types) {
$types['knl_shop_giftcard'] = __('Gift Card', 'knl-shop');
return $types;
}
add_action('init', 'knlShopGiftcardProductTypeClass');
function knlShopGiftcardProductTypeClass() {
require_once KNL_SHOP_INC . '/class-knlshop-wc-giftcard-prod-type.php';
}
add_filter('woocommerce_product_class', 'knlShopInitGiftcardProductTypeClass', 10, 2);
function knlShopInitGiftcardProductTypeClass( $classname, $product_type ) {
if ( $product_type == 'knl_shop_giftcard' ) {
$classname = 'KNL_SHOP_WC_GIFTCARD_PROD_TYPE';
}
return $classname;
}
add_action('admin_footer', 'knlShopGiftcardProductTypeDataTabs');
function knlShopGiftcardProductTypeDataTabs() {
if('product' != get_post_type()) :
return;
endif;
?>
<script type='text/javascript'>
jQuery(document).ready(function () {
jQuery('.enable_variation').addClass('show_if_knl_shop_giftcard').show();
jQuery('.inventory_options').addClass('show_if_knl_shop_giftcard').show();
jQuery('#inventory_product_data ._manage_stock_field').addClass('show_if_knl_shop_giftcard').show();
jQuery('#inventory_product_data ._sold_individually_field').parent().addClass('show_if_knl_shop_giftcard').show();
jQuery('#inventory_product_data ._sold_individually_field').addClass('show_if_knl_shop_giftcard').show();
});
</script>
<?php
}
add_filter('woocommerce_product_data_tabs', 'knlShopAddProductDataTabsForGiftcard', 10, 1);
function knlShopAddProductDataTabsForGiftcard($tabs) {
array_push($tabs['attribute']['class'], 'show_if_variable show_if_knl_shop_giftcard');
array_push($tabs['variations']['class'], 'show_if_knl_shop_giftcard');
array_push($tabs['inventory']['class'], 'show_if_knl_shop_giftcard');
//array_push($tabs['general']['class'], 'show_if_knl_shop_giftcard');
return $tabs;
}
add_action('woocommerce_knl_shop_giftcard_add_to_cart', 'knl_shop_giftcard_add_to_cart');
function knl_shop_giftcard_add_to_cart() {
wc_get_template('single-product/add-to-cart/giftcard.php', $args = array(), $template_path = '', KNL_SHOP_PATH . '/woocommerce/');
}
giftcard.php is a copy of variable.php
And then my custom product class is as follows:
class KNL_SHOP_WC_GIFTCARD_PROD_TYPE extends WC_Product_Variable {
public function __construct( $product = 0 ) {
parent::__construct( $product );
}
public function get_type() {
return 'knl_shop_giftcard';
}
}
Now, when I create the giftcard product, i can see all the attributes and variations in the product edit page. But on frontend, single page gives the above mentioned error that its out of stock.
On checking the values of $attributes and $available_variations, i get either empty or null.
I hope, I have explained my question well. Thanks in advance for the help.