I have a lot of products which need to be assigned to new categories. The products exist, the categories exist, but for some reason the import is not doing something.
I am using a dataflow profile using the following settings:
(source: i.imm.io)
The first two lines of my CSV are:
sku,store,category_ids
TT010,default,Face/Acne
What values should the first line have in order to assign the products to another category?
In the end I had to override the core/mage/catalog/model/convert/adapter/product.php file.
I added the following code after
if (isset($importData['category_ids'])) {
$product->setCategoryIds($importData['category_ids']);
}
//search for a category by name.
if(isset($importData['category']))
{
$cat_ids = array();
$cats = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->load();
foreach($cats as $cat)
{
if($cat->getName() == end(explode('/',$importData['category'])))
{
$cat_ids[] = $cat->getId();
}
}
$product->setCategoryIds($cat_ids);
}
This is for magento 1.7
Related
I have a custom module with change the default CATALOG/RESOURCE/PRODUCT/COMPARE/ITEM/collection.php, but the line below:
->order('ai.sort_order ASC');
Change the order for position with attributes position, but a few of is (about 10 attributes) is just a text (not Dropdown, Select or Price) and cannot allow to orders correctly position iquals a product page order. (i need to leave the page to compare products in the same order from the product page of the attribute list)
how i can make this?
To create sort by position write below code after getting product collection in app/design/frontend/<theme_name>/default/template/catalog/product/list.phtml
$_productCollection = new Varien_Data_Collection();
$sortedCollection = array();
foreach ($_defaultProductCollection as $key => $_product) {
if(!isset($sortedCollection[$positions[$_product->getId()]])){
$sortedCollection[$positions[$_product->getId()]] = array();
}
$sortedCollection[$positions[$_product->getId()]][] = $_product;
}
ksort($sortedCollection);
foreach ($sortedCollection as $_products) {
foreach ($_products as $_product) {
$_productCollection->addItem($_product);
}
}
Hope it will work for you.
I am implementing the Magento catalog product view page, and I need the name of subcategory the product is added to.
The code to display the category is like: $_product->category->name
but I am unable to get the sub category name.
to get the subcategory of a category, use the below code
$your_category_id = '2334'; // category_id whose subcategory you want to fetch
$subcats = Mage::getModel('catalog/category')->getCategories($your_category_id);
foreach ($subcats as $sub) {
echo $sub->getName();
}
Well I got the sub categories the following way:
<?php
if (Mage::registry('current_product')) {
if ($_product) {
$categoryIds = $_product->getCategoryIds();
$cat_id = $categoryIds[0]; ----> pass the level of sub catgeory to the array index
$category = Mage::getModel('catalog/category')->load($cat_id);
$cat_name = $category->getName();
}
}
?>
Hi i am working on extension for categories.i am facing problem regarding categories and products.I have more than 2 categories with same products.After creating new category I want to assign those products that exist under category1, (category 1 and category 2 have same products).It will remove products from category2 due to my code.My code is :
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $product) {
$categories_pd = $product->getCategoryIds();
$product->setCategoryIds(array($new_cateid , $category1_id));
$product->save();
}
I know that due to setcatgoryIds function its removing other entries.Can anyone know right function or logic to fix this issue.
With $categories_pd = $product->getCategoryIds(); you get all existing entries as an array.
Just add new categories here
$categories_pd[]=$newid1;$categories_pd[]=$newid2...
Then in $categories_pd you have all needed categories so just set it
$product->setCategoryIds($categories_pd);
$product->save();
Lets say I load my product object:
$product = Mage::getModel('catalog/product')->load($productId);
Is there a function or some way to extract the bundled ids related to this product?
e.g.
$product->getBundledProductIDs()
The following should work:
$product->getTypeInstance(true)->getChildrenIds($product->getId(), false)
The result is a multi-dimensional array with the top level being options and the children of options being products.
Also, you can change the false to a true and it will only return required options of the bundle.
Try this-
$collection = $product->getTypeInstance(true)
->getSelectionsCollection(
$product->getTypeInstance(true)
->getOptionsIds($product), $product);
foreach ($collection as $item) {
# $item->product_id has the product id.
}
How do I get the store to list all products, independent of category?
This should work in all Magento versions:
$products = Mage::getModel('catalog/product')->getCollection();
//Magento does not load all attributes by default
//Add as many as you like
$products->addAttributeToSelect('name');
foreach($products as $product) {
//do something
}