You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需修改wc-template-functions.php,传递变量给WooCommerce变体下拉过滤钩子

解决方案:无需修改核心文件,复用变体数据并优雅实现默认激活

一、复用单次调用的变体数据(避免重复执行函数)

不用修改wc-template-functions.php,可以通过提前存储变体数据的方式,让钩子和模板共享同一结果,步骤如下:

  1. 提前获取并缓存变体数据
    在产品页和商店页的前置钩子中,调用你的变体选择函数,将结果存储到WordPress临时缓存中(避免全局变量冲突):

    add_action( 'woocommerce_before_single_product', 'cache_default_variation_data', 10 );
    add_action( 'woocommerce_before_shop_loop_item', 'cache_default_variation_data', 10 );
    
    function cache_default_variation_data() {
        global $product;
        if ( ! $product->is_type( 'variable' ) ) return;
    
        // 调用你的自定义变体选择函数
        $default_variation = your_custom_variation_selection_function( $product );
        if ( $default_variation ) {
            // 用产品ID作为缓存键,避免不同产品数据冲突
            wp_cache_set( 'default_variation_' . $product->get_id(), $default_variation, 'woocommerce' );
        }
    }
    
  2. 在下拉钩子中复用缓存数据
    给woocommerce_dropdown_variation_attribute_options_args钩子添加回调,取出缓存的index值设置默认选中项:

    add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'set_default_variation_dropdown', 10, 1 );
    
    function set_default_variation_dropdown( $args ) {
        global $product;
        $default_variation = wp_cache_get( 'default_variation_' . $product->get_id(), 'woocommerce' );
        if ( $default_variation && isset( $default_variation['index'] ) ) {
            $args['selected'] = $default_variation['index'];
        }
        return $args;
    }
    
  3. 在自定义variable.php中复用缓存数据
    在你自定义的variable.php模板里,取出缓存的id值过滤变体数组:

    global $product;
    $default_variation = wp_cache_get( 'default_variation_' . $product->get_id(), 'woocommerce' );
    if ( $default_variation && isset( $default_variation['id'] ) ) {
        $available_variations = array_filter( $available_variations, function( $variation ) use ( $default_variation ) {
            return $variation['variation_id'] == $default_variation['id'];
        } );
    }
    

二、更优雅的默认变体激活方案

上述方法可以解决复用问题,但还有更贴合WooCommerce原生逻辑的方案——利用woocommerce_get_variation_default_attributes钩子直接设置默认属性,让WooCommerce自动处理下拉选中和加购按钮激活:

  1. 设置默认属性

    add_filter( 'woocommerce_get_variation_default_attributes', 'set_default_variation_attributes', 10, 2 );
    
    function set_default_variation_attributes( $default_attributes, $product ) {
        if ( ! $product->is_type( 'variable' ) ) return $default_attributes;
    
        // 调用你的自定义变体选择函数
        $default_variation = your_custom_variation_selection_function( $product );
        if ( $default_variation && isset( $default_variation['id'] ) ) {
            $variation = wc_get_product( $default_variation['id'] );
            if ( $variation ) {
                // 获取该变体的属性键值对,赋值给默认属性
                $default_attributes = $variation->get_attributes();
            }
        }
        return $default_attributes;
    }
    
  2. 商店页自动激活加购按钮
    产品页的加购按钮会随默认属性自动激活,商店页则需要替换加购链接为对应变体的链接:

    add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_shop_loop_variation_add_to_cart', 10, 2 );
    
    function replace_shop_loop_variation_add_to_cart( $link, $product ) {
        if ( ! $product->is_type( 'variable' ) ) return $link;
    
        $default_variation = your_custom_variation_selection_function( $product );
        if ( $default_variation && isset( $default_variation['id'] ) ) {
            $variation_id = $default_variation['id'];
            // 构建变体加购链接
            $link = sprintf(
                '<a href="%s" data-quantity="1" class="button product_type_variable add_to_cart_button ajax_add_to_cart" data-product_id="%s" data-product_variation_id="%s" aria-label="%s" rel="nofollow">%s</a>',
                esc_url( $product->add_to_cart_url() ),
                esc_attr( $product->get_id() ),
                esc_attr( $variation_id ),
                esc_attr( sprintf( __( 'Add %s to your cart', 'woocommerce' ), $product->get_name() ) ),
                esc_html( $product->add_to_cart_text() )
            );
        }
        return $link;
    }
    

注意事项

  • 确保你的自定义变体选择函数能正确处理产品对象,返回有效的id和index(或属性键值对)
  • 商店页的可变产品默认加购按钮激活需要主题模板支持,若主题有自定义商店页模板,可能需要调整对应逻辑
  • 使用wp_cache_set/wp_cache_get时,缓存会在请求结束后自动清理,不会造成数据残留

内容的提问来源于stack exchange,提问作者Rip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 04:06:10