Do products suddenly disappear from your Magento 2.4.9 storefront after saving a related product? The core issue ACP2E-4741 may be the reason. This can occur when related products, upsells or cross-sells are used in a store with a custom stock…
Getting Product Attributes Without Loading the Entire Product (Magento 2)
If you need only one product attribute value in Magento 2, you do not always need to load the entire product through a repository or collection. That can be wasteful in loops, cron jobs and imports. Using the Product Resource…
Finding products without product images using SQL (Magento 2)
Sometimes you need to find all products without assigned images in Magento 2, for example to verify that a product import or data migration is complete. The following SQL query returns products without product images:
|
1 2 3 4 5 6 7 8 9 |
SELECT p.entity_id, p.sku, p.type_id FROM catalog_product_entity AS p LEFT JOIN catalog_product_entity_media_gallery_value_to_entity AS mg ON mg.entity_id = p.entity_id WHERE mg.value_id IS NULL ORDER BY p.entity_id; |
Counting products per attribute set using SQL (Magento 2)
It can be useful to count how many products belong to each attribute set. The simplest approach in Magento 2 is the following SQL query:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
SELECT eas.attribute_set_id, eas.attribute_set_name, COUNT(cpe.entity_id) AS product_count FROM eav_attribute_set eas LEFT JOIN catalog_product_entity cpe ON cpe.attribute_set_id = eas.attribute_set_id WHERE eas.entity_type_id = ( SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product' ) GROUP BY eas.attribute_set_id, eas.attribute_set_name ORDER BY eas.attribute_set_name; |
The result lists the number of products assigned to each attribute set.
Checking whether a product is in stock by product ID (Magento 2)
Use the following Magento 2 snippet to check stock availability by product ID:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public function __construct( \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, ... ) { $this->stockRegistry = $stockRegistry; } $stockItem = $this->stockRegistry->getStockItem($productId); if ($stockItem->getIsInStock()) { echo "Product is in stock"; } else { echo "Out of stock"; } |
Et voilà!
Getting a product’s status (Magento 2)
A Magento 2 product normally has one of two statuses: enabled or disabled. Use the following code to check its status programmatically:
|
1 2 3 |
// 1 = aktiviert // 2 = deaktiviert $status = $product->getStatus() |
Fertig!
Getting a product attribute value for All Store Views (Magento 2)
Sometimes you need to retrieve the value of a Magento 2 product attribute for “All Store Views”, for example in a template. To do this, set the store ID to 0, load the product and the required product attribute, and…
Fixing Undefined array key 1234 in Eav.php on line 604 (Magento 2)
You may see the following message when opening a product in Magento 2 admin: Undefined array key 1234 in vendor/magento/module-catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php on line 604 The ID can vary, but the cause is the same. Rebuilding the indexes with the following command,…
Fixing Invalid input datetime format of value ’01.01.1970′ (Magento 2)
Do you get the message Invalid input datetime format of value ’01.01.1970′ (the date format may vary, for example ’1/01/1970′) when saving a product in Magento 2 admin? Often the special-price dates in the product’s Advanced Pricing section are incorrect.…
Mage_Catalog_Block_Product::getPriceHtml causes errors with PHP 7 (Magento 1)
The following Magento 1 code no longer works with PHP 7 or later:
|
1 |
echo Mage_Catalog_Block_Product::getPriceHtml($_product, true); |
With PHP 7, you need to create an instance of Mage_Catalog_Block_Product and call getPriceHtml on that instance.
|
1 2 |
$productBlock = new Mage_Catalog_Block_Product; echo $productBlock->getPriceHtml($_product, true); |