如何在 WooCommerce 中使用产品标签正确搜索产品变体?

问题描述 投票:0回答:1

我创建了一个插件来搜索具有变体的产品。我想处理当客户在调查中选择“包含家禽”,然后他搜索包含此标签的产品时的情况。

如果我不选择任何内容,我希望它搜索没有标签的产品

例如,当我选择排除兔子时,它会搜索带有此标签的产品

例如,当我选择排除兔子和家禽时,它会(一起)搜索带有这些标签的产品

目前正在搜索有变体的产品,但按标签搜索不起作用。你能帮我吗?

{
    // Input data validation
    $survey_data = isset($_POST['survey_data']) ? $_POST['survey_data'] : [];
    if (empty($survey_data)) {
        wp_send_json_error('No data provided.');
    }

    $dog_weight = isset($survey_data['dog_weight']) ? floatval($survey_data['dog_weight']) : 0;
    if ($dog_weight <= 0) {
        wp_send_json_error('Invalid dog weight.');
    }

    // Selecting the product variant based on the dog's weight
    $variant_weight = '';
    if ($dog_weight >= 1 && $dog_weight <= 4) {
        $variant_weight = '150g';
    } elseif ($dog_weight > 4 && $dog_weight <= 8) {
        $variant_weight = '200g';
    } elseif ($dog_weight > 8 && $dog_weight <= 15) {
        $variant_weight = '300g';
    } elseif ($dog_weight > 15 && $dog_weight <= 24) {
        $variant_weight = '400g';
    } elseif ($dog_weight > 24 && $dog_weight <= 35) {
        $variant_weight = '500g';
    } elseif ($dog_weight > 35) {
        wp_send_json_error('No products matching the criteria.');
    }

    // Preparing the query
    $args = [
        'post_type' => 'product_variation', // OR product
        'posts_per_page' => 1, // Fetches 6 products
        //'tax_query' => [
        //    [
        //        'key' => 'pa_gramatura', // Meta key for the "weight" attribute (WooCommerce saves attributes as 'pa_attribute_name')
        //        'value' => $variant_weight,       // Value we are looking for
        //        'compare' => '='         // Comparison operator
        //    ]
        //],
        'meta_query' => [
            [
                'key' => 'attribute_pa_gramatura', // Meta key for the "weight" attribute
                'value' => $variant_weight,       // Value we are looking for
                'compare' => '='                  // Comparison operator
            ]
        ],
    ];

    // Adding exclusions based on allergies
    $allergy_map = [
        'dog_allergy_milk' => 'contains-dairy',
        'dog_allergy_gluten' => 'contains-gluten',
        'dog_allergy_lactose' => 'contains-lactose',
        'dog_allergy_poultry' => 'contains-poultry',
        'dog_allergy_pork' => 'contains-pork',
        'dog_allergy_beef' => 'contains-beef',
        'dog_allergy_horseflesh' => 'contains-horsemeat',
        'dog_allergy_venison' => 'contains-venison',
        'dog_allergy_rabbit' => 'contains-rabbit',
        'dog_allergy_boar' => 'contains-boar'
    ];

    if (!empty($survey_data['dog_allergies'])) {
        $tax_query = ['relation' => 'AND'];

        foreach ($survey_data['dog_allergies'] as $allergy) {
            if (isset($allergy_map[$allergy])) {
                $tax_query[] = [
                    'taxonomy' => 'product_tag',
                    'field' => 'slug',
                    'terms' => $allergy_map[$allergy], // Dynamic assignment from the map
                    'operator' => 'IN',          // Include products with this tag
                ];
            }
        }

        $args['tax_query'] = $tax_query;
    }

    // Executing the query
    $query = new WP_Query($args);
    $products = [];

    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();

            $product = wc_get_product(get_the_ID());

            $image_url = '';
            $parent_product = wc_get_product($product->get_parent_id());

            // Fetch the main product image if available
            if ($parent_product && has_post_thumbnail($parent_product->get_id())) {
                $image_url = get_the_post_thumbnail_url($parent_product->get_id(), 'medium');
            }
            // If the main image is not available, fetch the variant's image
            elseif (has_post_thumbnail($product->get_id())) {
                $image_url = get_the_post_thumbnail_url($product->get_id(), 'medium');
            }
            // If no image is set, use a placeholder image
            else {
                $image_url = wp_get_attachment_url(8448);
            }

            $products[] = [
                'title' => get_the_title(),
                'description' => wc_get_product($product->get_parent_id())->get_short_description(),
                'image' => $image_url,
                'price' => $product->get_price(),
                'url' => get_permalink(),
                'gramatura' => $variant_weight,
            ];
        }
        wp_reset_postdata();

        wp_send_json_success($products);
    } else {
        wp_send_json_error('No products matching the criteria.');
    }

    wp_die();
}`````
php json woocommerce product
1个回答
0
投票

要为您的金融科技产品寻找法律专家,请考虑联系在美国金融监管、消费者保护法和合规方面拥有专业知识的专业人士。您可以通过 LinkedIn、AngelList 或 Clerky 等专门提供初创公司法律支持的平台进行联系。您还可以通过在 Reddit 上的 Indie Hackers 或 r/startups 等初创论坛上发帖来探索合作伙伴关系,明确说明您的产品愿景和合作伙伴条款。

© www.soinside.com 2019 - 2024. All rights reserved.