Magento how to add extra fields for static block - magento

I've been trying to add extra fields to the static block so it will be easier to update.
How can I add a simple textfield or maybe a image upload field?
See example: http://i.stack.imgur.com/2HtDC.jpg
Thanks in advance!

For this, you need to overwrite this class Mage_Adminhtml_Block_Cms_Block_Edit_Form. This class is used to add fieldsets and fields for cms_block. Take a look on the _prepareForm() method inside it.
If you put this code, just after Title field,
$fieldset->addField('sub_title', 'text', array(
'name' => 'sub_title',
'label' => Mage::helper('cms')->__('Sub Title'),
'title' => Mage::helper('cms')->__('Sub Title'),
'required' => true,
));
you can see your sub-title text field in static blocks. However Do not edit a core file directly. You need to write a custom module that should overwrite this class. Your Module config file should contain this code
File : app/code/local/Namespace/Module/etc/config.xml
<config>
<global>
<blocks>
<adminhtml>
<rewrite>
<cms_block_edit_form>Namespace_Module_Block_Adminhtml_Cms_Block_Edit_Form</cms_block_edit_form>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
This will allow you to rewrite the class. What you need to do now is define the rewrite class now and in there you need to rewrite _prepareForm(). It should some what look like this.
Location : app/code/local/Namespace/Module/Block/Adminhtml/Cms/Block/Edit/Form.php
<?php
class Namespace_Module_Block_Adminhtml_Cms_Block_Edit_Form extends Mage_Adminhtml_Block_Cms_Block_Edit_Form {
protected function _prepareForm()
{
//put all the code inside parent class here
//then place the below content in appropriate place
$fieldset->addField('sub_title', 'text', array(
'name' => 'sub_title',
'label' => Mage::helper('cms')->__('Sub Title'),
'title' => Mage::helper('cms')->__('Sub Title'),
'required' => true,
));
return parent::_prepareForm();
}
}
Try based on this idea
EDIT
Please note, it will allow you to put new field in cms > block, howver to save this, you need to define model for your module. You have two options. Add a new field to save your new field in Cms > Block table or create your own table and store this value in that field along with the referene to cms >block table. This is out of box and you should implement it your own way.
Happy coding

In adition to #Rajeev
You need get the parent form in this way...
<?php
class Namespace_Module_Block_Adminhtml_Cms_Block_Edit_Form extends Mage_Adminhtml_Block_Cms_Block_Edit_Form {
protected function _prepareForm()
{
$form = parent::_prepareForm()->getForm();
$fieldset = $form->addFieldset('fieldset_example', array('legend'=>Mage::helper('core')->__('My example fieldset')));
$fieldset->addField('sub_title', 'text', array(
'name' => 'sub_title',
'label' => Mage::helper('cms')->__('Sub Title'),
'title' => Mage::helper('cms')->__('Sub Title'),
'required' => true,
));
return $this;
}
}

My suggestion is, rather add an extra field, you can add a <div>...</div> to in the content are by removing what you see is what you get editor(just click on the show / hide Editor).
And for image you can direct upload images by clicking on the inset/edit image option from the menu.
It will be simpler than creating custom fields.

Related

Add custom column to Magento report and sales dashboard

I've been searching for a module or a way to deal with the fallowing situation:
A customer orders a product that is 100$ and pays shipping of 10$. That would charge him a total amount of 110$. By the time the product arrives he notices that the product is a little scratched and instead of sending it back he accepts to receive a discount.
For this to happen properly I would make a credit memo with an adjustment refund of let's say 30$.
I need to see the total amount that remains after this operation is done (80$) in a separate column in either reports or the sales dashboard.
For this particular task we have also installed a module called "Advanced Orders Manager by Iksanika" but this appears to only get the data that already exists in the database and is not allowing us to use variable for let's say a substraction.
Also in Magento reports we use Reports > Sales > Orders but that gives us only the total figures and we cannot find anywhere a "total amount charged" that would give us the exact final figure (80$).
This is a particular request of the accounting dpt of an online store.
What you want to do is to add a custom grid column to the sales order grid.
Magento uses the database table sales_flat_order to fill the values in the sales order grid. Unfortunately the sales_flat_order table doesn't provide the information you need (grandtotal minus refunded amount) but both values separately (grand_total and total_refunded). Because of that you have multiple options:
Extend the table sales_flat_order.
Add a custom renderer for your custom sales order grid column.
As everything in this world both methods have advantages and disadvantages.
If you extend the sales_flat_order table you have to make sure the value of your new database column is set when creating new orders. On the other hand, as the value is persisted in your database, you can use the column for other extensions.
With a custom renderer you don't have to care about persistence. You have the opportunity to do operations and return your result which will be displayed in your custom sales order grid column. As we have both grand_total and total_refunded saved already you can return the grandtotal minus refunded amount.
I'll describe how to add a custom sales order grid column and add a custom renderer to return the value of grandtotal minus refund amount.
Part 1: How to add custom column to sales order grid in backend?
To add a custom column to sales order grid first add your own sales order grid block XX_ModuleName_Block_Adminhtml_Order_Grid.
Rewrite magentos *Mage_Adminhtml_Block_Sales_Order_Grid (app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php) and extend it with your sales order grid.
To add your custom column override _prepareColumns() method. Inside the overwritten _prepareColumns() you want to first add the column from parent class. Finally, add your custom column:
note: you can download the example module.
$this->addColumn('my_column', array(
'header' => Mage::helper('sales')->__('My Column'),
'index' => 'my_column',
'type' => 'text',
'renderer' => 'XX_ModuleName_Block_Adminhtml_Order_Grid'
));
Example etc/config.xml:
<?xml version="1.0"?>
<config>
<modules>
<EG_AdminSalesOrder>
<version>1.0.0</version>
</EG_AdminSalesOrder>
</modules>
<global>
<blocks>
<eg_adminsalesorder>
<class>EG_AdminSalesOrder_Block</class>
</eg_adminsalesorder>
<adminhtml>
<rewrite>
<sales_order_grid>EG_AdminSalesOrder_Block_Adminhtml_Order_Grid</sales_order_grid>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<eg_adminsalesorder>
<class>EG_AdminSalesOrder_Helper</class>
</eg_adminsalesorder>
</helpers>
</global>
</config>
Example custom sales order grid block:
class EG_AdminSalesOrder_Block_Adminhtml_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
/**
* Add custom column to sales order grid
*
* #return Mage_Adminhtml_Block_Widget_Grid
* #throws Exception
*/
protected function _prepareColumns()
{
$this->addColumn('real_order_id', array(
'header'=> Mage::helper('sales')->__('Order #'),
'width' => '80px',
'type' => 'text',
'index' => 'increment_id',
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('store_id', array(
'header' => Mage::helper('sales')->__('Purchased From (Store)'),
'index' => 'store_id',
'type' => 'store',
'store_view'=> true,
'display_deleted' => true,
));
}
$this->addColumn('created_at', array(
'header' => Mage::helper('sales')->__('Purchased On'),
'index' => 'created_at',
'type' => 'datetime',
'width' => '100px',
));
$this->addColumn('billing_name', array(
'header' => Mage::helper('sales')->__('Bill to Name'),
'index' => 'billing_name',
));
$this->addColumn('shipping_name', array(
'header' => Mage::helper('sales')->__('Ship to Name'),
'index' => 'shipping_name',
));
$this->addColumn('base_grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Base)'),
'index' => 'base_grand_total',
'type' => 'currency',
'currency' => 'base_currency_code',
));
$this->addColumn('grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Purchased)'),
'index' => 'grand_total',
'type' => 'currency',
'currency' => 'order_currency_code',
));
$this->addColumn('refunded', array(
'header' => Mage::helper('sales')->__('Total - Refund'),
'index' => 'refunded',
'type' => 'text',
'renderer' => 'EG_AdminSalesOrder_Block_Adminhtml_Sales_Order_Grid_Widget_Renderer_Refunded'
));
parent::_prepareColumns();
}
}
Part 2: How to add a custom renderer for my custom column?
Now you can add your custom renderer to fill the values into your custom sales order grid column.
First add a cusom renderer class XX_ModuleName_Block_Adminhtml_Sales_Order_Grid_Widget_Renderer_MyColumn.
Then extend Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract.
Override the render(Varien_Object $row) method. Here you can do your specific operation and return the string that should be displayed in your grid. In your case you want to load the order collection for the current $row param, get the total refunded amount, subtract the grandtotal with the refunded amount and return the value.
Example custom renderer block:
class EG_AdminSalesOrder_Block_Adminhtml_Sales_Order_Grid_Widget_Renderer_Refunded
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
/**
* #param Varien_Object $row
* #return string
*/
public function render(Varien_Object $row)
{
$currentOrderId = $row->getId();
$currentOrderGrandTotal = $row->getGrandTotal();
$orderCollection = Mage::getModel('sales/order')->getCollection();
$orderCollection->addFieldToSelect('total_refunded');
$orderCollection->addFieldToFilter('entity_id', array('eq' => $currentOrderId));
$orderCollectionItem = $orderCollection->getFirstItem();
$refundedAmount = $orderCollectionItem->getTotalRefunded();
$grandTotalWithoutRefundedAmount = (float)$currentOrderGrandTotal - (float)$refundedAmount;
$grandTotalWithoutRefundedAmount = Mage::helper('core')->currency($grandTotalWithoutRefundedAmount);
return (string)$grandTotalWithoutRefundedAmount;
}
}
You can download the example module.

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.

Magento Save Selected Country

I want to add another dropdown list of countries in customer account register page.I tried
<?php echo $this->getCountryHtmlSelect() ?>
But this doesn't display the dropdown.Another one I tried is
<select name="partner_country" id="partner_country">
<option value=''>– Please Select –</option>
<?php foreach($_countries as $_country): ?>
<option value="<?php echo $_country->getId() ?>"><?php echo $_country->getName() ?></option>
<?php endforeach; ?>
</select>
This one displays country list, but the selected country doesn't show up in backend customer information page.
I know getCountryHtmlSelect() renders dropdown of countries.Do I have create similar method in my module to save the selected country?
Update
I already created a source model while adding this attribute via setup script,
$installer->addAttribute('customer_address','partner_country_id',array(
'type' => 'varchar',
'label' => 'Partner Country',
'input' => 'select',
'source' => 'wholesale/attribute_source_partnercountry',
'global' => 1,
'visible' => 1,
'required' => 0,
'visible_on_front' => 1,
'sort_order'=>220
));
Source model
class Company_Wholesale_Model_Attribute_Source_Partnercountry extends Mage_Eav_Model_Entity_Attribute_Source_Table
{
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = Mage::getResourceModel('directory/country_collection')
->loadByStore($this->getAttribute()->getStoreId())->toOptionArray();
}
return $this->_options;
}
}
config.xml
<config>
<global>
<resources>
<wholesale_setup>
<setup>
<module>Company_Wholesale</module>
<class>Company_Wholesale_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</wholesale_setup>
</resources>
</global>
</config>
The issue you have is related to type of attribute you've created. For making possible selection of your custom attribute in the admin, you need to create/update it with type 'select' and 'source_model' for it. For this purpose using of customer module setup model is required, so in your module configuration of setup resources you need to specify it:
<config>
<global>
<resources>
<your_module_setup>
<setup>
<class>Mage_Customer_Model_Resource_Setup</class>
<module>Your_Module</module>
</setup>
</your_module_setup>
</resources>
</global>
</config>
And in your setup file, you need to create/modify your attribute. (If attribute exists, current code snippet will modify it, instead of creation).
<?php
$this->startSetup();
$this->addAttribute('customer', 'partner_country' , array(
'type' => 'varchar',
'label' => 'Partner Country',
'input' => 'select',
'source' => 'customer/entity_address_attribute_source_country'
));
$this->endSetup();
UPDATED:
Now I got your question, you haven't added your attribute to customer create account form, so your attribute is filtered out from data that is set to customer model during account creation process.
So you simply need to specify your customer attribute for the form where it should be possible to save your attribute.
Currently there are such forms available:
customer_account_create - Register Form
customer_account_edit - Change Account Details Form
checkout_register - Registering new account during checkout
For adding your custom attribute to form, just create one more setup script, that add a record with form code and your attribute id to table called customer/form_attribute:
<?php
$this->startSetup();
$attributeId = $this->getAttributeId('customer', 'partner_country_id');
$data = array(
array('attribute_id' => $attributeId, 'form_code' => 'customer_account_create'),
array('attribute_id' => $attributeId, 'form_code' => 'customer_account_edit'),
array('attribute_id' => $attributeId, 'form_code' => 'checkout_register')
);
$this->getConnection()->insertMultiple($this->getTable('customer/form_attribute'), $data);
$this->endSetup();
Just opt-out the forms you don't need.
If you would like to usegetCountryHtmlSelect(), then you should give it some parameters so that it can apply to your attribute, and not countryby default. For the register form, it could give something like this :
echo $this->getCountryHtmlSelect($this->getFormData()->getPartnerCountryId(), 'partner_country_id', 'partner_country', $this->__('Partner Country'))
And in the second example, you use partner_country in select name, whereas you created a partner_country_id attribute.

How to show Sum of Two fields in Grid in Magento Admin Panel

I am working on a extension in which user enter different price for " Stamp Cost, Ink Cost,
Form Cost ". currently in data grid i am showing value of one field
$this->addColumn('stamp_cost', array(
'header' => Mage::helper('imprint')->__('Stamp Cost'),
'width' => '100px',
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'index' => 'stamp_cost'
));
But Now I Need to show sum of all these fields in one column
How can we show sum of two fields in one column in magento admin data grid ?
Essentially, there are two ways to do it. Add the field to the collection and get the data from the database, or calculate it in PHP based on the 3 values returned from the DB. Doing the first way with the Magento Collection would, in my opinion, be too complex. instead, you want to use a Renderer (Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract)
First, inside of the Block/Adminhtml folder of your plugin, make a new folder called Renderer. Inside of it make a new file called CostSum.php with the following contents:
<?php
class Company_Module_Block_Adminhtml_Renderer_CostSum extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
return $row->getStampCost() + $row->getInkCost() + $row->getFormCost();
}
}
Then, in the grid, make a new column
$this->addColumn('cost_total', array(
'header' => Mage::helper('imprint')->__('Stamp Cost'),
//'index' => 'Im not sure this is necessary',
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'renderer' => new Company_Module_Block_Adminhtml_Renderer_CostSum()
));
Hope that helps!
The more right way is 'renderer' => 'company_module/adminhtml_renderer_costSum'
As #Zyava says, the correct option is this. But actually, is not 'company_module'. Instead you should call it as you have declared your blocks in the config.xml file.
<blocks>
<declaration>
<class>Company_Module_Block</class>
</declaration>
</blocks>
So, in this case you should create the 'renderer' as:
'renderer' => 'declaration/adminhtml_renderer_costSum'

Magento - Add column to customer grid

how to add a custom column to magento customer grid ?
Thanks a lot.
You should override the class Mage_Adminhtml_Block_Customer_Grid (app/code/core/Mage/Adminhtml/Block/Customer/Grid.php) and apply the following changes:
1 - add the new attribute to show in the _prepareCollection() function
2 - add the new column to show in the _prepareColumns() function
Credit: http://www.leonhostetler.com/blog/magento-add-attribute-columns-in-manage-products-grid-201205/
Magento does not provide us with the ability to choose which attributes are included as columns in the Manage Products grid but it’s fairly simple to make the necessary code changes.
The code that generates the Manage Products grid is at /app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php. The first thing you need to do is copy Grid.php into to the local directory structure. In other words, you copy Grid.php into the following location; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/. If there is no such location, then you must create the necessary directories. The final location of the file must be; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php
Now, open Grid.php (the one in the local directory structure) and begin editing. Find the following code;
$this->addColumn('sku',
array(
'header'=> Mage::helper('catalog')->__('SKU'),
'width' => '80px',
'index' => 'sku',
));
That’s the code that adds the SKU column to the product grid. Now, let’s say you have a custom attribute called Supplier ID (supplier_ID) and you want these to appear on the Manage Products grid as well. Place the following code either before or after the above block of code, as long as it’s inside _prepareColumns().
$this->addColumn('supplier_id',
array(
'header'=> Mage::helper('catalog')->__('Supplier ID'),
'width' => '150px',
'index' => 'supplier_id',
));
Then add the following line to _prepareCollection() where the other attributes are listed like this;
->addAttributeToSelect('supplier_id')
That should be all you need to do. You might have to re-compile, refresh your caches, logout, and log back in to see the change in your product grid.
The above example is for adding an attribute with a Catalog Input Type for Store Owner of Text Field. What if your attribute uses a dropdown list? The code above will have to be modified.
Let’s say you have an attribute called Supplier (supplier) which in the product editor presents a dropdown list of suppliers to choose from. To do this, we can add the following code to _prepareColumns():
$supplier_items =
Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id',
'attribute_code'); foreach ($supplier_items as $supplier_item) :
if ($supplier_item->getAttributeCode() == 'supplier')
$supplier_options[$supplier_item->getOptionId()] = $supplier_item->getValue(); endforeach; $this->addColumn('supplier',
array(
'header'=> Mage::helper('catalog')->__('supplier'),
'width' => '150px',
'type' => 'options',
'index' => 'supplier',
'options' => $supplier_options, ));
And let’s not forget to add the following line to _prepareCollection() where the other attributes are listed like this;
->addAttributeToSelect('supplier')
That should do it for you. Re-compile, refresh your caches, and logout and then back in if you need to.
I have publish here with real example.
If you need to add custome attribute, you may need to take care of join statements carefully.
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
Rewrite customer grid block with your custom module.
app/code/[local or community]/YourCompany/YourModule/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<yourcompany_yourmodule>
<version>0.1.0</version>
</yourcompany_yourmodule>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>YourCompany_YourModule_Block_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
app/code/[local or community]/YourCompany/YourModule/Block/Customer/Grid.php
<?php
class YourCompany_YourModule_Block_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
public function setCollection($collection)
{
$collection->addAttributeToSelect('confirmation');
parent::setCollection($collection);
}
protected function _prepareColumns()
{
parent::_prepareColumns();
$this->addColumn('confirmation', array(
'header'=> Mage::helper('sales')->__('Confirmed'),
'index' => 'confirmation',
'type' => 'text',
'width' => '100px',
));
return parent::_prepareColumns();
}
}
Detailed explanation can be found here:
http://tipsmagento.blogspot.com/2011/03/add-new-column-on-customers-grid.html
Make sure to check out TigerMin for Magento. It's a tool with which you can easily add columns to productsgrid and even inline edit values instantly. Here`s a live demo: http://demo.emvee-solutions.com/tigermin/

Resources