【问题标题】:WooCommerce Auto Select color optionWooCommerce 自动选择颜色选项
【发布时间】:2023-01-26 04:14:18
【问题描述】:

我的商店有近 50.000 种产品,没有为每种产品手动选择默认颜色的选项。现在,我有了这段代码,但它选择了第一个变体选项,而不仅仅是颜色。

add_filter('woocommerce_dropdown_variation_attribute_options_args','fun_select_default_option',10,1);
function fun_select_default_option( $args)
{

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

长话短说,我如何才能在浏览我的商店时为我的访客自动选择第一个也是唯一的颜色?

【问题讨论】:

    标签: javascript php wordpress woocommerce


    【解决方案1】:

    尝试这个 :

    add_filter('woocommerce_variation_select_matching_attributes', 'custom_select_default_color', 10, 2);
    function custom_select_default_color( $options, $product ) {
        $attributes = $product->get_attributes();
        if ( isset( $attributes['pa_color'] ) ) {
            $options['pa_color'] = $attributes['pa_color']['options'][0];
        }
        return $options;
    }
    

    假设颜色的属性名称是“pa_color”,您应该将其替换为您的属性名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 1970-01-01
      相关资源
      最近更新 更多