Magento - Add field to actions in Catalog Price Rules - magento

I want to add a custom field to the actions tab in the Catalog Price Rules section.
This is what I did:
I added these lines to the file app\code\core\Mage\Adminhtml\Block\Promo\Catalog\Edit\Tab\Actions.php
$fieldset->addField('custom_field', 'select', array(
'label' => 'Custom Field',
'title' => 'Custom Field',
'name' => 'custom_field',
'options' => array(
'1' => Mage::helper('catalogrule')->__('Yes'),
'0' => Mage::helper('catalogrule')->__('No'),
),
));
I changed the version to 1.6.0.4 in this file: app\code\core\Mage\CatalogRule\etc\config.xml
<Mage_CatalogRule>
<version>1.6.0.4</version>
</Mage_CatalogRule>
I created new file with the name app\code\core\Mage\CatalogRule\sql\catalogrule_setup\upgrade-1.6.0.3-1.6.0.4.php
$installer->startSetup();
$ruleProductTable = $installer->getTable('catalogrule/rule');
$columnOptions = array(
'TYPE' => Varien_Db_Ddl_Table::TYPE_SMALLINT,
'UNSIGNED' => true,
'NULLABLE' => false,
'DEFAULT' => 0,
'COMMENT' => 'Custom Field',
);
$installer->getConnection()->addColumn($ruleProductTable, 'custom_field', $columnOptions);
$installer->endSetup();
Then, I logged in to the admin panel and tried to edit one of the catalog price rules.
I saw my new field in the actions tab - great!
But when I click "Save", the changes that I made in that field, was not saved.
I logged in to the phpmyadmin and went to catalogrule table.
In this table I can see the new field custom_field, with the value 0 (default) - so It really didn't save the changed.
My question is what I did wrong?
Why the changed in the custom_field didn't saved in the DB?
Thanks

I'm not sure why it isn't working for you my guess is your installer isn't being called. I managed the modification through a module and local edits to future proof the change.
Directory structure:
/app/code/local/custom/promo
/app/code/local/custom/promo/etc/
/app/code/local/custom/promo/sql
/app/code/local/custom/promo/sql/promo_setup
/app/code/core/local/Adminhtml/Block/Promo/Catalog/Edit/Tab
add file /app/code/local/custom/promo/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Custom_Promo>
<version>1.0</version>
</Custom_Promo>
</modules>
<global>
<resources>
<promo_setup>
<setup>
<module>Custom_Promo</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
</promo_setup>
</resources>
</global>
</config>
add file /app/code/local/custom/promo/sql/promo_setup/install-1-0.php
/* #var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();
$ruleProductTable = $installer->getTable('catalogrule/rule');
$columnOptions = array(
'TYPE' => Varien_Db_Ddl_Table::TYPE_BOOLEAN,
'NULLABLE' => false,
'COMMENT' => 'Custom Promo',
);
$installer->getConnection()->addColumn($ruleProductTable, 'custom_promo', $columnOptions);
$installer->endSetup();
Clear caches and verify the column is added by looking at the catalog_rule table
SELECT * FROM catalogrule;
Finally take a copy of /app/code/core/Mage/Adminhtml/Block/Promo/Catalog/Edit/Tab/Main.php and create the same file /app/code/core/local/Adminhtml/Block/Promo/Catalog/Edit/Tab/Main.php
Edit the file and add the field before $form->setValues($model->getData());
//Custom Promo
$fieldset->addField('custom_promo', 'select', array(
'label' => 'Custom Promo',
'title' => 'Custom Promo',
'name' => 'custom_promo',
'options' => array(
'1' => Mage::helper('catalogrule')->__('Yes'),
'0' => Mage::helper('catalogrule')->__('No'),
),
));

Related

Need to create a new attribute in a new type of product

I am creating a module to insert a new attribute and a new type of product in my store. As template, I use the same registration screen of the Virtual Product type and include new options ONLY this type of product.
For this, in my config.xml, I used:
<catalog>
<product>
<type>
<incomm_virtual translate="label" module="incomm">
<label>Incomm Virtual</label>
<model>incomm/product_type</model>
<price_model>incomm/product_price</price_model>
<is_qty>1</is_qty>
<composite>0</composite>
<can_use_qty_decimals>0</can_use_qty_decimals>
</incomm_virtual>
</type>
</product>
</catalog>
and to configure the installation with the sql in same config.xml:
<resources>
<abc_incommproduct_setup>
<setup>
<module>ABC_Incomm</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
</abc_incommproduct_setup>
</resources>
The next file was used mymodule / model / product / type.php containing:
class ABC_Incomm_Model_Product_Type
extends Mage_Catalog_Model_Product_Type_Virtual
{
const TYPE_INCOMM = 'incomm';
const XML_PATH_AUTHENTICATION = 'catalog/incomm/authentication';
protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
{
if ($this->_isStrictProcessMode($processMode)) {
return Mage::helper('ABC_Incomm_Helper_Data')->__(
'Incomm product %s cannot be added to cart. ' .
' On the product detail page click the "Go to parent site"'.
' button to access the product.',
$product->getName()
);
}
return parent::_prepareProduct($buyRequest, $product, $processMode);
}
}
In the same folder, the virtual.php:
class ABC_Incomm_Model_Product_Virtual extends Mage_Catalog_Model_Product_Type_Virtual {
}
Finally, the SQL installation file in sql folder / abc_incommproduct_setup / mysql-install-4-0.1.0.php:
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'terms_of_use', array(
'group' => 'General',
'input' => 'textarea',
'type' => 'text',
'sort_order' => 4,
'label' => 'Terms of use',
'backend' => '',
'visible' => true,
'required' => true,
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'apply_to' => 'incomm', //only in this type of product
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
My problem is this, after disabling the cache in magento, clear the cache and include a new type of product, in the Product Types list appears my NEW type of product, but the options NOT load the new attribute inserted via sql, the What's happening? my sqlinstall.php did not run ??
Probably because your new product type is incomm_virtual as specified in config.xml and not incomm. Try to edit 'apply_to' => 'incomm' with 'apply_to' => 'incomm_virtual' and re-run the installer
If you upgrade you script follow the similar pattern as install upgrade script, except the file-name is now data-upgrade-{old-version}-{new-version}.php and they are located under the data folder.
If you don't want to upgrade your script and just change it. don't forget to delete the existing entries for youmodule_setup code from core_resource table. it only loaded at once. if you don't delete that your script wont be loaded.

Save custom attribute to order object at Checkout in Magento - How can I confirm this?

I've been having trouble with this for a few days, essentially I need to add a custom organisation_id attribute (that I have created via an installer script) to be added to both the customer object and order object at the end of the onepage checkout.
The installer script works fine (I can see the values within the eav_attribute and core_resources table in the database.
During the checkout phase on the final 'Order Review' section, once the Place Order button is clicked this executes the following observer:
sales_order_place_after
Once this has been executed it runs the following function within an Observer:
public function afterOrderPlaced($observer)
{
// this id below comes from a select dropdown within the checkout onepage & already saved to session
$organisation_id = Mage::getSingleton('customer/session')->getCustomerOrganisationId();
$this->_order = $observer->getEvent()->getOrder();
$this->_order->setOrganisationId($organisation_id)->save(); // e.g 25621
// Customer stuff
$this->_customer_id = $this->_order->getCustomerId();
$this->_customer = $this->_order->getCustomer();
$this->_customer->setOrganisationId($organisation_id)->save(); // e.g 25621
}
In this instance, I simply want to set the organisationId value on both the customer and order objects to '25621'.
Once I have completed the checkout process and hit the 'order confirmation' page I'd like to be able to confirm this organisation_id has indeed been correctly added to the customer and order object, can anyone confirm the easiest/best way to do this? I presume this will need to include some modification to the admin html for Sales to include this information as well.
Please note - My Magento skillset is rather limited
My Installer Script below:
$installer = new Mage_Eav_Model_Entity_Setup();
$installer->startSetup();
$installer = new Mage_Eav_Model_Entity_Setup();
$installer->startSetup();
$installer->addAttribute('customer', 'organisation_id', array(
'input' => 'select', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->addAttribute('quote', 'organisation_id', array(
'input' => 'select', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->addAttribute('order', 'organisation_id', array(
'input' => 'select', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Organisation ID',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->endSetup();
You can check your custom value in your database sales_flat_order_address table and sales_flat_quote_address table .
Let me know if you have any query

Magento - Add custom attribute to order

I'm trying to add a custom field to my orders. At this moment, I found the post bellow that helped me to create such attribute in my database:
http://fabrizioballiano.net/2011/11/15/create-a-custom-order-attribute-in-magento/
require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$installer = new Mage_Sales_Model_Mysql4_Setup;
$attribute = array(
'type' => 'int',
'backend_type' => 'text',
'frontend_input' => 'text',
'is_user_defined' => true,
'label' => 'My Label',
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'default' => 0
);
$installer->addAttribute('order', 'special_attribute', $attribute);
$installer->endSetup();
After executing the code above and creating several orders, I'm able to loop through all orders and see the default value to the every order.
The question is, how can I store the data I want in this field? How can I retrieve such data?
Thanks!
Add this to the gobal scope in config.xml. Then simply set the attribute in the quote - it gets automagically transferred to the order in the quote to order conversion process.
<global>
...
<fieldsets>
<sales_convert_quote>
<your_special_attribute>
<to_order>*</to_order>
</your_special_attribute>
</sales_convert_quote>
</fieldsets>
...
</global>
You can retrieve/set the attribute at any time via the magic getter/setter e.g.
$quote->getYourSpecialAttribute()
$order->getYourSpecialAttribute()
$quote->setYourSpecialAttribute()

magento: add attibute to product, but not show up when editing the product

As per bens comment and answer I updated my script, comments indicate changes
{Magento 1.4.0.1} currently i have an installer script:
$installer = $this;
$installer->startSetup();
//commented out to use factory method
//$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$setup = Mage::getResourceModel('catalog/setup','core_setup');
if(!$setup->getAttribute('catalog_product','attribute_code')){
$newFields = array(
'attribute_code' => array(
'type' => 'text',
'label' => 'Attribute Label',
//added visible option
'visible' => false,
),
);
$entities = array(
'catalog_product',
);
foreach($newFields as $attributeName => $attributeDefs) {
foreach ($entities as $entity) {
$setup->addAttribute($entity, $attributeName, array(
'type' => $attributeDefs['type'],
'label' => $attributeDefs['label'],
//added visible option
'visible' => $attributeDefs['visible'],
'class' => '',
'required' => false,
));
}
}
}
$installer->endSetup();
It works wonderfully! Except the attribute shows up in the General attribute group when editing the product and I don't want it to show up at all (its a secret ninja attribute) is there something I'm doing wrong? or perhaps something I should be doing to let Magento know not its not supposed to show up?
Using addAttribute() you can set the 'visibleindex tofalse. UsingupdateAttribute()` you should do the following:
$setup->updateAttribute('catalog_product','attr_code','is_visible',false);
Let me know if I'm wrong.

Magento 1.4.2 : errors when saving custom customer Attributes

I can't save my custom customer attributes:
My Magento backend is blocked and I get this errors :
2011-08-03T12:27:36+00:00 ERR (3): Warning: include(Mage\Customer\Model\Attribute\Data\.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\xampp\htdocs\magento_test142_3\lib\Varien\Autoload.php on line 93
2011-08-03T12:27:36+00:00 ERR (3): Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'Mage\Customer\Model\Attribute\Data\.php' for inclusion (include_path='C:\xampp\htdocs\magento_test142_3\app\code\local;C:\xampp\htdocs\magento_test142_3\app\code\community;C:\xampp\htdocs\magento_test142_3\app\code\core;C:\xampp\htdocs\magento_test142_3\lib;.;C:\xampp\php\PEAR') in C:\xampp\htdocs\magento_test142_3\lib\Varien\Autoload.php on line 93
This is my module code:
in file mysql4-install-0.1.0.php:
<?php
$installer=new Mage_Customer_Model_Entity_Setup ('core_setup');
$installer->startSetup();
$installer->addAttribute('customer', 'kd_nr', array(
'label' => 'Kundennummer',
'visible' => true,
'required' => false,
'position' => 20,
));
Mage::getSingleton( 'eav/config' )
->getAttribute( 'customer', 'kd_nr' )
->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
->save();
$installer->endSetup();
and here is my config.xml file:
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Newattcustomer>
<version>0.1.1</version>
</Mycompany_Newattcustomer>
</modules>
<global>
<resources>
<newattcustomer_setup>
<setup>
<module>Mycompany_Newattcustomer</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</newattcustomer_setup>
</resources>
<fieldsets>
<customer_account>
<kd_nr><create>1</create><update>1</update></kd_nr>
</customer_account>
</fieldsets>
</global>
</config>
I use Magento 1.4.2
Please help to solve this problem.
Thanks a lot.
[edit]
Thanks a lot for help, the code of Nasaralla seems to be working, but I discover that my problem come from a YES/NO select: Here is the code for the select box:
$installer->addAttribute('customer', 'in_search', array(
'label' => 'Appear in search',
'type' => 'int',
'input' => 'select',
'source' => 'eav/entity_attribute_source_boolean',
'visible' => true,
'required' => false,
));
Mage::getSingleton( 'eav/config' )
->getAttribute( 'customer', 'in_search' )
->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
->save();
It will be very helpful if anyone help me to solve the really problem
[/edit]
Get rid of this part in install script
Mage::getSingleton( 'eav/config' )
->getAttribute( 'customer', 'kd_nr' )
->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
->save();
In order to call eav_setup you do
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
and then you can updateAttribute like:
$setup->updateAttribute ($entityTypeId,$id,$field,$value = null,$sortOrder=null)
You can get the list of methods you can call in here:
Freegento-Eav-Model-entity-setup
Therefore your code, I think, will be replaced by:
$setup->updateAttribute('customer', 'kd_nr', 'used_in_forms', 'adminhtml_customer');
Ok if the above didn't work at all ... I think lets revert back to your original script and I think I can spot some fixes:
<?php
$installer=new Mage_Customer_Model_Entity_Setup ('core_setup');
$installer->startSetup();
$installer->addAttribute('customer', 'kd_nr', array(
'label' => 'Kundennummer',
'is_visible' => 1,
'is_required' => 0,
'sort_order' => 20,
));
$attr = Mage::getSingleton( 'eav/config' )->getAttribute( 'customer', 'kd_nr' );
$attr->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
$attr->save();
$installer->endSetup();
For details check the upgrade script mysql4-data-upgrade-1.4.0.0.7-1.4.0.0.8.php at core/Mage/Customer

Resources