预先选择首个有库存的 WooCommerce 产品变体。

huangapple go评论54阅读模式
英文:

Preselect first in stock product variation woocommerce

问题

我想在没有设置默认值的情况下预先选择第一个有库存的产品变体。

目前我正在使用这段代码https://stackoverflow.com/questions/70935529/automatically-pre-select-first-in-stock-value-in-woocommerce-variable-product-dr,它几乎可以工作,但如果变体已重新排列,它会出错,因为索引不再与变体匹配。

英文:

I’d like to preselect the first product variation that is in stock if no default is set.

Currently I’m using this code https://stackoverflow.com/questions/70935529/automatically-pre-select-first-in-stock-value-in-woocommerce-variable-product-dr and it almost works but if the variations have been rearranged it breaks as the indexs no longer match up with the variations

答案1

得分: 1

I have translated the code you provided:

成功修改了我之前使用的上述代码以找到解决方案

function filter_woocommerce_dropdown_variation_attribute_options_args( $args ) {

    // 检查下拉列表中可用选项的计数
    if ( count( $args['options'] ) > 0 ) {
        // 初始化
        $options = array();
        // 获取WC_Product_Variable对象
        $product = $args['product'];
        $has_default = count($product->get_default_attributes());
        if(!$has_default) {
            // 是WC产品变体
            if ( is_a( $product, 'WC_Product_Variable' ) ) {
                // 获取当前产品的可用变体的数组
                foreach ( $product->get_available_variations() as $key => $variation ) {
                    // 是否有库存
                    $is_in_stock = $variation['is_in_stock'];
                    $attributes = $variation['attributes'];
                    // 是的
                    if ( $is_in_stock ) {
                        // 设置键
                        foreach ( $attributes as $key => $attribute ) {
                            array_push($options, $attribute);
                        }
                        // 中断
                        break;
                    }
                }
            }
            // 查找变量是否为数字
            if ( count( $options ) > 0 ) {
                // 已选择
                $option_key = "";
                foreach ( $options as $key => $option ) {
                    $i = 0;
                    while($i < count($args['options'])) {
                        if($option == $args['options'][$i]) {
                            $option_key = $i;
                            // 中断;
                        }
                        $i++;
                    }
                    // 中断;
                }
                $args['selected'] = $args['options'][$option_key];
            } else {
                $args['selected'] = "";
            }
        }
    }

    return $args;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 );
英文:

Managed to modify the above code I was using to find the solution

    function filter_woocommerce_dropdown_variation_attribute_options_args( $args ) {

    // Check the count of available options in dropdown
    if ( count( $args[&#39;options&#39;] ) &gt; 0 ) {
        // Initialize
        $options = array();
        // Get WC_Product_Variable Object
        $product = $args[&#39;product&#39;];
        $has_default = count($product-&gt;get_default_attributes());
        if(!$has_default) {
            // Is a WC Product Variable
            if ( is_a( $product, &#39;WC_Product_Variable&#39; ) ) {
                // Get an array of available variations for the current product
                foreach ( $product-&gt;get_available_variations() as $key =&gt; $variation ) {
                    // Is in stock
                    $is_in_stock = $variation[&#39;is_in_stock&#39;];
                    $attributes = $variation[&#39;attributes&#39;];
                    // True
                    if ( $is_in_stock ) {
                        // Set key
                        foreach ( $attributes as $key =&gt; $attribute ) {
                            array_push($options, $attribute);
                        }
                        // Break
                        break;
                    }
                }
            }
            // Finds whether a variable is a number
            if ( count( $options ) &gt; 0 ) {
                // Selected
                $option_key = &quot;&quot;;
                foreach ( $options as $key =&gt; $option ) {
                    $i = 0;
                    while($i &lt; count($args[&#39;options&#39;])) {
                        if($option == $args[&#39;options&#39;][$i]) {
                            $option_key = $i;
                            // break;
                        }
                        $i++;
                    }
                    // break;
                }
                $args[&#39;selected&#39;] = $args[&#39;options&#39;][$option_key];
            } else {
                $args[&#39;selected&#39;] = &quot;&quot;;
            }
        }
    }

    return $args;
}
add_filter( &#39;woocommerce_dropdown_variation_attribute_options_args&#39;, &#39;filter_woocommerce_dropdown_variation_attribute_options_args&#39;, 10, 1 );

huangapple
  • 本文由 发表于 2023年5月25日 04:42:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定