programing

WooCommerce에서 사용자 정의 제품 유형에 대한 가격 및 재고 활성화 방법

minimums 2023. 10. 10. 20:12
반응형

WooCommerce에서 사용자 정의 제품 유형에 대한 가격 및 재고 활성화 방법

WooCommerce 응용 프로그램에서 사용자 지정 제품 유형을 만들었습니다.

function register_variable_bulk_product_type() {

    class WC_Product_Variable_bulk extends WC_Product_Simple {

        public function __construct($product) {

            $this->product_type = 'variable_bulk';
            parent::__construct($product);
        }

    }

}

add_action('init', 'register_variable_bulk_product_type');

function add_variable_bulk_product($types) {

    $types['variable_bulk'] = __('Variable Bulk');

    return $types;
}

add_filter('product_type_selector', 'add_variable_bulk_product');

제품 데이터 드롭다운에 제품 종류를 표시하면 다음과 같습니다.

enter image description here

저의 고민은.

신제품은 재고와 가격을 추가할 수 있는 옵션이 없는데, 이 옵션을 활성화하려면 어떻게 해야 하나요?

가격 및 인벤토리 탭을 표시하려면 작은 JS 트릭이 필요합니다. 즉 클래스를 추가해야 합니다.show_if_{your_custom_product_type}당신의 경우는 그럴 것입니다.show_if_variable_bulk.

작업 코드는 다음과 같습니다.

function wh_variable_bulk_admin_custom_js() {

    if ('product' != get_post_type()) :
        return;
    endif;
    ?>
    <script type='text/javascript'>
        jQuery(document).ready(function () {
            //for Price tab
            jQuery('.product_data_tabs .general_tab').addClass('show_if_variable_bulk').show();
            jQuery('#general_product_data .pricing').addClass('show_if_variable_bulk').show();
            //for Inventory tab
            jQuery('.inventory_options').addClass('show_if_variable_bulk').show();
            jQuery('#inventory_product_data ._manage_stock_field').addClass('show_if_variable_bulk').show();
            jQuery('#inventory_product_data ._sold_individually_field').parent().addClass('show_if_variable_bulk').show();
            jQuery('#inventory_product_data ._sold_individually_field').addClass('show_if_variable_bulk').show();
        });
    </script>
    <?php

}

add_action('admin_footer', 'wh_variable_bulk_admin_custom_js');

코드가 들어갑니다.functions.php활성 하위 테마(또는 테마)의 파일입니다.또는 플러그인 PHP 파일에서도 마찬가지입니다.
코드를 테스트하고 작동합니다.

일반 탭의 모양은 다음과 같습니다.

enter image description here

인벤토리 탭은 이렇게 표시됩니다.

enter image description here

도움이 되길 바랍니다!

언급URL : https://stackoverflow.com/questions/43109755/how-to-enable-price-and-inventory-for-custom-product-type-in-woocommerce

반응형