Plugin Directory

Changeset 3242765


Ignore:
Timestamp:
02/18/2025 04:26:40 PM (14 months ago)
Author:
multiwoomanager
Message:

Fixed products catalog_visibility value during synchronization

Location:
multi-woo-manager/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • multi-woo-manager/trunk/README.txt

    r3240547 r3242765  
    66Tested up to: 6.7.1
    77Requires PHP: 8.0
    8 Stable tag: 1.1.6
     8Stable tag: 1.1.7
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    5757== Changelog ==
    5858
     59= 1.1.7 =
     60* Fixed product's catalog visibility value synchronization
     61
    5962= 1.1.6 =
    6063* Fixed a recursion bug where prefixes were lost in nested ACF group fields.
     
    6770== Upgrade Notice ==
    6871
     72= 1.1.7 =
     73* Recommended update to fix catalog_visibility during products synchronization.
     74
    6975= 1.1.6 =
    7076* Recommended update to fix field prefix recursion for nested group fields.
  • multi-woo-manager/trunk/admin/class-multi-woo-manager-admin.php

    r3236761 r3242765  
    259259     * @return mixed
    260260     */
    261     function get_woocommerce_products($paged = 1, $posts_per_page = 10, $sku = null): mixed
    262     {
     261    function get_woocommerce_products($paged = 1, $posts_per_page = 10, $sku = null): mixed {
    263262        global $wpdb;
    264263
     
    285284        }
    286285
    287         // Base query to fetch WooCommerce products excluding variations
     286        // Build the query.
     287        // We remove the old _visibility meta join and instead use taxonomy tables.
     288        // Use GROUP_CONCAT() to get all product_visibility term slugs for each product.
    288289        $query = "
    289290        SELECT
     
    293294            p.post_status AS status,
    294295            p.post_title AS name,
    295             COALESCE(pm_visibility.meta_value, 'visible') as catalog_visibility,
     296            GROUP_CONCAT(DISTINCT t_visibility.slug SEPARATOR ',') as visibility_terms,
    296297            COALESCE(pm_stock.meta_value, IFNULL(pm_stock_status.meta_value, 'outofstock')) as stock,
    297298            COALESCE(pm_manage_stock.meta_value, 'no') as manage_stock,
     
    300301             FROM {$wpdb->posts} vp
    301302             WHERE vp.post_parent = p.ID
    302              AND vp.post_type = 'product_variation') as has_variations
     303               AND vp.post_type = 'product_variation') as has_variations
    303304        FROM {$wpdb->posts} p
    304         LEFT JOIN {$wpdb->postmeta} pm_sku ON (p.ID = pm_sku.post_id AND pm_sku.meta_key = '_sku')
    305         LEFT JOIN {$wpdb->postmeta} pm_visibility ON (p.ID = pm_visibility.post_id AND pm_visibility.meta_key = '_visibility')
    306         LEFT JOIN {$wpdb->postmeta} pm_stock ON (p.ID = pm_stock.post_id AND pm_stock.meta_key = '_stock')
    307         LEFT JOIN {$wpdb->postmeta} pm_stock_status ON (p.ID = pm_stock_status.post_id AND pm_stock_status.meta_key = '_stock_status')
    308         LEFT JOIN {$wpdb->postmeta} pm_manage_stock ON (p.ID = pm_manage_stock.post_id AND pm_manage_stock.meta_key = '_manage_stock')
     305        LEFT JOIN {$wpdb->postmeta} pm_sku
     306            ON (p.ID = pm_sku.post_id AND pm_sku.meta_key = '_sku')
     307        LEFT JOIN {$wpdb->term_relationships} tr_visibility
     308            ON (p.ID = tr_visibility.object_id)
     309        LEFT JOIN {$wpdb->term_taxonomy} tt_visibility
     310            ON (tr_visibility.term_taxonomy_id = tt_visibility.term_taxonomy_id AND tt_visibility.taxonomy = 'product_visibility')
     311        LEFT JOIN {$wpdb->terms} t_visibility
     312            ON (tt_visibility.term_id = t_visibility.term_id)
     313        LEFT JOIN {$wpdb->postmeta} pm_stock
     314            ON (p.ID = pm_stock.post_id AND pm_stock.meta_key = '_stock')
     315        LEFT JOIN {$wpdb->postmeta} pm_stock_status
     316            ON (p.ID = pm_stock_status.post_id AND pm_stock_status.meta_key = '_stock_status')
     317        LEFT JOIN {$wpdb->postmeta} pm_manage_stock
     318            ON (p.ID = pm_manage_stock.post_id AND pm_manage_stock.meta_key = '_manage_stock')
    309319        WHERE p.post_type IN ($placeholders)
    310         AND p.post_status NOT IN ('trash')";
    311 
     320          AND p.post_status NOT IN ('trash')
     321    ";
     322
     323        // If SKU filter is provided, add it. Otherwise, apply LIMIT.
    312324        if ($sku) {
    313325            $query .= " AND pm_sku.meta_value = %s";
    314326            $params = array_merge($product_types, [$sku]);
    315327        } else {
    316             $query .= " LIMIT %d, %d";
     328            $query .= " GROUP BY p.ID LIMIT %d, %d";
    317329            $params = array_merge($product_types, [$offset, $posts_per_page]);
    318330        }
    319331
    320         // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery -- Direct query needed for performance and systematic reasons.
     332        // For performance reasons, we use a direct query.
     333        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery
    321334        $products = $wpdb->get_results($wpdb->prepare($query, ...$params));
    322335
    323         // Cache the results
     336        // Process each product to determine catalog_visibility based on your mapping.
     337        foreach ($products as &$product) {
     338            // Explode the comma-separated list (if any) into an array.
     339            $terms = !empty($product->visibility_terms) ? explode(',', $product->visibility_terms) : [];
     340
     341            $has_exclude_from_search  = in_array('exclude-from-search', $terms, true);
     342            $has_exclude_from_catalog = in_array('exclude-from-catalog', $terms, true);
     343
     344            // Apply mapping:
     345            if ($has_exclude_from_search && $has_exclude_from_catalog) {
     346                // Hidden: excluded from both search and catalog
     347                $product->catalog_visibility = 'hidden';
     348            } elseif ($has_exclude_from_catalog && !$has_exclude_from_search) {
     349                // Only search: product is not in catalog
     350                $product->catalog_visibility = 'search';
     351            } elseif ($has_exclude_from_search && !$has_exclude_from_catalog) {
     352                // Only catalog: product is not in search
     353                $product->catalog_visibility = 'catalog';
     354            } else {
     355                // If neither term is applied, default to visible
     356                $product->catalog_visibility = 'visible';
     357            }
     358
     359            // (Optional) Remove the raw visibility_terms property if not needed.
     360            unset($product->visibility_terms);
     361        }
     362        unset($product); // break reference
     363
     364        // Cache the results for one minute.
    324365        wp_cache_set($cache_key, $products, 'woocommerce_products', MINUTE_IN_SECONDS);
    325366
     
    481522        // Count total matching attachments
    482523        $count_query = "SELECT COUNT(*) FROM {$wpdb->posts} {$where_clause}";
     524        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
    483525        $total = (int) $wpdb->get_var($count_query);
    484526
     
    495537        }
    496538
     539        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
    497540        $attachments = $wpdb->get_results($query);
    498541
  • multi-woo-manager/trunk/multi-woo-manager.php

    r3240547 r3242765  
    1818 * Plugin URI:        https://multiwoomanager.com
    1919 * Description:       WooCommerce products management, the easy way. Plugin extends WooCommerce REST API in order to provide optimized API routes for product management.
    20  * Version:           1.1.6
     20 * Version:           1.1.7
    2121 * Author:            MultiWooManager
    2222 * Author URI:        https://multiwoomanager.com/
Note: See TracChangeset for help on using the changeset viewer.