Magento create configurable product BUT without child products data - magento

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)");

Related

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

Is there a way to see which items are not categorized in magento?

Is there a way to see which items are not categorized in magneto on the backend? I know I can pull a report but I was wondering if there was anything easier?
You can use the free Enhanced Admin Grids extension, as the github version allows to add a categories column on the products grid, for which it is optionally possible to filter on products that are not assigned to any category.
Not natively in the Magento backend.
You could run a simple SQL query against your database to grab a list of skus that aren't assigned to a category.
Something like this:
SELECT sku FROM catalog_product_entity
WHERE entity_id
NOT IN (SELECT product_id FROM catalog_category_product)
Which will return a list of all skus not assigned to a category. From there you can save the results to a CSV using your SQL program.

Remove a Magento attribute from configurables and associated simple products

I have some configurable products that have many associated simple products, each with many custom options. The configurables use three attributes. My client has now decided that they want to remove one of the attributes. I have used the SQL method to remove one of the attributes, but this is now affecting the custom options when the remaining attributes are selected. The custom options no longer show up. I am assuming this because the attribute is still part of the associated simple products.
Is there a way to remove the attribute from the associated simple products as well, so that the custom options will show correctly when the remaining attributes are chosen?
The attribute, which you are trying to remove is part of each configurable super product, not associated products, as far as I know. Check 'catalog_product_super_attribute' table in db, remove all rows, which have 'attribute_id' set to id, which you want to remove.
Clean the cache, reindex and see, if it works.
To remove one super product attribute from all configurable products, you could execute this SQL query in the database:
DELETE FROM catalog_product_super_attribute WHERE attribute_id = <id>;
The table catalog_product_super_attribute links products to super product attributes.

Update All Product Attributes programmatically

How can I set the value of a given attribute to the same value for all products (efficiently)?
By efficient I mean in one transaction, not having to loop through the entire product collection.
In the past I've used Mage_Catalog_Model_Product_Action for bulk updates on products, and it runs pretty fast
Mage::getSingleton('catalog/product_action')
->updateAttributes($productIds, array('some_attribute' => 'some_value'), 0)
But it requires you specify which product ids you're updating, creating a huge WHERE entity_id IN(...) clause in the MySQL statement. Is there a way to do this for everything?
I had same problem before, when I added 11096 product(downloadable products) in my store then client told me to add new attributes in product so i create 1 attribute (Type is Yes/No) and set to attribute set. Now my problem is how can iIedit all product and set that attribute yes or not. If I don't set then value is null so I wrote few line code.
Please check this code may be it'll helpful to you.
$ProductId = Mage::getResourceModel('catalog/product_collection') -
addAttributeToFilter('type_id', Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE) -
getAllIds(); //Now create an array of attribute_code => values
$attributeData = array("my_attribute_code" =>"my_attribute_value");
//Set the store to affect. I used admin to change all default values
$storeId = 0;
//Now Update the attribute for the given products.
Mage::getSingleton('catalog/product_action') ->updateAttributes($ProductId, $attributeData, $storeId);
It was work for me.I hope it'll work for you
Amasty "Mass Product Actions" will allow you to edit product attributes in a variety of ways. This is what we use and it's going to be a life saver once we get the Configurable Products pricing tier working properly.
http://amasty.com/mass-product-actions.html

Magento, problem with catalog_product_flat

We have a website with 2 store views: FR and EN. For some products after import catalog_product_flat for EN store view is not refreshed. In EAV tables everything is fine. Data re-index should truncate this flat table and fill it with updated data. Somehow it doesn't work for some items.
Did anyone of you had a similar problem? I'd appreciate for any clues or advices on this topic.
EDIT
I have made further checks and I was wrong about EAV tables. It turns out that catalog_product_entity_varchar is consistent with catalog_product_flat. So flat table has the same data as EAV table but in the Admin Panel values are wrong. For EN store view they are the same as default values, only for some products (magic? ;)). On my local PC I didn't encounter such issue. This is only on our production environment. As far as I know we do not use any DB replication (which could be the issue here).
EAV database model is used by Magento for easy upgrade and development as this model gives more flexibility to play with data and attributes.
When flat catalog is enabled in Magento then all the above product attributes (id, name, price) are kept in one table named like catalog_product_flat. Then Magento fetches product data from the flat table rather than joining all the other smaller tables.
There are two types of Flat Catalog:
1) Flat Catalog Product
2) Flat Catalog Category
Flat Categories are recommended for any Magento installation for improved performance.
Flat Products is designed and recommended for catalogs that have over 1000 SKU’s.
Enable Flat Catalog Category:
Go to Admin Panel->System->Cache Management->Rebuild Flat Catalog Category
Go to Admin Panel->System->Configuration->Catalog->Frontend->Use Flat Catalog Category = Yes
Enable Flat Catalog Product:
Go to Admin Panel->System->Cache Management->Rebuild Flat Catalog Product
Go to Admin Panel->System->Configuration->Catalog->Frontend->Use Flat Catalog Product = Yes
Remember that, initially the selection list of
Admin Panel->System->Configuration->Catalog->Frontend->Use Flat Catalog Product
OR,
Admin Panel->System->Configuration->Catalog->Frontend->Use Flat Catalog Product
is non-editable. You have to rebuild Flat Catalog from Cache Management. Only then the selection list becomes editable.
Make sure that while importing of the Catalog Products, the required attributes of the products are provided with correct values in the import file. If this is not done properly, then Data re-index may not function correctly.
Also before re-indexing, it is always advisable & wise to clear the cache from the "Cache Management" & from the "cache" folder of your Magento installation directory.
Hope it helps.
I was wrong about what's wrong. And everything is OK with database. The problem was an order of attributes that are retrieved from DB.
In Mage_Eav_Model_Entity_Abstract we can find:
$selects = array();
foreach ($this->getAttributesByTable() as $table=>$attributes) {
$selects[] = $this->_getLoadAttributesSelect($object, $table);
}
if (!empty($selects)) {
$values = $this->_getReadAdapter()->fetchAll(implode(' UNION ', $selects));
foreach ($values as $valueRow) {
$this->_setAttribteValue($object, $valueRow);
}
}
Line implode(' UNION ', $selects) concatenates all select statements. But there is no ORDER BY, so data can be retrieved in random order. As a matter of fact, for some of the products it is like this. That select takes values of attributes for store view 0 (always) and selected store view (current one).
Array $values contains of arrays with attributes properties. Order does matter here, because if attribute (for example 'name') for store view e.g. 1 will be proceed before one for store view 0, then it will be overwritten.
Solution is to add ORDER BY clause to the $selects or sort $values array.

Resources