How to determine if a simple product is part of a configurable product? - magento

Im currently writing a export tool for Mage1 in order to export products from Mage1 to Mage2.
I thereby want to determine if a product in the collection is part of a configurable product.
My current collection is set up this way:
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->setPageSize(500)
->setCurPage(1)
->addAttributeToSort('sku', 'ASC')
->addAttributeToFilter('type_id', array('eq' => 'simple'))
->addAttributeToSelect('*')
->load();
First, I request only simple products right now. If I delete this line, I get every products in the shop in the collection, but In the code every product is handled like a simple product currently.
Now, following is happening:
I request the script and get my information for all simple products. In the store I use to test there are configurable products AND bundle products. I know want to know how I could determine if a product is part of a configurable or bundle article and how I could assign those "simple" products to the respective configurable products?
I generate a CSV which I can upload in Mage2 successfully, but as I already said, it only imports simple products (as desired so far).
I googled a little (https://www.google.de/search?q=get+assigned+simple+products+for+a+configurable+product&ie=utf-8&oe=utf-8&gws_rd=cr&ei=b3i8VuD6BIevswGWtbGACw) but I'm still clueless. :/ Hopefully someone here can help me with this matter.
Also, I checked this thread: Checking if a Magento product is a child of a configurable product - but I don't get what to do with the respective name. Maybe this is already the right guess?
Thanks, Max

Yes there is a way of doing so. Have a look at the model class Mage_Catalog_Model_Product_Type_Configurable, there is a function called getParentIdsByChild.
Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($productId);
It will return an empty array if no parents id's have been found.

Related

Change Magento collection to show only grouped products

I'm trying to modify Magento (1.7.0.2) collection to show only grouped products in the product list. I've tried to do this with addAttributeToSelect or addAttributeToFilter like this
$_productCollection=$this ->getLoadedProductCollection()
->addAttributeToSelect('*')->addAttributeToFilter('type_id','grouped');
or
$_productCollection=$this ->getLoadedProductCollection()
->addAttributeToFilter('type_id', array('eq' => 'grouped'));
but it's not working now with this version of Magento.
Anybody knows how can I get it working?
Filters not being applied often happens when the production is already loaded.
Try doing a $_productCollection->clear() before you iterate across the collection. Preferably though, find a place to update the collection before it is loaded.

Magento - Updating the price of a configurable product option

Magento version 1.6.1.0. I'm attempting to write some code that will dynamically update the price of a configurable product's option. My ultimate aim is to write a module that will update the price of the configurable product's options based on the price of the children products of the configurable. The attached code pulls out all the configurable products from a catalogue and displays them along with the product options & prices and the children products' name & price. I plan to work out the difference in price between the configurable and each child product and update the price appropriate option to match. So far I have been unable to work out how to update the price of a product option.
Short version: I just need a way to update the price of a configurable product's option. Do you know how to do this?
<?php
require_once './app/Mage.php';
Mage::app();
Mage::app()->setCurrentStore(1);
// load in configurable products
$productConfig = Mage::getResourceModel('catalog/product_collection')->addAttributeToFilter('type_id', 'configurable');
foreach ($productConfig as $_product)
{
// load the configurable product
$_product = Mage::getModel('catalog/product')->load($_product->getId());
echo 'Product Name';
var_dump ($_product->getName());
var_dump ($_product->getPrice());
// Collect options applicable to the configurable product
$productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
$attributeOptions = array();
foreach ($productAttributeOptions as $productAttribute)
{
var_dump($productAttribute['label']);
foreach ($productAttribute['values'] as $attribute)
{
var_dump($attribute);
}
}
// loop through the child products
echo 'Child products';
$col = Mage::getModel('catalog/product_type_configurable')->setProduct($_product)->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
foreach($col as $simple_product)
{
var_dump($simple_product->getName());
var_dump($simple_product->getPrice());
}
}
echo '~fin';
?>
Thank you!
Well, I solved this one after quite a bit of head scratching. I don't know if I did it "right" or not.
I solved it by manually running some SQL to adjust the pricing in the catalog_product_super_attribute_pricing table. If you're going to do this, you'll need a product_super_attribute_id, which you can get from the catalog_product_super_attribute table if you have a product ID.
One caveat: If a price does not exist in the backend for an option (if the option exists, but adds £0 to the product price when selected), there will not be a record for the option in the catalog_product_super_attribute_pricing table, you'll need an insert query instead of an update in that case.
I had the same issue and I started doing the same thing: write a module to update configurable product's options.
I recently published it here: Magento Configurable Auto Pricing
I tested it just with EE 1.12 but it should work also with CE and I'd be glad if anyone wants to try it and give me any feedback or even better write his own fixes and commit them :)

Displaying single product through list.phtml edit

So what i'm trying to achieve is that the product with the attribute 'promotion' set to 'yes' is displayed on the frontpage of the website. This is working, but the .phtml file i'am using with this is the regular list.phtml. This is currently showing all the items I have set to promotion but I only want to show 1.
So in short: How do I edit the list.phtml to only show 1 product instead of everything?
Change how you are pulling your collection. Clone/Rename your list.phtml, for example promotion.phtml. Then change this line, from this:
$_productCollection=$this->getLoadedProductCollection();
To this:
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('promotion', 1)
->addAttributeToSort('updated_at', 'DESC')
->clear()->setPageSize(1)->load();
And it should load only one item with promotion set to yes. Make sure you set the new template in either your CMS Page Content or XML, depending on which method this is added.
Explanation
Mage::getModel('catalog/product')->getCollection(): Gets the Product Collection. You can get other collections by changing the model, such as "catalog/category" and "cms/page".
->addAttributeToSelect('*'): Adds All Product Columns. This can be exchanged for things like ('name', 'url'). I'm assuming it's faster than loading all but I haven't benchmarked it. Since you're using the full template it's probably best to leave this set to all.
->addFieldToFilter('promotion', 1): Filters out products by attributes. Here we have the products filtered for all those with the 'promotion' attribute set to 1(yes/true). Products use this one, while categories use ->addAttributeToFilter() oddly enough. Definitely give Alan Storm's Collection Explanation(link below) a read through to know what all you can do with this one. You can add multiple filters to your collection, either by adding another ->addFieldToFilter(), or storing your filters in nested arrays.
->addAttributeToSort('updated_at', 'DESC'): Sorts the product collection by specific attribute and direction. Here I have the "updated_at" date set to descending order, "ASC" is ascending. You can add multiple sorting attributes, pay attention to the order you add them in, of course.
->clear()->setPageSize(1)->load(): These three are needed to make adjustments to how much the collection pulls. ->clear() must be called before it will allow changing how many products are pulled. The ->setPageSize() bit is where you specify how many products you would like to return, and ->load() of course loads the collection. Note that if you do not limit the size of the collection returned, you do not need this entire line, the products will iterate without having to call ->load().
Resources
Alan Storm said it best, give this a read and you should be a pro at manipulating collections: http://alanstorm.com/magento_collections

Magento Configurable products don't go out of stock

I have the usual configurable-simple products store. Ideally I would like that a configurable products should automatically go out of stock when all of its associated products go out of stock. How ever this does not seem to happen. Am I missing something in the configuration or is this not supported?
I am using magento community 1.6.1.
Probably it's not supported. I have similar problem and I use this code to eliminate out of stock products:
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'configurable'));
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_productCollection);
sulabh
First check your inventory configuration for every product ("manage stock") should be set to true.
Then run stock reindex. It will check if every child is out of stock -> will set configurable product out of stock.

What is the conceptual difference between a Product and a Quote Item

Involved classes : Mage_Sales_Model_Quote_Item and Mage_Catalog_Model_Product.
I get both of them as a result of listening an event ( on cart add ). I am trying to update the quantity information for a product from an external source.
So far I based my code only on the product information and I am not sure if this is correct.
What is the purpose of Quote Items?
How about an bundled of configurable product? Do you have any recommendation on how to get the individual items from a bundle product?
Thanks
I want to thank both responders so far for their effort but their responses are pretty far from my question. I'll try to respond myself based on the things that I've learned.
A quote is a concept related to the order, only that is previous to that in terms of work flow in Magento. A real world concept is something like a preorder, like a postIt on which you place your asked dishes in a restaurant without being an order or a bill.
I was monitoring an event (checkout_cart_product_add_after) that is sending me the $product and the $orderItem. I understand now that is sending both in order to get information about the product and information about billing and the representation of that product in the future order.
In the case of the grouped products for example where the $product is Tshirt with various associated sizes, the $product will contain the SKU of the main grouped product and the $orderItem will contain the instance of the Tshirt that was selected ( medium size SKU ).
FYI: So in order to update the information of a product at cart update you have is better to get the product info from the $item if is a complex type ( bundle, configurable or grouped )
Magento drops quote items into the cart, specifically. Those quote items are retrieved by using $product->prepareForCart. These items also include different information, such as quantity and configurable product options (on a quote item).
From a backend perspective, data for products are stored in: catalog_product_entity_*, whereas quote items are stored in sales_flat_quote_item (at least in Enterprise. someone else might want to verify this on community).
EDIT: Attaching some code that we wrote to import product inventories a while back.
$product = Mage::getModel("catalog/product")->load($productId);
$product->seStockData(array(
"qty" => (int)$yourQuantity,
"is_in_stock" => ((int)$isTheProductInStock),
"manage_stock" => $manageStock,
"is_qty_decimal" => $isQtyDecimal,
"use_config_manage_stock" => $useConfigManageStock,
));
Mage::getModel('catalog/product_api')->update($sku,$product->getData());
For your purposes you may just need to call $product->save();, but I'm including the whole snippet as written because it works.
Hope that helps. Thanks,
Joe
My first answer would be that a product and a quote for a product are two separate entities and therefore should not be modelled in a unified entity.
An example of why would be from the company I work for, and why we model these things separately:
In our e-procurement system you might have a "contract" between a specific buyer and a seller. The "quote" item models this when it comes to invoice entries. If there isn't a contract use the normal product price to create a Quote Item else adjust the price using the "contract" between supplier and buyer.

Resources