Last active
April 28, 2025 00:01
-
-
Save boykore/0a116bda1ad8e709b2554db2b58a83af to your computer and use it in GitHub Desktop.
Product category reindex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "Magento 2: Smart Reindex & Category Product Check π" | |
echo "----------------------------------------------------" | |
# Check if we are in the Magento root directory | |
if [ ! -f "bin/magento" ]; then | |
echo "β Error: This script must be run from the Magento project root." | |
exit 1 | |
fi | |
# Ask for Category ID | |
read -p "Enter the Category ID to check: " CATEGORY_ID | |
# Check indexer status | |
echo "" | |
echo "β Checking indexer statuses:" | |
php bin/magento indexer:status | |
# Reindex necessary indexes | |
echo "" | |
echo "π Reindexing catalog_category_product, catalog_category_product_index, catalogsearch_fulltext..." | |
php bin/magento indexer:reindex catalog_category_product | |
php bin/magento indexer:reindex catalog_product_category | |
php bin/magento indexer:reindex catalogsearch_fulltext | |
# Flush cache | |
echo "" | |
echo "β»οΈ Flushing Magento cache..." | |
php bin/magento cache:flush | |
# Connect to Database | |
echo "" | |
echo "π Checking products in Category ID = $CATEGORY_ID..." | |
read -p "Enter the database name: " DB_NAME | |
read -p "Enter the database user: " DB_USER | |
read -s -p "Enter the database password: " DB_PASS | |
echo "" | |
read -p "Enter the store_id (e.g., 1 for default): " STORE_ID | |
# Query to find products | |
QUERY="SELECT | |
cpe.sku AS 'SKU', | |
cpev.value AS 'Product Name', | |
CASE cpi.value | |
WHEN 1 THEN 'Not Visible Individually' | |
WHEN 2 THEN 'Catalog' | |
WHEN 3 THEN 'Search' | |
WHEN 4 THEN 'Catalog, Search' | |
ELSE 'Unknown' | |
END AS 'Visibility', | |
CASE cps.value | |
WHEN 1 THEN 'Enabled' | |
WHEN 2 THEN 'Disabled' | |
ELSE 'Unknown' | |
END AS 'Status', | |
CASE WHEN cisi.is_in_stock = 1 THEN 'In Stock' ELSE 'Out of Stock' END AS 'Stock Status' | |
FROM | |
catalog_category_product_index_store${STORE_ID} AS ccpi | |
JOIN catalog_product_entity AS cpe ON ccpi.product_id = cpe.entity_id | |
LEFT JOIN catalog_product_entity_varchar AS cpev ON cpe.entity_id = cpev.entity_id | |
AND cpev.attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'name' AND entity_type_id = 4) | |
LEFT JOIN catalog_product_entity_int AS cpi ON cpe.entity_id = cpi.entity_id | |
AND cpi.attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'visibility' AND entity_type_id = 4) | |
LEFT JOIN catalog_product_entity_int AS cps ON cpe.entity_id = cps.entity_id | |
AND cps.attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'status' AND entity_type_id = 4) | |
LEFT JOIN cataloginventory_stock_item AS cisi ON cpe.entity_id = cisi.product_id | |
WHERE | |
ccpi.category_id = ${CATEGORY_ID} | |
ORDER BY | |
cpe.sku;" | |
# Execute the query | |
mysql -u${DB_USER} -p${DB_PASS} ${DB_NAME} -e "${QUERY}" | |
echo "" | |
echo "π Done! Check the results above: SKU, Product Name, Visibility, Status, and Stock Status." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment