I am creating a module that needs the presence of one special product.
Did anybody manage to create a new product during a module's setup script?
There are several problems arising, for example Mage_Core_Model_App::getStore() is returning the default store as updateMode is set to true.
I fixed the problem
Call to a member function getStoreIds() on a non-object in
Mage/Catalog/Model/Resource/Abstract.php on line ...
by adding the following code at the beginning of my data upgrade script where i create products:
Mage::app()->getStore(Mage_Core_Model_App::DISTRO_STORE_ID)->setWebsiteId(1);
It is a workaround, but i could not find any other solution.
I think it should work with a data update script (mysql4-data-upgrade-1.0.0-2.0.0.php). In the data Mage_Core_Model_Resource_Setup::applyAllDataUpdates() function the update mode is - in contrast to the normal update scripts - not set to true. The update mode causes problems with creating products.
Try the below script for creating a product using SQL setup resource file
// Create Default Products
$product = Mage::getModel('catalog/product');
$data = array(
'attribute_set_id' => $attributeSetId,
'type_id' => 'simple',
'store_id' => 0,
'category_ids' => array($category->getId()),
'website_ids' => array(0),
'sku' => 'sample-product',
'name' => 'Sample Product',
'description' => 'Sample Product',
'short_description' => 'Sample Product',
'status' => 1,
'visibility' => 4,
'weight' => 1,
'price' => 100.00,
'setcustomdefault' => 1,
'tax_class_id' => 0,
'rearimage' => 'rear.png',
'frontimage' => 'front.png',
'defaultimage' => 'thumb.jpg',
'stock_data' => array('is_in_stock' => 1,'qty' => 20),
'created_at' => strtotime('now')
);
$product->addData($data)
->setInitialSetupFlag(true)
->save();
Here is a piece of code to create a product programatically:
require_once 'app/Mage.php';
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app();
$product = new Mage_Catalog_Model_Product();
// Build the product
$product->setSku('some-sku-value-here');
$product->setAttributeSetId('some_int_value_of_some_attribute');
$product->setTypeId('simple');
$product->setName('Some cool product name');
$product->setCategoryIds(array(7)); # some cat id's, my is 7
$product->setWebsiteIDs(array(1)); # Website id, my is 1 (default frontend)
$product->setDescription('Full description here');
$product->setShortDescription('Short description here');
$product->setPrice(39.99); # Set some price
# Custom created and assigned attributes
$product->setHeight('my_custom_attribute1_val');
$product->setWidth('my_custom_attribute2_val');
$product->setDepth('my_custom_attribute3_val');
$product->setType('my_custom_attribute4_val');
//Default Magento attribute
$product->setWeight(4.0000);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setStatus(1);
$product->setTaxClassId(0); # My default tax class
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));
Mage::helper('core')->p($product->getData());
After that to save it use $product->save();
Play with this code to get an ideea what it does.
Related
I am dynamically adding products to the cart in Magento2 with some custom options. Every product has the same base product id with different options. Represent Product has been properly overridden so that all products added to the cart are separate. However with this code, the second product added will lose it's custom options:
$magento_product = $this->productRepository->get('simple-product-1');
$params = array(
'product' => $magento_product->getId(),
'qty' => intval(5),
'options' => array(
'cr_price' => 12.0,
'Product' => "Test P",
'cr_XML' => '<root></root>'
),
);
$this->cart->addProduct($magento_product, $params);
$params = array(
'product' => $magento_product->getId(),
'qty' => intval(10),
'options' => array(
'cr_price' => 14.0,
'Product' => "Test P2",
'cr_XML' => '<root></root>'
),
);
$this->cart->addProduct($magento_product, $params);
$this->cart->save();
Only the first product has an entry in the quote_item_option table.
Any thoughts on why or how to fix would be appreciated.
Force reloading the product between each add fixes this issue.
$this->productRepository->get('simple-product-1', false, null, true);
The last true parameter is forceReload.
I’m trying to precomplete custom options during the process of creating or editing a product in the Magento admin : after product type choice.
I add an event trigger on the Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs _prepareLayout function. So basically when you open the edition or creation of a product, the event is triggered.
My aim is : when you start editing your new product, 3 custom options are already present in the “Custom Options” tab.
I can’t trigger it with the catalog_product_save_before event because one option is a dropdown type and have to be filled by the admin.
So i’ve coded my observer and succeeded to modify the product name with setName() function, but I can"t find how to precomplete/add custom options.
I tried with the code of the following blog : http://kamal250.wordpress.com/2012/10/22/create-custom-option-programatically-while-creating-product/#comments
But it doesn’t seems to work.
Anyone can help me with that ?
Here is my code in the observer :
$option_data = array(
'is_delete' => 0,
'is_require' => true,
'previous_group' => '',
'title' => 'Height',
'type' => 'field',
'price_type' => 'fixed',
'price' => '0',
'sort_order' => 1,
'values' => array()
);
$product->setHasOptions(1);
$product->setCanSaveCustomOptions(1);
$product->setOptions(array($option_data));
$product->setProductOptions(array($option_data));
$opt = Mage::getSingleton('catalog/product_option');
$opt->setProduct($product);
$opt->addOption($option_data);
$opt->saveOptions();
$product->setOption($opt);
Finally i have found the solution :
I modified the trigger to the action of opening the custom options tab.
And here is my code in the observer :
$product = $observer->getProduct();
$option_data = array(
'is_delete' => 0,
'is_require' => true,
'previous_group' => '',
'title' => 'Height',
'type' => 'field',
'price_type' => 'fixed',
'price' => '0',
'sort_order' => 1
);
$product->setHasOptions(1);
$option = Mage::getModel('catalog/product_option')->setProductId($product->getId())->setStoreId($product->getStoreId())->addData($option_data);
$option->save();
$product->addOption($option);
Hope it will help someone sooner or later.
See ya.
I've been following along with this example: Magento programmaticaly create bundle Product
and the code is working when I create a new product, however, I can't get it to work when I load a product either bundled or simple. [EDIT] I can load a bundled product that I created programmatically through the code below and add products to the bundle. A bundled product I created through the GUI I cannot add products too.
Any idea how I can load a product up then bundle it with another a product?
Here is my current code:
$items = array();
$items[] = array(
'title' => 'Bundle Option',
'option_id' => '',
'delete' => '',
'type' => 'radio',
'required' => 1,
'position' => 0,
);
$selectionRawData = array();
$selectionRawData[0] = array();
$selectionRawData[0][] = array(
'selection_id' => '',
'option_id' => '',
'product_id' => 3,
'delete' => '',
'selection_price_value' => 0,
'selection_price_type' => 0,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => 0,
'is_default' => 1,
);
$selections = $selectionRawData;
$websiteIDs = array(1);
$cats = array(4);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
// load product
// NOT WORKING
$product = Mage::getModel('catalog/product');
$product->load(127);
// new product
/******
* THIS WORKS
$p = array(
'sku_type' => 1,
'sku' => '123321',
'name' => "BarProduct",
'description' => 'Foo',
'short_description' => 'Bar',
'type_id' => 'bundle',
'attribute_set_id' => 4,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 1,
'price_view' => 0,
'price' => 1.99,
'has_options' => 1,
'required_options' => 1,
'status' => 1,
'created_at' => strtotime('now'),
'category_ids' => $cats,
'store_id' => 0,
'website_ids' => $websiteIDs,
'weight' => 0,
'weight_type' => 1,
'delivery_time' => '',
'generate_meta' => 1,
'tax_class_id' => 1, //19%
);
$product->setData($p);
*****/
Mage::register('product', $product);
Mage::register('current_product', $product);
$product->setCanSaveConfigurableAttributes(false);
$product->setCanSaveCustomOptions(true);
$product->setBundleOptionsData($items);
$product->setBundleSelectionsData($selections);
$product->setCanSaveCustomOptions(true);
$product->setCanSaveBundleSelections(true);
$product->setAffectBundleProductSelections(true);
$product->save();
$result['product_name'] = $product->getId();
return $result;
This is important for bundled products:
$product->setData("price_type", 0);
You must set this attribute to 0 (Dynamic price) before saving.
Reindexing is then necessary of course.
I had some funky stuff going on with my product index. I deleted all my products and fixed my index and this now works, albeit not well.
So here is what I gleaned from the process:
If you want to take two simple products and bundle them, you'll need to create a new bundled bundled product via the $p = array code above, then add both simple products.
Otherwise, you'll need a bundled product that is premade via the magento gui. then you'll have bring up that product using the $product->load(product_id) command, then add your simple products to that.
Just delete all unneeded options like this:
$optionsselectionsmap = array();
$options = Mage::getModel("bundle/option")->getCollection()->setProductIdFilter($product->getId());
foreach($options as $option){
$selection = Mage::getModel("bundle/selection")->getCollection()->setOptionIdsFilter($option->getId())->getFirstItem();
$tmp = array();
$tmp['option_id'] = $option->getId();
$tmp['selection_id'] = $selection->getData('selection_id');
$optionsselectionsmap[$selection->getData('sku')] = $tmp;
}
$deleteoptionids = array();
foreach($optionsselectionsmap as $k=>$v) $deleteoptionids[] = $v['option_id'];
foreach($product->getTypeInstance(true)->getOptionsCollection($product) as $deleteitem){
$deleteitem = $deleteitem->getData();
$deleteitem['delete'] = 1;
$bundleOptions[] = $deleteitem;
}
I need to insert a bundle in my shopping cart using the API for Mage_Checkout_Model_Cart_Product_Api,
function add($quoteId, $productsData, $store = null)
The simple product can perfectly, but I have problems with the bundle.
there are probably errors in the creation of the $productsData?
$productsData = array ('product_id' => $productId,
'qty' => $qty);
Thanks
sorry my friend but our problem is that we need to insert a bundle in a cart, but we don't use the model "checkout/cart", but "checkout/cart_product_api" because we'll be able to add a bundle in a defined quote_id.
this is the function:
$params = array(
'product_id' => $productId,
'related_product' => null,
'bundle_option' => array(
57 => array(0 => 37,
1 => 38
),
'bundle_qty' => 1));
Mage::getModel('checkout/cart_product_api')->add($cartId, $params, $store_id['eshop_id']);
I have my module. This module has installation script where should be add custom image field to categories.
$setup->addAttribute('catalog_category', 'additional_image', array(
'type' => 'varchar',
'backend' => 'catalog/category_attribute_backend_image',
'label' => 'Additional Image',
'input' => 'image',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'default' => '',
'position' => 6,
));
After that it must change captions other image fields (Image, Thumbnail). How I can get this system's fields and change their?
You can do this using Mage_Eav_Model_Entity_Setup::updateAttribute() method.
This is a long way off but someone else may need the information.
Mage_Eav_Model_Entity_Setup::updateAttribute()
has 5 arguments, 3 of which are necessary.
I am going to use the example of a custom customer attribute:
$entityTypeId = 'customer'
$id = 'my_custom_attribute_code'
$field = 'is_used_for_customer_segment'
$value = '1'
$sortOrder = Not Needed
So as you can see I am using the customer entity to update the attribute. I am updating my custom attribute with attribute id (code) my_custom_attribute_code. The field in this attribute that I am udpating is the is_used_for_customer_segment and setting the value to yes(1).
Here is an example of how to do this as an update.
$installer->startSetup();
$installer->updateAttribute('customer', 'my_custom_attribute_code', 'is_used_for_customer_segment', '1');
$installer->endSetup();