将 +More Colors 标签添加到 WP 主题的集合页面 – f(x)

Adding a +More Colors label to collection page of WP theme – f(x)

提问人:austin dodd 提问时间:10/8/2023 更新时间:10/8/2023 访问量:11

问:

我正在使用 Wordpress 上的高级 Woo Labels 插件为给定某些规则集的产品创建前端标签。我已将此代码段添加到我的函数 .php 文件中,但我需要调整逻辑,以便规则集与我的条件匹配。问题是......我不知道怎么写逻辑 🥲

而不是下面的代码段,如果至少有一个子产品数量等于/不等于/小于/大于 X,则为可变产品添加特殊标签。如果产品具有 1 个以上的属性 = 颜色,我需要添加一个特殊标签。

例如:产品 https://thewalnuttreeaurora.com/product/line-dress/ 有 2 种颜色变体。

我知道这个逻辑对于我的方案来说是不正确的,这就是为什么我需要帮助来更新它以检查 IF 产品是否具有多个颜色属性(例如:灰色、白色)

add_filter( 'awl_label_rules', 'my_awl_label_rules' );
function my_awl_label_rules( $options ) {
 
    $options['Custom'][] = array(
        "name" => __( "Variable product with child quantity", "advanced-woo-labels" ),
        "id"   => "custom_var_quantity",
        "type" => "number",
        "operators" => "equals_compare",
    );
 
    return $options;
 
}
 
add_filter( 'awl_labels_condition_rules', 'my_awl_labels_condition_rules' );
function my_awl_labels_condition_rules( $rules ) {
    $rules['custom_var_quantity'] = 'condition_custom_var_quantity';
    return $rules;
}
 
function condition_custom_var_quantity( $condition_rule ) {
 
    global $product;
 
    $match = false;
 
    if ( $product->is_type( 'variable' ) ) {
        if ( sizeof( $product->get_children() ) > 0 ) {
 
            foreach ($product->get_children() as $child_id) {
 
                $variation_product = new WC_Product_Variation($child_id);
                $variation_quantity = $variation_product->get_stock_quantity();
 
                $match = awl_compare_condition_values( $condition_rule['operator'], $variation_quantity, $condition_rule['value'] );
 
                if ( $match ) {
                    break;
                }
 
            }
        }
    }
 
    return $match;
 
}
php html wordpress 字符串 函数

评论


答:

0赞 neopheus 10/8/2023 #1

是的,你的逻辑不正确。

首先,修改my_awl_label_rules中的自定义规则:

add_filter( 'awl_label_rules', 'my_awl_label_rules' );
function my_awl_label_rules( $options ) {
 
    $options['Custom'][] = array(
        "name" => __( "Product with multiple color attributes", "advanced-woo-labels" ),
        "id"   => "custom_multiple_colors",
        "type" => "number",
        "operators" => "equals_compare",
    );
 
    return $options;
}

修改条件规则筛选器以使用新的规则 ID:

add_filter( 'awl_labels_condition_rules', 'my_awl_labels_condition_rules' );
function my_awl_labels_condition_rules( $rules ) {
    $rules['custom_multiple_colors'] = 'condition_custom_multiple_colors';
    return $rules;
}

并创建新的条件函数condition_custom_multiple_colors以检查产品是否具有多个颜色属性:

function condition_custom_multiple_colors( $condition_rule ) {
    global $product;
    $match = false;

    if ( $product->is_type( 'variable' ) ) {
        $attributes = $product->get_variation_attributes();

        if ( isset($attributes['attribute_pa_color']) && count($attributes['attribute_pa_color']) > 1 ) {
            $match = true;
        }
    }
 
    return $match;
}

注意:用正确的蛞蝓替换attribute_pa_color。