Magento how to save custom field in admin panel - php

In Manage Products -> Edit a product -> Images I've added 2 new columns (which are located in the catalog_product_entity_media_gallery_value table).
Those 2 columns are filled every time there is a media import and each field only show the value (you can't enter a new value). It's also used on the frontend.
I've modified in my own theme the correct admin template file and created a new product.js to display the 2 new fields.
So far everything is working as intended.
But whenever a user tries to modify something in the Images section, let say the label of an image or the position, my 2 custom fields are not saved and Magento give them the NULL value.
Because the 2 new columns are not input fields, I thought Magento would not update it, but looks like it does. I've been trying to find where the label and position are saved for the Images section so that I can add my custom values but I can't find it. Could somebody point me in the right direction?
Or would it be better to listen to the catalog_product_save_after event and create an observer to save my custom values?
Any help would be great, thank you.

Finally found it, I missed it during my previous searches. I'll answer it myself just in case it can help someone else. Code is in the Mage_Catalog_Model_Product_Attribute_Backend_Media class, inside the afterSave function.
All you have to do is to rewrite it.
Config.xml should contain the following :
<models>
<catalog_resource>
<rewrite>
<product_attribute_backend_media>
Namespace_Module_Model_Resource_Product_Attribute_Backend_Media
</product_attribute_backend_media>
</rewrite>
</catalog_resource>
</models>
And class should be : Namespace_Module_Model_Product_Attribute_Backend_Media extends Mage_Catalog_Model_Product_Attribute_Backend_Media

Related

Custom Fields, linking to a separate download page?

Afternoon all,
I have some Advanced Custom Fields set up, which allow the user to download files which are appropriate to a certain product.
For example, if the product was 'Product 1', the files would be 'Product 1 size' etc. This works perfectly, using the following:
Brochure
This adds the file which is added on each individual page, as in ACF I have set the location rules as shown in the screenshot
Now, the issue is as follows.
I need to create a whole new page, called downloads.
This page, will list all the products in my page, and then need to have the downloads available (the Custom Field File). Now, because this field is set up to show on the product pages, I am completely unsure as to how to show the downloads on a seperate page, because using
Brochure
is technically not telling me which product this is for.
I hope this makes sense.
EDIT:
I completely forgot to mention, this information will need to be inside a table, and I am currently using TablePress, a plugin.
So it would go like:
Prouduct --- Category --- Download
Thank you all!
After you have edited your question, my first answer does not make sense anymore. So here is what I suggest now:
A. This is the solution to what you actually asked
Install the Reveal IDs Plugin so you can see in the admin panel what post-IDs your products have
Look up the ID for a specific product
In your TablePress table-cell you paste the ACF-shortcode:
Download brochure
post_id="12": this is the ID of your product-post which you can look up with Reveal IDs.
I already tested this right now, so I do know it's working.
B. Another solution that is more a question
Your ACF field 'brochure' obviously contains a URL to a file which might be available in your WP media library too?!
When this is the case you don't need the ACF field for your table. You can just:
click "Advanced Editor" (when working with the table)click into a table cell
click into a table cell
click "Add files" and link to your file directly this way
Maybe that is an option too.
I think I understand what you're trying to do. This should point you in the right direction: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/

Adding a search field to the Magento admin

Is there a way to add a new search field in the Magento admin under "Orders"? I want to be able to search by coupon code usage (ex: search by coupon code: "sale2013", come up with all the orders that applied that coupon code during checkout).
I'm able to pull this data out of the Magento DB, but I'd really like to add this functionality to the Magento admin to make it easier for co-workers and my boss. Can it be done and, if so, ideas as to where I can start? Thanks guys.
The coupon code used during checkout is stored on the primary order table (sales_flat_order) when the customer checks out. If there is no value, then no coupon code was used. However, the data for the grid in the admin comes from the sales_flat_order_grid table; it comes from there instead of the primary order table for performance reasons which become particularly evident on very high traffic sites.
Now that we know where the data is that you need to filter on, and where the data for the grid comes from, we can move on to build it!
The first thing that you will need to do is add a coupon_code column to the sales_flat_order_grid table matching the definition of the column on the sales_flat_order table. The values in the coupon_code column of sales_flat_order should automatically populate into sales_flat_order_grid once the matching column is there and your cache storage has been flushed.
The data is updated on the order save, so to be specific, new orders and orders updated via the admin (even a comment on the order) will have the data populated. For existing orders, run a data upgrade script to copy the values over.
After the data/schemas are in place, you'll want to have the grid show a filterable column for it. For this you will want to rewrite the Mage_Adminhtml_Block_Sales_Order_Grid class from your custom module (please don't edit the core files directly, it will haunt you later) and override the _prepareColumns method with one that includes your column definition:
$this->addColumn('coupon_code', array(
'header' => Mage::helper('sales')->__('Coupon Code'),
'index' => 'coupon_code',
));
If you're very fresh on Magento development, I'd venture to say that I've only begun to help you. But you'll have plenty of reference points from which to dig in and build the functionality you need. HTH! :)
Adding new columns to a grid is easy with this extension:
https://github.com/magento-hackathon/GridControl
Add this extension, write your own with a gridcontrol.xml inside your etc directory
And then something like this should work:
<?xml version="1.0"?>
<gridcontrol>
<grids>
<order.grid>
<coupon_code>
<after>columnname</after>
<add>
<header>Coupon code</header>
<type>text</type>
<index>coupon_code</index>
<joinField>coupon_code|sales/order|coupon_code|entity_id=entity_id||left</joinField>
</add>
</coupon_code>
</order.grid>
</grids>
</gridcontrol>

Magento product page adjustments

I need to display on product page few fields from corresponding row of MANUFACTURERS table
I don't know how to find required Model and Controller for that task.
Manufacturers table is the table where manufacturers stored
And there is another one table Manufacturers_products (manufacturer_id, product_id)
What I did is added 2 checkboxes to backend add/edit manufacturers form and added to fields for this checkboxes in manufacturers table.
Now I need to display 2 types of strings for each of fields, depending on user choice, during process of manufacturers create.
For example, if 1st checkbox is checked, then on all products, that have the same manufacturer, text row must appear, telling that 1st checkbox is checked. The same for the second.
Actually I need this checkboxes take part in product compare and filtering.
Any information will help. Thanks.
Here is my solution.
1) Added new block to Manufacturers module layout ( manufacturers.xml), situated in the layout folder of frontend site theme
2) Created new block class in Manufacturers module Block folder and specified it in the type attribute of my block, added on the 1st step
3) In the block class, you must extend protected method
_tohtml()
by adding your own behavior. Don't forget to call
parent::_tohtml()
4) To work with Manufacturers table, you must add new method to Manufacturers model of this module and make here a call to
$this->getRequest()->yourNewMethodName()
5) Add new method with the same name to the Manufacturers model, placed in Mysql4 folder or Model folder in this module. Here you must add your query, that will retrieve data from manufacturers table
You can also create enable/disable config parameter. To do this, you must read about Magento system.xml configuration (Tabs,Sections,Groups,Fields)
This solution steps are acceptable for any magento module. Hope this helps to somebody.
If you want to get the Model and controller of the extension then you need to check the Module files. In order to check that just
a) Go to app/code/community or app/code/local there you will find the Package name like FME and inside that folder you will find your module name.
b) In order to check the Model just Go inside the Module folder->etc folder and check the config.xml
c) In config.xml you will find a code like
<entities>
<modelname>
<table>tablename</table>
</modelname>
</entities>
In the above code Modelname tag will give the modelname
d) In order to find the controller you need to check the controllers folder.
hope this helps!!

store values inside Magento attributes

I am setting up a magento feed and I stuck since I need the system to auto calculate the values for the last few attributes.
how can I have magento create an attribute with the MAX sale price, and MAX list price, and Rating, also I need 2 more attribites 1 for discount amount and the last for discount percentage.
any help is appreciated.
If I understand your problem correctly, you could probabaly use the afterLoad event of some Magento objects. This would work for most models within Magento.
Preface
In general, most of the Model used in Magento (that is, e.g. products, orders, etc. in the core libraries and every model you would define in a custom module with db access) extend the class Mage_Core_Model_Abstract, which provides certain "event"-based methods.
These methods are _afterDelete, _afterDeleteCommit, _afterLoad, _afterSave, _afterSavecommit, _beforeLoad, _beforeSave and _beforeDelete (in Magento >1.4.2.0).
Overriding a model
I assume, you would like to have automatic caluclated attributes on a product. The first step here would be to create a new custom module which overrides the core model for Magento products. In a newly created model, you can use the following entry in ./app/code/local/YourNamespace/YourModule/etc/config.xml:
[...]
<global>
[...]
<models>
<catalog>
<rewrite>
<product>YourNamespace_YourModule_Model_Product</product>
</rewrite>
</catalog>
</models>
[...]
<global>
[...]
You can then overwrite the Product Model as shown in a file called ./app/code/local/YourNamespace/YourModule/Model/Product.php:
<?php
class YourNamespace_YourModule_Model_Product extends Mage_Catalog_Model_Product
{
protected function _afterLoad()
{
parent::_afterLoad();
//your voodoo goes here.
$my_awesome_calculated_value = $this->getData('my_awesome_float_attribute') / 2;
$this->setData('my_awesome_calculated_value', $my_awesome_calculated_value);
}
}
With this, as long as you have a model loaded, you have the opportunity to use that calculated value in whatever way you could conceive. Keep in mind, that this code is executed every time a product is loaded, which can be quite often (if it's a product, that is), so don't get too temerarious with this.
Also keep in mind, that this value is lost, after your code execution is finished, as the value exists only as long as the model "lives". If you want to store it, you could also override _beforeSave or create placeholder attributes, which are filled each time a model is loaded and then saved afterwards.
For the attributes for discount_amountand discount_percentage i would use the same strategy:
load the model
calculate the dicount_amount and discount_percentage in _afterLoad and store it on the model
work with the model.
Addendum
If you're new to Magento and don't know explicitly how to create new modules, you can start with http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/custom_module_with_custom_database_table (although it's a bit outdated) or look around stackoverflow.
lg,
and good luck,
flo

Custom Magento Order Field and the Magento Database

Alright, so I've successfully added a custom order field to my page layout in magento. I require a "dealer" from which the user has to select a "dealer state" and then I use ajaxt to pull in all the dealers from that specific state into another field called "dealerid". The value of this field is in fact the dealer id in my database. My question is kind of a full one.
First, I need to add a field to the "order table" that saves the dealer id for the specific order and secondly I need to actually have magento take this field from the form and insert it. How do I go about doing these things? Mainly, what table would magento store this dealerid and what files do I modify to actually have magento access and insert this field value?
Note: I am using the one page checkout method only.
First of all, familiarize yourself with magento SQL update mechanism (its diffrent when your dealing with EAV - like customer tables - and regular tables - like orders). Create a module which will have a sql-update script in /YourNamespace/YourModule/sql/yourmodule_setup/mysql4-install-1.0.0.php for instance. You should use something like:
$installer = $this;
$installer->getConnection()->addColumn($installer->getTable('sales/order'), 'dealerid', 'decimal(12,4) NULL');
and in your config.xml:
<global>
(...)
<models>
(...)
<yourmodule>
<class>Mage_Sales_Model</class>
<resourceModel>yourmodule_mysql4</resourceModel>
</yourmodule>
<yourmodule_mysql4>
<class>YourNamespace_Yourmodule_Model_Mysql4</class>
<entities>
<order><table>sales_order</table></order>
</entities>
(...)
</yourmodule_mysql4>
(...)
</models>
<resources>
<yourmodule_setup>
<setup>
<module>YourNamespace_YourModule</module>
<class>YourNamespace_YourModule_Model_Mysql4_Setup</class>
</setup>
</yourmodule_setup>
</resources>
(...)
</global>
Remember to create your own classes: YourNamespace_YourModule_Model_Mysql4_Setup, YourNamespace_Yourmodule_Model_Mysql4. Check in app/code/core/Mage/Sales/ how they should look like (nothing special needed there, they just must inherit from proper core classes). Also check out the app/code/core/Mage/Sales/sql and app/code/core/Mage/Sales/etc/config.xml.
If you want to get a field from the onepage checkout, you must do a couple of things... I don't know what you really need from the user (and in what part of checkout its going to be taken). Adding an input is done by the templates (edit proper files in app/design/frontend/yourskin/default/template/checkout).
To capture the right data you must rewrite the checkout controller whit your own controller action (look at the app/code/core/Mage/Checkout/Controllers/OnepageController.php) to get the data from the form and store it in the checkout session. After that you must hook to the checkout_type_onepage_save_order_after event (its in the app/code/core/Mage/Checkou/Model/Type/Onepage.php), take the data from your session and put it in the Order model (and save it of course). Its kind of a standard procedure, so you shouldn't have a problem.
Its possible that you will also have add a column to the sales_flat_order (the structure of order tables changed in 1.4.x.x, so I'm not sure of it - you will have to do some experimenting).
If you want to see it in the adminhtml order view, you will have to override some Core/Adminhtml/Blocks/Sales/Order (and templates probably).
Its kind of a complex procedure, so you'll have to figure out some things by yourself. This post is kind of a general idea how it should be done.

Categories