How to assign categories to a new product automatically? - magento

I am new to Magento and not familiar with it. My requirement is to assign a category to new products automatically. How could I do that?

If you have fixed category to assign when new product is saved then it can be done by observer event (like catalog_product_save_after).
When product is saved, in observer you can write code to assign that newly saved product to particular category.
Following is snippet to assign product to category.
$categories = $product->getCategoryIds();
$categories[] = $categoryId;
$product->setCategoryIds($categories);
$product->save();

Related

How to programmatically get option id's and values from configurable product in phtml?

I am modifying phtml code were I have access to
$this which happens to be Mage_Checkout_Block_Cart_Item_Renderer_Configurable class
and
$_item = $this->getItem();
$_product = $_item->getProduct();
where product is a configurable product in a cart.
Based on this I need to get a list of option id (swatches) and their values.
For example, to get specific size option id :
$product = $_item->getProduct();
$idStore = $_item->getOrder()->getStoreId();
$sizeSpecificAttributeCode = 'specific_size';
$sizeAttributeOptionId = $product->getResource()
->getAttributeRawValue($product->getId(), $sizeSpecificAttributeCode,$idStore);
from the same logic you can get the swatches or other options

Get product order custom attributes in save_order_save_after observer in Magento 2

I want to capture some product custom attributes for the item in the order in magento 2.
I tried with getAttribute() and getAttributeText(‘attribute_code’)
Please help
Use this code to get product attribute for product items in save_order_save_after observer in Magento 2.
$orderObj = $objectManager->create('Magento\Sales\Model\Order')->load($orderID);
$orderAllItems = $orderObj->getAllItems();
if ($orderAllItems) {
foreach ($orderAllItems as $item) {
$product_manufacturer = $item->getManufacturer();
}
}
You can get all attribute from $item.

Creating programatically a category when Flat mode is enabled in Magento

I want to create programatically a category in my data folder of my module. The Flat categories option is enabled.
When a I try to create a category like so:
$category
->setStoreId(0)
->setName('My category')
->setUrlKey('club-campaigns')
->setPath($rootCategory->getPath())
->setIsActive(1)
->setIsAnchor(1)
->setIncludeInMenu(1)
->addData($data)
->setCustomDesignApply(1)
->save();
I get an error which says that catalog_category_flat doesn't exists. Ok, so I know that flat categories info is kept in catalog_category_flat_store_storenumber table. I looked in the database and I have the following tables:
catalog_category_flat_store_1
catalog_category_flat_store_2
catalog_category_flat_store_3
catalog_category_flat_store_4
catalog_category_flat_store_5
catalog_category_flat_store_6
and I want to create a category for store 6. Good, now if I do like this:
$category
->setStoreId(6)
->setName('My category')
->setUrlKey('club-campaigns')
->setPath($rootCategory->getPath())
->setIsActive(1)
->setIsAnchor(1)
->setIncludeInMenu(1)
->addData($data)
->setCustomDesignApply(1)
->save();
the category is created without errors, and it sets the info in catalog_category_flat_store_6 but if I go to admin>Manage Categories and cant't see my category created.
I think that when I create a category I should set te store id of the admin(0) so I can see it in the admin panel but then I get the error above, and if I create with store 6 I don't see it in the admin. I'm really stuck.
How can I properly create my category programatically without problems?
Create category dynamically:
$category = Mage::getModel('catalog/category');
$category->setStoreId(Mage::app()->getStore()->getId());
$cat['name'] = "Custom Category Name here";
$cat['path'] = "1/2/30"; //parent relationship..
$cat['description'] = "categorie's description";
$cat['is_active'] = 1;
$cat['is_anchor'] = 0; //for layered navigation
$cat['page_layout'] = 'two_columns_left';
$cat['url_key'] = "custom-category"; //url to access this category
$cat['image'] = "custom-category.jpg";
$category->addData($cat);
$category->save();
Then reindex catalog_category_flat dynamically:
$process = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_category_flat');
$process->reindexEverything();

Magento: Get product price from another store?

I have multi-store Magento installation, and different product prices are set in different stores. I want to display on one page the actual product price from the current store, and the price from other store (I have its ID), but I'm not sure how to get that info?
The prices are set for each store view for each product, no tier-pricing or special-pricing was used.
If you know the storeId, set in setStoreId :
/**
* call the Magento catalog/product model
* set the current store ID
* load the product
*/
$product = Mage::getModel('catalog/product')
->setStoreId($storeId)
->load($key);
Display in a block :
echo $product->getName();
We can also use print_r to see the values :
print_r($product->getData());
The following code will show current store ID :
$storeId    = Mage::app()->getStore()->getId();
To get all product ID's with each store view :
$product    = Mage::getModel('catalog/product');
$products   = $product->getCollection()->addStoreFilter($storeId)->getData();
If you change the $storeId will show different product.

Magento: Filter Configurable Product by their options

I want to filter some configurable products by attributes that are used to create more instances of that product (size, color etc.). This means those attributes are not directly assigned to the configurable product, but their childs.
I already have a code that filters configurable products by some attributes, but these are all assigned to the main product, and the children inherit that: designer.
$attributes_designers = $this->getRequest()->getParam('designers');
$attributes_colors = $this->getRequest()->getParam('color');
$attributes_sizes = $this->getRequest()->getParam('size');
$currentCategory = Mage::getModel('catalog/layer')->getCurrentCategory();
$_productCollection = $currentCategory->getProductCollection();
if(count($attributes_designers)>0 and !in_array("ALL",$attributes_designers)) {
$_productCollection->addAttributeToFilter('designer',$attributes_designers);
}
if(count($attributes_colors)>0 and !in_array("ALL",$attributes_colors)) {
$_productCollection->addAttributeToFilter('color',$attributes_colors);
}
if(count($attributes_sizes)>0 and !in_array("ALL",$attributes_sizes)) {
$_productCollection->addAttributeToFilter('size_apparel_eu',$attributes_sizes);
}
if(isset($_GET['order'])) $_productCollection->setOrder($this->getRequest()->getParam('order'), $this->getRequest()->getParam('dir'));
$_productCollection->load();
here, the color, and size_apparel_eu, are not working, because those are not directly assigned to the product, but their children.
If I'm understanding you correctly here...You first need to get the child products with something like this:
$_product = $this->getProduct();
$_configurable_model = Mage::getModel('catalog/product_type_configurable');
$_child_products = array();
if ($_product->getTypeId() == 'configurable')
$_child_products = $_configurable_model->getUsedProducts(null, $_product);
Then you can use some sort of loop (foreach is handy)
foreach ($_child_products as $_child_product){
$_child_product->getRequest()->getParam('designers');
}
Or however you need to call those methods to get the child product information.
I hope this was helpful and relevent!

Resources