How to add new columns in group price section in magento - magento

I need to add 2 new columns in magento admin in the group price section.
I have checked the template file.
app/design/adminhtml/default/default/template/catalog/product/edit/price/group.phtml
I have added new columns there, but..
how can i add these two columns in database and save values.
Any suggestions?
Thanks.

I've had the same issue. Answer by Samuel Coman was great help for me. To be more precise, you have to modify three files. I have added integer column 'popularity' (to be used by xml-rpc methods), but rules are the same. Here is my example.
The first file
magento/app/design/adminhtml/default/default/template/catalog/product/edit/price/group.phtml
in table around line 50 add new <th>:
<th><?php echo $this->getPriceColumnHeader(Mage::helper('catalog')->__('Popularity')); ?></th>
in var groupPriceRowTemplate around line 67 add new <td>:
+ '<td><input class="<?php echo $_htmlClass; ?>" type="text" name="<?php echo $_htmlName; ?>[{{index}}][popularity]" value="{{popularity}}" id="group_price_row_{{index}}_popularity"/></td>'
in var groupPriceControl line 95 modify default arguments etc.:
var data = {
website_id: '<?php echo $this->getDefaultWebsite(); ?>',
group: '<?php echo $this->getDefaultCustomerGroup(); ?>',
price: '',
popularity: '',
readOnly: false,
index: this.itemsCount++
};
if(arguments.length >= 3) {
data.website_id = arguments[0];
data.group = arguments[1];
data.price = arguments[2];
data.popularity = arguments[3];
}
if (arguments.length == 5) {
data.readOnly = arguments[4];
}
add your column in addItem call, line 165:
groupPriceControl.addItem('<?php echo $_item['website_id']; ?>', '<?php echo $_item['cust_group']; ?>', '<?php echo sprintf('%.2f', $_item['price']); ?>', '<?php echo sprintf('%.0f', $_item['popularity']); ?>', <?php echo (int)!empty($_item['readonly']); ?>);
The second file
magento/app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Groupprice/Abstract.php
here you have to modify afterSave method, firstly add your column to the array $new, line 316:
$new[$key] = array_merge(array(
'website_id' => $data['website_id'],
'all_groups' => $useForAllGroups ? 1 : 0,
'customer_group_id' => $customerGroupId,
'value' => $data['price'],
'popularity' => $data['popularity'],
), $this->_getAdditionalUniqueFields($data));
then you have to handle a case when you only change your new column and save it, so on line 349, after if (!empty($update)) statement, goes:
if (!empty($update)) {
foreach ($update as $k => $v) {
if ($old[$k]['price'] != $v['value'] || $old[$k]['popularity'] != $v['popularity']) {
$price = new Varien_Object(array(
'value_id' => $old[$k]['price_id'],
'value' => $v['value'],
'popularity'=> $v['popularity'],
));
$this->_getResource()->savePriceData($price);
$isChanged = true;
}
}
}
The third file
magento/app/code/core/Mage/Catalog/Model/Resource/Product/Attribute/Backend/Groupprice/Abstract.php
here modify loadPriceData method to properly show values from database; you have to just add your column to $colums variable, line 49:
$columns = array(
'price_id' => $this->getIdFieldName(),
'website_id' => 'website_id',
'all_groups' => 'all_groups',
'cust_group' => 'customer_group_id',
'price' => 'value',
'popularity' => 'popularity',
);
There is no need to change savePriceData method, as _prepareDataForTable inside it will find your new columns in database.
Than you have to add your columns to group prices table and also to tier price table, because it uses the same save methods, so run in mysql on your database commands similar to the following:
alter table catalog_product_entity_group_price add popularity integer;
alter table catalog_product_entity_tier_price add popularity integer;
And last, but not least remember to refresh the cache - without doing this system won't find new columns.
Hope it will help someone.

To add the column create a new module and inside the installer alter the table catalog_product_entity_group_price and add your desired columns.
To save & display the data...well, I hope you are pretty advanced of Magento overriding.
The file you mentioned takes some data from Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Price_Group_Abstract but the real data is taken using the loadPriceData from Mage_Catalog_Model_Resource_Product_Attribute_Backend_Groupprice_Abstract
The prices are saved in method afterSave from Mage_Catalog_Model_Product_Attribute_Backend_Groupprice_Abstract which calls savePriceData from Mage_Catalog_Model_Resource_Product_Attribute_Backend_Groupprice_Abstract
You could try to override those methods but you might break some Magento stuff. Those prices are tied with the Indexers also. Another option would be to create a separate module, to add another observer on product save, save your data into a separate table and get it back using an extension of Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Price_Group_Abstract

Related

Repopulate Dynamic Dropdown with "set_select" after Form Validation fails in Codeigniter?

I was wondering if it is possible to repopulate a dynamic dropdown(specifically the option of a select) after form validation fails which is generated by another dropdown at on change event.
My jQuery is working good on populating dynamic option of a select as well as at server-side when fetching the data, my only problem is when the form is submitted and validation fails, so basically the dynamic select option/s will reset.
Can somebody help me with this issue?
You have 2 options here:
1) You manually set the 2x select fields with CI and repopulate/set them. You would construct these based on the POST values that were incorrect.
Assuming that list A populates list B which populates list C you may not want to do that. In which case you could define some hidden variables such as this:
var field1 = '<?php $_POST['field1']; ?>';
var field2 = '<?php $_POST['field2']; ?>';
var field3 = '<?php $_POST['field3']; ?>';
$(document).ready(function(){
$('#field1').val(field1).trigger('change'); // or whatever on() query event you use
$('#field2').val(field1).trigger('change'); // or whatever on() query event you use
$('#field3').val(field1).trigger('change'); // or whatever on() query event you use
})
Might help? You would do the trigger to then load whichever lists/ajax calls populate each select box.
Yes it is possible,
this example works with Codeigniter 4 but I'm quite sure it works also with Codeigniter 3. Here I wanted to do a multiple dropdown field with Select2
<div class="form-group">
<label>Categories</label>
<?php
$parameters = array('class' => 'form-control select2_cat', "multiple" => "multiple");
$options = array();
foreach ($categories as $cat) {
$options[$cat['name']] = $cat['name'];
}
echo form_dropdown(
'categories[]',
$options,
set_value('categories[]') != "" ? set_value('categories[]') : 0,
$parameters
);
?>
</div>
You see from the code, I'm using set_value() . I'm checking also if set_value is empty string.
You could find more info here: https://www.py4u.net/discuss/37330

Magento getAttributeName / Text

I'm playing arround with magento product attributes.
What i'm trying to do is getting an "Unique Product Number" attached to the same " Unique Video Number" That works perfectly.
The same goes for an Dropdown Attribute using the following code.
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/avembed.js"></script>
<div class="av_videoplayer" data-av-search-ean="<?php echo $_product->getAttributeText('DropDown_Atribute') ?>"></div>
But as soon as i try it with an TextBox or TextField it fails to return an output.
I have been using getAttributeName to make it happen but i can't seem to get it working or find the anwser to do this.
Any tips on what i might be doing wrong here?
Thanks in advance!
Magento provides magic getters and setters, so for most product attributes, you can retrieve them using the attribute name, preceded by get. For example:
$freeShipping = $_product->getFreeShipping();
The alternative to this is the method you are using, getAttributeText.
$freeShipping = $_product->getAttributeText('free_shipping');
HOWEVER, the attribute must be in the product collection, which most custom attributes are not. When you setup your attribute, you need to set the option Visible on Product View Page on Front-end to Yes.
Failing that, you can recreate the product collection (in your own helper, for example) and add your attribute(s) manually as follows:
public function getMyProductCollection( $skuArray ) {
$products = Mage::getResourceSingleton('catalog/product_collection')
->addAttributeToSelect(
array(
'free_shipping',
'list_price',
'manufacturer',
'name',
'price',
'special_price',
'special_from_date',
'special_to_date',
'thumbnail'
)
)
->addAttributeToFilter(
'sku', array( 'in' => $skuArray )
)
->load();
return $products;
}
You can then use the public function as follows:
$_productObj = $this->helper('my_helper')->getMyProductCollection(
$_productSkus
);
Understand that I'm describing the principle, this code is not copy + pastable. Hope that helps.

x-editable drop down from remote not working

I am using X-editable bootstrap version 2
I am using codeigniter . But the text area and text box is working fine for me but drop down is not working .
in my view
< a href="#" id="contract" data-type="select" data-pk="1" data-source="<?php echo base_url('property/contract_get_update')?>" data-title="Select Contract type" class="" >contract data </a>
$( document ).ready(function() {
$('#contract').editable();
});
in my controller
public function contract_get_update()
{
echo "{'M': 'male', 'F': 'female'}";
}
i just tested with this output not working i tried many tricks but it is not seems to be working.
i want to display details from table name contract how can i get that value as a drop down in x-editable
I answered you on gitHub too. The problem is with your json format.
Try this:
$contract_types = array();
$contract_types[] = array('value' => 'M', 'text' => 'male');
$contract_types[] = array('value' => 'F', 'text' => 'female');
echo json_encode($contract_types);
If you are looping through a db query of results try this in your function:
foreach($results AS $result){
$json_response[] = array('value' => $result['id_column'], 'text' => $result['your_column']);
}
echo json_encode($json_response);
You need to use a custom class with text and value members (case sensitive) to enumerate it as a List of type this class then use it to response after ajax call.

Update area outside of CGridView via Ajax

I'm working with CGridview and updating the grid after an Ajax call. However before I get into the grid I have a a small title and a count $model->itemCount identifying how many records there are. How would I additionally try to update this piece of information along with the grid. Would it be a separate JS call?
Appreciate any help
This is my current code for the grid
'click'=>"function(){
$.fn.yiiGridView.update('item-grid', {
type:'POST',
url:$(this).attr('href'),
success:function(message) {
$('#Ajax-Flash').html(message).fadeIn().animate({opacity: 1.0}, 3000).fadeOut('slow');
$.fn.yiiGridView.update('item-grid');
}
})
return false;
}",
Not sure is this what yoy want but you can use beforeAjaxUpdate() in CgridView as shown below
<?php
$this->widget('zii.widgets.grid.CGridView', array('id'=>'insurance_grid',
'beforeAjaxUpdate'=>"function(id, data){
//Your code to update the title and count
}",
'dataProvider'=>$model->search(),
//'filter'=>$model,
'summaryText' => '', // 1st way
'template' => '{items}{pager}', // 2nd way
'selectableRows' => 2,
'selectionChanged' => "
function(id){
}",
'columns'=>array(array('id'=>'selectedCompanies',
'class'=>'CCheckBoxColumn',
array('name'=>'Agency Name',
'type'=>'raw',
'value'=>'$data->agencyname',
),
),
));
?>

Help with ajax callback and drupal_process_form

I have a form that is added through the nodeapi displayed only on view mode. Users can select an item from a select menu and their choice will automatically get saved to the database with a hook_menu callback on change. If the user has javascript disabled, it'll submit normally with the form api. This is all working fine, but now for security reasons I want to submit the ajax version via the form api too. My form_name_submit is simple like:
function mymodule_test_form_submit($form, &$form_state) {
global $user;
db_query("INSERT INTO {mymodule} (nid, uid, status, created) VALUES (%d, %d, %d, " . time() . ")", $form['#parameters'][2], $user->uid, $form_state['values']['mymodule_status']);
}
My ajax:
$('.mysubmit').css('display', 'none');
$('.myclass').change(function() {
$.ajax({
type: 'POST',
url: Drupal.settings.basePath + 'mymodule/set/' + $nid + '/' + $('.myclass').val(),
dataType: 'json',
data: { 'ajax' : true, 'form_build_id' : $('#mymodule-form input[name=form_build_id]').val() }
});
});
And my callback function:
function mymodule_set($nid, $status) {
$form_build_id = $_POST['form_build_id'];
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form['#post'] = $_POST;
$form['#redirect'] = FALSE;
$form['#programmed'] = FALSE;
$form_state['post'] = $_POST;
drupal_process_form($form_id, $form, $form_state);
}
Originally my callback function was about the same as my submit function, but now I'm trying to use the submit function with ajax as well. Is this the right way to use drupal_process_form? The form is grabbed from the cache, it get's validated and submitted if no errors? I'm using some code from this tutorial to apply to my situation: http://drupal.org/node/331941 There doesn't seem to be any examples of what I'm trying to do. I also have $form['#cache'] = TRUE; in my form function.
How does drupal_process_form compare the submitted values with the original form to check for integrity? Am I supposed to add my values to the form_state since the form state will be empty with ajax. Been stuck on this for a few days, hopefully someone has experience with this.
Thanks.
I had to do somenthing similar to you in the past and read the same tutorial you posted, unfortunately there isn't much information avalaible about this and it was headache for me to make it work. I don't remember well the details but I was taking a look to the code I wrote and here are a couple of suggestions that may work for you:
Do not submit the form using your own jQuery code, instead use the new "#ahah" property of form elements that can be used for calling your callback in the onchange of the select http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#ahah
IF you are doing this in a node form, adding the #ahah property in a form alter may not work, I remember having seen an issue about this that wasn't solved at that moment. if this is the case, use this code for attaching the ahah binding, you'll not need "efect", "method" or "progress" since you just want to submit the form, not to change anything about it:
function YOURMODULE_form_alter(&$form, $form_state, $form_id) {
if ('YOURCONTENTTYPE_node_form' == $form_id) {
//the only way I could make it work for exisiting fields is adding the binding "manually"
$ahah_binding = array(
'url' => url('YOURCALLBACKPATH'),
'event' => 'change',
'wrapper' => 'FIELD-wrapper',
'selector' => '#FIELD',
'effect' => 'fade',
'method' => 'replace',
'progress' => array('type' => 'throbber'),
);
drupal_add_js('misc/jquery.form.js');
drupal_add_js('misc/ahah.js');
drupal_add_js(array('ahah' => array('FIELDd' => $ahah_binding)), 'setting');
//force the form to be cached
$form['#cache'] = TRUE;
}
}
Here is my callback function, note that it has some modifications from the tutorial you posted:
function YOURMODULE_js() {
// The form is generated in an include file which we need to include manually.
include_once 'modules/node/node.pages.inc';
// We're starting in step #3, preparing for #4.
//I have to add the 'rebuild' element, if not an empty node was created
$form_state = array('storage' => NULL, 'submitted' => FALSE, 'rebuild' => TRUE);
$form_build_id = $_POST['form_build_id'];
// Step #4.
$form = form_get_cache($form_build_id, $form_state);
// Preparing for #5.
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// if you want to do any modification to the form values, this is the place
// Step #5.
drupal_process_form($form_id, $form, $form_state);
// Step #6 and #7 and #8.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
// Final rendering callback.
drupal_json(array('status' => TRUE, 'data' => $output));
}
As I said before there are details that I have forgot but maybe this will help you.
Good Luck.

Resources