Get all site product ID in magento - magento

Now, i want to get all product ID of my site. IS there a simple way to get all product ID in my site of magento,

To save resources it is better to use:
Mage::getModel('catalog/product')->getCollection()->getAllIds();

foreach(Mage::getModel('catalog/product')->getCollection() as $product)
{
var_dump($product->getId());
}

Or if you have access to command line, run mysql -u your_username -p magento_database and then you can run this query:
SELECT entity_id, sku FROM catalog_product_entity; don't forget that you might need something like LIMIT 0,30 or WHERE sku LIKE '%shirt%'. You can get to this via the command line or from phpmyadmin (if available). phpmyadmin has a query window in it somewhere.
I normally do sneaky things in Magento by creating php scripts in the var/export/ folder. Then you do not need to worry about how to interact with the Magento/Zend/MVC framework.

little variation If you All Product id from particular categories then:
$category_ids = array(11,16); // your category ids
$productIds = array();
foreach($category_ids as $category_id){
$category = Mage::getModel('catalog/category')->load($category_id);
$result = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($category)->getAllIds();
$productIds = array_merge($productIds,$result);
}
var_dump($productIds);

Related

Magento cart sort by category_id

I am trying to sort out items in cart by categroy_id:
$cartItems = Mage::getModel("checkout/cart")->getItems()
->addFieldToSelect('name')
->addFieldToSelect('category_id')
->addAttributeToSort('category_id', 'ASC');
echo $cartItems->getSelect(); //debug info
foreach($cartItems as $item) {
echo $this->getItemHtml($item);
}
Magento complaints that method addAttributeToSort does not exists, had a look and of course it isn't defined.
What is the proper way of doing this query?
Otherwise,
you can try the following code:
$collection = Mage::getModel('checkout/cart')->getItems()
->addFieldToSelect('name')
->addFieldToSelect('category_id')
->addOrder('category_id', 'asc');
addAttributeToSort() does work on certain collections, but not the checkout/cart Model.
I hope this helps!
When you run Mage::getModel("checkout/cart")->getItems() you get a collection of quote items, not products. So there is no category_id on the table sales_flat_quote_item that you can access.
If you want access to the product you have to loop your collection of quote items and run ->getProduct() on each item. Then you could call functions on the product, like ->getCategoryIds() to get a list of category ids that has the product assigned to it.

export the products having no images in magento

I have imported more than 55k products in magento. And i found that some of the products don't have images imported.
Can you give me a way how can i export the products having no images ?
I have tried to export the product having image value as 'no_selection' but, it gives me only 2-3 items. Then i tried to cross verify those entity_id which have no images into 'catalog_product_entity_varchar' table. And i surprised by not finding when some entity_id presents in 'catalog_product_entity' table but not in 'catalog_product_entity_varchar' table.
Can anyone give me a way to solve this problem.
Any help will really be appreciated.
Regards
Does this query help?
SELECT t0.sku
FROM catalog_product_entity AS t0
WHERE NOT EXISTS(
SELECT NULL
FROM catalog_product_entity_media_gallery AS t1
WHERE (t1.entity_id = t0.entity_id)
)
If you only need SKUs, one method I have used in the past is via a Magento shell script. This example will output only SKU's (one per line) for only those products which have no value for the image attribute.
<?php
error_reporting(E_ALL);
error_reporting(-1);
require_once 'app/Mage.php';
Mage::app();
$collection = Mage::getModel('catalog/product')->getCollection();
foreach($collection as $product){
$productId = $product->getId();
$prod = Mage::getModel('catalog/product')->load($productId);
$hasImage = Mage::helper('catalog/image')->init($prod, 'image');
if (!$hasImage){
echo $prod->getSku() . "\n";
}
}
?>
I put this into "list-noimages.php, and into my Magento root folder. Only use this in development environment, or very limited on production. Run it via shell as so:
php list-noimages.php
For that many products you can output to a file:
php list-noimages.php >> skus.txt
Otherwise visit the file via your web browser and save the output.

How to disable products that are not assigned to a category?

Could someone assist with this issue I am having please... I need to disable all products that are not assigned to a category in Magento but the problem is there are some 10,000+ products that I will need to sort through.
I would like to ask what the best approach would be so I could at least begin to solve the problem.
Would it be possible to set all products to disabled if they are not assigned to a category using an Observer? Should I echo the list of unassigned products in a loop then set the status to disabled...
I'm not sure how to go about this one...
Best way to do this is using Magento collections.
Create a new PHP file, include Mage.php, initialize the application and make your changes.
It might take a while depending on product count.
I think you want to do something like that:
require_once('../app/Mage.php');
Mage::init();
$product = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
$store_id = 1;
foreach ($product as $prod)
{
if ($prod->getCategoryIds() == NULL)
{
Mage::getModel('catalog/product_status')
->updateProductStatus($prod->getId(), $store_id, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
}
}

Magento : get related products from both product ID and current category ID

I totally begin with Magento during my training period. I've been working on a project for two weeks now and sometimes I can't figure out how to proceed.
I'm gonna try to represent my categories tree :
- accessories
* visors
* communication systems
* other
- helmets
* a lot of subcategories and subcategories...
My actual problem is : I'm in one of the accessories subcategory (for example visors). I added a form with a select allowing to choose a helmet model. When the select is submitted, I'd like to display the list of visors related to the chosen helmet model (that is actually a virtual product).
I can get the current category ID (in this case, visors) and the virtual product ID (so the helmet model). But I can't figure out how to display related products by both product ID and category ID.
I tried stuff like this :
$relatedProducts = Mage::getModel('catalog/product_link')
->getCollection()
->addCategoryFilter($myCurrentCat)
->addFieldToFilter('product_id',$myVirtualProductId)
->addFieldToFilter('link_type_id','1');
But it seems not to work.
Any help is welcome. Thanks.
EDIT : 10 days after I asked this question, I still don't know how to solve my problem. If anyone could help, even a little, just a clue...
I cannot test this at the moment, but you can try to do something like this:
$collection = Mage::getSingleton('catalog/product_link')
->useRelatedLinks()
->getProductCollection()
->setIsStrongMode();
$product = Mage::getModel('catalog/product')->load($productId);
$collection->setProduct($product);
$collection->addCategoryFilter($category); //I'm not sure if this will work correctly
I'm gonna test it, when I will get some more time.
Actually I just solved my problem by myself.
Here is my solution if it could help anyone in the future :
$mainProduct = Mage::getModel('catalog/category')->load($myCurrentCat->getId());
$mainProduct = $mainProduct->getProductCollection();
$mainProduct = $mainProduct->addAttributeToFilter('product_id', $myVirtualProductId);
foreach ($mainProduct as $product) {
$related = $product->getRelatedProductIds();
if ($this->array_contains($related, $myVirtualProductId)) {
//TODO
}
}

Magento get all products

I am trying to get the entire magento product collection, without any filters or restrictions, but I fail to get all products.
I've tried various methods already, but they all give me a very limited selection of products. Let's say the store contains 5000 products, but it only shows 500. When I check the catalog -> products is does show me the entire list.
Mage::getModel('catalog/product')->getCollection();
Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*');
Mage::getModel("catalog/product")->getResourceCollection()->load();
All of them return the same amount (500), while I expect it to give me 5000 products. I would prefer not to use Zend or PHP and just stick to the Magento way to get them.
Does anyone know how to really get ALL products or can point me in the right direction why this isn't working?
The select-string that is returned is:
SELECT 1 AS `status`, `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id` FROM `catalog_product_flat_4` AS `e`
//to overwrite limit but you need first to increase your memory limit
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*') // select all attributes
->setPageSize(5000) // limit number of results returned
->setCurPage(1); // set the offset (useful for pagination)
// we iterate through the list of products to get attribute values
foreach ($collection as $product) {
echo $product->getName(); //get name
echo (float) $product->getPrice(); //get price as cast to float
echo $product->getDescription(); //get description
echo $product->getShortDescription(); //get short description
echo $product->getTypeId(); //get product type
echo $product->getStatus(); //get product status
// getCategoryIds(); returns an array of category IDs associated with the product
foreach ($product->getCategoryIds() as $category_id) {
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getName();
echo $category->getParentCategory()->getName(); // get parent of category
}
//gets the image url of the product
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).
'catalog/product'.$product->getImage();
echo $product->getSpecialPrice();
echo $product->getProductUrl(); //gets the product url
echo '<br />';
}
And something like this:
$products = Mage::getModel('catalog/product')->getCollection();
foreach($products as $prod) {
$product = Mage::getModel('catalog/product')->load($prod->getId());
}
With this method I get more than 500 but all my product...
Several possibilities here:
1. Some inner limitation, like 500 at all.
2. Some paging limitation. Products per page(in db abstract)
3. Some lazyload limitation.
Perhaps, there is some over problem, but I think this is some inner limit.
Turn off your flat_catalog_product in admin > system > configuration > catalog > catalog. After this you will get a full product collection. Even though this is a workaround, it helped me to achieve what I needed to achieve.
You might be using Flat Catalog Product Structure . This create separate table for each store view.
Use Code below to print sql query . you will see the collection is coming from flat table like
catalog_product_flat_38
echo Mage::getModel('catalog/product')->getCollection()->getSelect();
try using
Mage::app()->setCurrentStore('0');
// same as
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
this hepled me

Resources