Magento - Updating the price of a configurable product option - magento

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 :)

Related

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

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.

Magento create configurable product BUT without child products data

I checked some existing questions, but none of them explains how to create configurable products without child products data. I don't need to link products, I just need to create configurable product for later use.
So I tried that:
$_prod->setTypeId('configurable');
$_prod->setConfigurableAttributes('color');
But this is not working. When I click in admin on product, the product is created, but I need to choose first configurable attributes. So setConfigurableAttributes didn't make any action, but it also didn't invoke any error. What is the proper way for that?
I ended up with the below raw query:
$colorAttrId = 92;
$tableName = Mage::getSingleton('core/resource')->getTableName('catalog_product_super_attribute');
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("INSERT IGNORE INTO $tableName (`product_id`, `attribute_id`) VALUES ($prodId,$colorAttrId)");

product show in stock in admin order when it is in out of stock Magento

Hi i am trapped in a very strange situation, my client has enable the customers to order the products which are out of stock , the problem is when the customer place the order it shows the product in stock in order . I need to show the product status out of stock in the order so he come to know about the order item status .
Below is the settings i have in catalog-> inventory in admin
please suggest me where i am doing mistake or how can i do this
thanks
You need to modify your product.phtml inside app/design/frontend/{yourpackage}/{yourtheme}/template/catalog/product/view.phtml
Add following in your code scope for testing and checking if its not already showing out of stock .
$stockItem = Mage::getModel('cataloginventory/stock_item');
$resource = $stockItem->loadByProduct($_product->getId());
if(round($resource->getQty(),0) < 1)
{
echo "product is out of stock";
}
The above code actually tests if there is any quantity available in stock and is greater than 1. Also make a note that this will not work for the products setup as not to be managed by stock under inventory tab in product add/modify case.
Thanks

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.

in magento how can i Get related product in the product listing page

I am doing a magento project where i have to bring related product and upsell product in the product listing page which is product/list.phtml
i can get all the product details from
foreach ($_productCollection as $_product):
$_product->
but i can't get any related product and upsell product using $_product->
please help me please
You can do the following (inside the foreach loop)
$related_product_collection = $_product->getRelatedProductCollection();
$related_product_collection->AddStoreFilter();
Regards,
Kenny
PS: If you want to know which methods are available, you can always run the
var_dump(get_class_methods($_product)); die;
//or
Mage::log(print_r(get_class_methods($_product),true));
First one will print all available methods on the screen while second one will output it in the system.log located in the /var/log/system.log (make sure that in configuration->developer you have logging enabled of course)
You can pass product id for which you require related product.
E.g
You need related product for a particular product (Say $_product)
You can get related product ids by
$_product->getRelatedProductIds()
You can see array of ids by :
print_r($_product->getRelatedProductIds());
I hope this will help you.
Regards,
Kamesh J
On list page you get all product details with id.
using this code $_product=Mage::getModel('catalog/product')->load(product_id);
you get product for that id.
After use this code to get details of related product
$relatedProductsId=$_product->getRelatedProductIds();
$relatedProducts=array();
$i=0;
foreach($relatedProductsId as $relatedProductId)
{
$relatedProducts[$i] = Mage::getModel('catalog/product')->load($relatedProductId)->getName();
$i++;
}
and you get all related product

Resources