get_type(), array( 'simple', 'variable', 'variation' ), true ) ) { $issues[] = 'Product type requires a separate agent-checkout compatibility test.'; } if ( $product->get_sku() === '' ) { $issues[] = 'Missing SKU; numeric product identifiers still work, but a stable SKU improves catalogue matching.'; } if ( $product->get_catalog_visibility() === 'hidden' ) { $issues[] = 'Hidden from the catalogue; agents may not discover this product.'; } if ( $product->get_description() === '' && $product->get_short_description() === '' && ! $product->is_type( 'variation' ) ) { $issues[] = 'Add a description with the attributes needed to choose this product.'; } if ( ! $product->is_type( 'variable' ) && $product->get_price() === '' ) { $issues[] = 'No price is configured.'; } if ( ! $product->is_in_stock() ) { $issues[] = 'Out of stock; confirm the agent handles this without substituting products.'; } if ( $product->needs_shipping() && $product->get_weight() === '' ) { $issues[] = 'No weight; review if your shipping rates depend on weight.'; } if ( $product->is_type( 'variable' ) && empty( $product->get_variation_attributes() ) ) { $issues[] = 'Variable product has no variation attributes.'; } if ( $product->is_type( 'variation' ) && in_array( '', $product->get_attributes(), true ) ) { $issues[] = 'Variation uses an “Any” attribute; test exact option resolution with the agent.'; } return $issues; } public function run( $page, $include_excluded = false ) { $page = max( 1, (int) $page ); if ( ! function_exists( 'wc_get_products' ) ) { return array( 'error' => 'Activate WooCommerce to audit products.' ); } $result = self::products( array( 'status' => 'publish', 'limit' => self::PAGE_SIZE, 'page' => $page, 'paginate' => true, 'orderby' => 'ID', 'order' => 'ASC' ), $include_excluded ); $rows = array(); $checked = 0; foreach ( $result->products as $product ) { ++$checked; $issues = self::issues_for( $product ); if ( $product->is_type( 'variable' ) ) { // Query a bounded page of variations; do not load an unbounded get_children() list. $children = self::products( array( 'type' => 'variation', 'parent' => $product->get_id(), 'status' => 'publish', 'limit' => self::VARIATION_LIMIT, 'paginate' => true, 'orderby' => 'ID', 'order' => 'ASC' ), $include_excluded ); if ( $children->total === 0 ) { $issues[] = $include_excluded ? 'No published variations.' : 'No included published variations.'; } if ( $children->total > self::VARIATION_LIMIT ) { $issues[] = 'Partial variation audit: only the first 100 published variations were checked.'; } foreach ( $children->products as $variation ) { ++$checked; $variation_issues = self::issues_for( $variation ); if ( $variation_issues ) { $rows[] = self::row( $variation, $variation_issues, $product->get_id() ); } } } if ( $issues ) { $rows[] = self::row( $product, $issues, $product->get_id() ); } } return array( 'page' => $page, 'pages' => (int) $result->max_num_pages, 'total' => (int) $result->total, 'checked' => $checked, 'rows' => $rows ); } /** Apply exclusions in the query so pagination and totals match the selected scope. */ private static function products( array $args, $include_excluded ) { if ( $include_excluded ) { return wc_get_products( $args ); } $args['wacc_agent_catalog'] = true; add_filter( 'woocommerce_product_data_store_cpt_get_products_query', array( Shopping\Catalog::class, 'query_search' ), 10, 2 ); try { return wc_get_products( $args ); } finally { remove_filter( 'woocommerce_product_data_store_cpt_get_products_query', array( Shopping\Catalog::class, 'query_search' ), 10 ); } } private static function row( $product, $issues, $edit_id ) { return array( 'id' => $product->get_id(), 'edit_id' => $edit_id, 'name' => $product->get_name(), 'issues' => $issues ); } }