【问题标题】:Automatically pre-select first in stock value in WooCommerce variable product dropdown在 WooCommerce 可变产品下拉列表中自动预选第一个库存值
【发布时间】:2022-06-14 03:56:37
【问题描述】:

从标题可以看出,我尝试“在 WooCommerce 变量产品下拉列表中自动预先选择第一个库存值”。

目前,我正在使用下面的代码预先选择第一个变体,但我需要此代码来选择第一个 IN STOCK 变体。有什么建议吗?

function fun_select_default_option( $args ) {
    // Check the count of available options in dropdown
    if ( count($args['options']) > 0 ) {
        $args['selected'] = $args['options'][0];
    }

    return $args;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'fun_select_default_option', 10, 1 );

【问题讨论】:

    标签: wordpress woocommerce product stock product-variations


    【解决方案1】:

    回调函数仅包含$args,但通过$args['product'],您可以访问产品变量对象。

    基于此,您可以循环访问可见的子项。通过变体 ID,您可以获得产品变体对象。

    使用get_stock_status(),您可以确定状态。

    所以你得到:

    function filter_woocommerce_dropdown_variation_attribute_options_args( $args ) {
        // Check the count of available options in dropdown
        if ( count( $args['options'] ) > 0 ) {
            // Initialize
            $option_key = '';
    
            // Get WC_Product_Variable Object
            $product = $args['product'];
    
            // Is a WC Product Variable
            if ( is_a( $product, 'WC_Product_Variable' ) ) {
                // Loop through children
                foreach ( $product->get_visible_children() as $key => $variation_id ) {
                    // Get product variation object
                    $variation = wc_get_product( $variation_id );
    
                    // Is a WC Product Variation
                    if ( is_a( $variation, 'WC_Product_Variation' ) ) {
                        // Get stock status
                        $product_stock_status = $variation->get_stock_status();
    
                        // In stock
                        if ( $product_stock_status == 'instock' ) {
                            // Set key
                            $option_key = $key;
    
                            // Break
                            break;
                        }
                    }
                }
            }
    
            // Finds whether a variable is a number
            if ( is_numeric( $option_key ) ) {
                // Selected
                $args['selected'] = $args['options'][$option_key];
            }
        }
    
        return $args;
    }
    add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 );
    

    或者你也可以使用get_available_variations()$variation['is_in_stock']

    与上述答案的最大区别在于,此答案还将看到延期交货,其中延期交货被允许作为库存,因为它不考虑特定的库存状态。

    那么你得到:

    function filter_woocommerce_dropdown_variation_attribute_options_args( $args ) {
        // Check the count of available options in dropdown
        if ( count( $args['options'] ) > 0 ) {
            // Initialize
            $option_key = '';
    
            // Get WC_Product_Variable Object
            $product = $args['product'];
    
            // Is a WC Product Variable
            if ( is_a( $product, 'WC_Product_Variable' ) ) {
                // Get an array of available variations for the current product
                foreach ( $product->get_available_variations() as $key => $variation ) {
                    // Is in stock
                    $is_in_stock = $variation['is_in_stock'];
    
                    // True
                    if ( $is_in_stock ) {
                        // Set key
                        $option_key = $key;
    
                        // Break
                        break;
                    }
                }
            }
    
            // Finds whether a variable is a number
            if ( is_numeric( $option_key ) ) {
                // Selected
                $args['selected'] = $args['options'][$option_key];
            }
        }
    
        return $args;
    }
    add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 );
    

    【讨论】:

      【解决方案2】:

      谢谢!你的生活更安全:-)

      【讨论】:

        猜你喜欢
        • 2018-06-06
        • 1970-01-01
        • 2020-11-07
        • 2018-07-07
        • 2023-03-06
        • 1970-01-01
        • 2017-06-11
        • 2020-02-06
        • 2019-07-25
        相关资源
        最近更新 更多