WooCommerce自定义变量产品类型(缺货问题)

时间:2020-07-09 11:52:59

标签: wordpress woocommerce

我已经在寻找解决方案,但在这方面一无所获。

在Woocommerce中,我正在注册扩展WC_PRODUCT_VARIABLE的自定义产品类型(礼品卡),然后设置当产品类型为Giftcard时要显示的属性,变体和库存标签。

之后,我将variable.php模板复制到了giftcard.php。问题是我没有在前端显示变化。

它总是在说:

该产品目前无货,不可用。

产品类型设置:

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是variable.php的副本

然后我的自定义产品类如下:

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';
    }
}

现在,当我创建礼品卡产品时,我可以在产品编辑页面中看到所有属性和变体。但是在前端,单页会出现上述错误,表明它缺货。

在检查 $ attributes 的值时, $ available_variations ,我为空或为空。

希望,我已经很好地解释了我的问题。预先感谢您的帮助。

0 个答案:

没有答案