Magento - Programmatically added products don't show up - magento

I am trying to import products using a custom made import profile. Here I set some values on the product and then save it, like this:
$product = Mage::getModel('catalog/product');
$data = array();
// Inbetween I fill $data with some values
$product->addData($data);
$product->save();
However, the products do show up in the admin, but do not show up on the frontend (I did add them to categories).
The odd thing is, they do appear on the frontend once I open them in the admin and save them again (without modifying anything). Am I forgetting something, or some value that is somehow set by that action?
I tried to rebuild all indices in the backend and to clear the cache, but this does not work, contrary to Magento API: Rebuild Indexes after adding new products.

I had the same problem and solved it by adding the Website ID to the product:
$newProduct ->setWebsiteIds(array(1))
Keep in mind that the parameter is an array, so you can add multiple website td's

Some guesses, since this comes up from time to time. See also here.

Related

Magento quote items modify sku

I need to change SKU on quoteitems and I do it like this:
foreach($items as $item){
$item->setSku($newSku);
$item->save();
}
But magento always resets SKUs into their original values. Is there some easy way to change it, like superMode on prices? Or if not, then where can I find methods to rewrite, so that they ignore my custom SKUs?
Tried looking for it, but even if I remove cart init and save from index action, they still reset somewhere.
I need it to work in Magento 1.7-1.8
Ok, so I didnt feel like waiting and found it.
You can find it in Mage_Sales_Model_Quote_Item and there in function setProduct. This function is run every time the cart is shown and it resets the data from Product to Item.
Not sure if now my data will get through whole checkout process, will update the answer if not.
EDIT
This has solved only cart, will need to find also function for order. Will update when I get it.
EDIT
In the end, I don't advise you to do this. But if you need it then, the way to do it is to create observe on event sales_order_save_after and modify sku of order items. This seems to work everywhere, only in email after checkout it doesn't work, there you have to modify email. Also I use this only on configurable products.
I do the same thing on my project using sales_quote_collect_totals_after
Just change quote item SKU as you want using this event.

Magento - category products by attribute

When I create categories in Magento, I currently add all the products manually.
Problem is that we are getting more and more products and that some of our products change quite often.
So is it possible to dynamically change the category products depending on the product attributes?
i.e. to create categories that each contain all the products with a specific attribute value.
For example the category "blue", dynamically containing all the products where the attribute "color" is set to "blue".
Thanks.
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('color', 'blue');
$_category = Mage::getModel('catalog/category')->loadByAttribute('name', 'blue');
foreach ($collection as $_item){
$_item->setCategoryIds(array($_category->getId()));
$_item->save();
}
Its just a stupid dummy you may need another foreach loop for all colors....but it shows what needs to be done...
Why are you doing data entry Manually?.
Magento has an inbuilt stable product import export system.Did you try this?
If this import export system does not solve your purpose then you will have to write a script and run it via cron once every day.
See www.proxiblue.com.au, there is a module that does exactly this.
Products can Be Assigned to categories, using any attribute combination.
Disclaimer: this is my site and module.

Magento CE 1.7.0.2 – Imported products do not show up in frontend

I've been battling with this issue for the last week now, and am really hoping someone here might be able to help me with it.
After repeatedly importing products from a CSV generated by a different (and slightly older) Magento store, using both the Import and the Dataflow/Import All Products tools, I can get them to show up in the backend, seemingly with all the data intact and with images and everything, but they simply will not show up in the frontend.
It seems that the only way to make them show up in the frontend is to save them from the admin product page, which I'd rather not make the client do on 1922 products. They do not show up if I add them to categories from the category manager, only (as far as I know) if I save them from the product manager page.
Also note that all products
are enabled
have stock quantity > 0, weight > 0 and price > 0
have stock availability = In Stock
have visibility = "Catalog, search"
have the correct website assigned to them
have the right tax class associated with them
In addition, the cache has been entirely disabled and I've re-indexed everything at least two dozen times. I've also tried running the following snippet of PHP:
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*');
foreach ($products as $product) {
if ($product->isSaleable())
Mage::log("Sellable product: {$product->getId()}");
else
Mage::log("Unsellable product: {$product->getId()}");
}
and the log file subsequently contains nothing but "Sellable product: ...". I've also used the same product loop to set the website ID of the product and then save it, which also makes no difference. I've tried outputting the attribute values of various invisible products (using $product->getData()) both before and after saving through the product manager, but I cannot locate any differences whatsoever. I have not examined every MySQL table before and after saving, and frankly, I'd rather not have to do that.
I'm at wits' end. I don't know what else to try. I could re-import the product list for the umpteenth time, but I doubt it'll make a difference. I could try mass-updating the products again (using the product manager's "Actions" dropdown set to "Update Attributes"), but it's probably not going to help this time either. Does anyone have any ideas whatsoever?
My apologies if this is not the correct venue for this kind of question; feel free to point me in the right direction if that's the case.
UPDATE
Turns out that the products did not actually have the right website set, even though the admin panel clearly claimed so for all the products I looked at (none of which showed up in the frontend).
Solution: Manage Products > Select All, then select the action Update Attributes and add the products to the correct website(s). Don't forget to rebuild the indexes.
I had the same problem, so thank you for this answer you provided in your post:
UPDATE
Turns out that the products did not actually have the right website set, even though the admin panel clearly claimed so for all the products I looked at (none of which showed up in the frontend).
Solution: Manage Products > Select All, then select the action Update Attributes and add the products to the correct website(s). Don't forget to rebuild the indexes.
Just to add to this - I also had the same problem, however mine turned out to be the is_in_stock was either 0 or set to a qty value. Needs to be 0 or 1.
Just incase anyone has that issue.
Do you have gd php extension?
Install it (in CentOS):
yum install gd gd-devel php-gd
After execute this
service httpd restart
Best Regards

After importing configurable product in magento 'has_options' is zero

I have successfully imported a configurable product (and it's related products). When I open the configurable product in backend I see the related products in the last tab. But they are not displayed in frontend.
This is because the fields has_options and required_options are zero. The product with entity_id 2 has been created manually in backend and works perfect.
When I open the imported product in backend, change nothing and save it - the both values are changing to 1. What is missing in the import to set these fields directly to 1?
I tried this:
$configurableProduct->setHasOptions(TRUE);
But this has no effect.
After several hours trialing different solutions I resorted to following the save process of a configurable within the admin area, this pointed me towards the below method;
Mage_Catalog_Model_Product_Type_Configurable::save
I then discovered, when saving the configurable programmatically getConfigurableAttributesData and getConfigurableProductsData were empty.
This resulted in me creating a custom shell script where I loaded a collection of configurable products, looped and re-saved the data;
foreach($_collectionConfigurables as $_configurableProduct){
// flag to be saved
$_configurableProduct->setCanSaveConfigurableAttributes(1);
// get the configurable attributes
$_configurableAttributes = $_configurableProduct->getTypeInstance()->getConfigurableAttributesAsArray();
$_configurableProduct->setConfigurableAttributesData($_configurableAttributes);
// Build a collection of configurable children
$_collectionChildren = $_configurableProduct->getTypeInstance()->getUsedProducts();
// loop the children and save configurable attributes into an array
$arr = array();
foreach($_collectionChildren as $_childProduct){
foreach($_configurableAttributes as $_attr){
$arr[$_childProduct->getId()][] = array(
'attribute_id' => $_attr['attribute_id'],
'label' => $_childProduct->getAttributeText($_attr['attribute_code']),
'value_index' => $_childProduct->getData($_attr['attribute_code'])
);
}
}
$_configurableProduct->setConfigurableProductsData($arr);
$_configurableProduct->save();
echo "{$_configurableProduct->getId()}\n";
}
Please note this has been tried and tested on CE 1.9.2.1
I used Magmi now... it seems not to be so easy to import products on my own.

How to change the product display order in magento

How can I change the product display order in the front end (grid or list) by setting some preferences from back-end? I guess it should be other than best value and name from the default Magento display order property.
I tried by creating a new attribute called display_order, and each product holds a value based on its value the product needs to shown in front end. However, it is not working. Please help me fix this.
You'll need to extend the Mage_Catalog_Block_Product_List block to provide your own functionality for the getProductCollection() method. Probably something along the lines of:
class ... extends Mage_Catalog_Block_Product_List {
function getProductCollection() {
parent::getProductCollection()->addAttributeToSort('display_order', 'ASC')
}
}
Then, of course, you'll have to update you layout xml file on your, presumably, custom controller (unless you want all of the product listing screens to act like this) to use your new block instead of the Magento default of catalog/product_list.
Why don't you use the Magento sorting thing ?
In your category, under Category Product you have the possibility to choose the sorting order in the last column. To do it through php, just make a custom script that you'll need to launch once.
$collection = 'Your product collection';
$result = array();
foreach ($collection as $product) {
$sort = 'Your way of calculating the desired sorting';
$result[$product->getId()]=$sort;
}
Mage::getModel('catalog/category')->load('your category id')->setPostedProducts($result)->save();
And that's it :)
To change the display order , first you need to set the default sort by option to position.This can be done from the magento admin configuration.After that you need to set position for all the products starting with the value 1.
I come across the following module which will make this task very easy, just by drag and drop the products from the manage categories itself.Please check the following extension
http://www.blackqubers.com/extensions/product-sorting-drag-and-drop.html
Hope this will helps you

Resources