Magento 1.9 add custom column to associated products grid - magento-1.9

We have an existing customization that appears to have broken when we upgraded from 1.7 to 1.9 community.
The customization adds a column to the associated products grid.
The customization is a local override of
app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Group.php
This was done before I started on the project
$this->addColumn('breakdown_part_no', array(
'header' => Mage::helper('catalog')->__('Part No'),
'name' => 'breakdown_part_no',
'type' => 'varchar',
'index' => 'breakdown_part_no',
'width' => '120px',
'editable' => true,
));
This was added to _prepareColumns()
Another customization was added to method getSelectedGroupedProducts()
public function getSelectedGroupedProducts()
{
$associatedProducts = Mage::registry('current_product')->getTypeInstance(true)
->getAssociatedProducts(Mage::registry('current_product'));
$products = array();
foreach ($associatedProducts as $product) {
$products[$product->getId()] = array(
'qty' => $product->getQty(),
'position' => $product->getPosition(),
'breakdown_part_no' => $product->getBreakdownPartNo(),
);
}
return $products;
}
The behavior is that the column appears in the admin and can be edited, however when saved, it does not save any value.
If I modify the getSelectedGroupedProducts part and set a hard coded value, it displays still no value (blank field), but interestingly if I click save with no value, it saves the value that was hard coded. If I enter any value in the field, it saves as a blank. This is really strange behavior that makes no sense to me.
If I change one of the other fields, such as position to be a hard coded value, it appears instantly and works as expected. Please let me know the proper way for this to work.

There are several posts on various forums about how to do the above and the modification mentioned is true, but what all of the other posts left out was the adminhtml layout input. When a user edits product data in Magento Admin (Associated Products), the data is serialized and sent to the controller save action. I noticed that the fields were not present when a value was entered. This is because the value wasn't in the layout so it was being stripped off of the request before it was posted to the controller.
Add input field in adminhtml/default/default/layout/catalog.xml
adminhtml_catalog_product_supergroup
addColumnInputName

Related

How do I update an eloquent json column from validation data without over writing what's stored in the column?

I'm hoping for a straight-forward solution to do this, but so far I've been coming up empty...
I have a front end vue app/form that sends data back to my laravel backend - I have a controller that validates and saves the request (not looking for feedback on this architecture at the moment unless it actually solves the problem - that's a task for another day...)
I've added a json column called "custom_redeem_fields"
For context, it's to support more flexibility and accepts key/val pairs to use in another field called "custom_redeem_instructions" that has text with delimiters for each of the keys from "custom_redeem_fields", although, I'd prefer to keep from defining these keys statically because the whole point is to be able to add new keys at will. So custom_redeem_instructions will read something like "please visit {•URL•} and enter code {•CODE•}..." and those values will come from the custom_redeem_fields json field.
In the model, I have "custom_redeem_fields" in the fillable array, as well as set as castable to json.
protected $fillable = ['custom_redeem_fields'];
protected $casts = ['custom_redeem_fields' => 'json'];
In the controller, I have ~20 additional columns (not really relevant here, so I've only included two) so I'm trying not to call them out individually beyond their validation rules. The request typically sends one field at a time, so the user can update and save each field as they go. This was working appropriately for all the other fields I had before I added the "custom_redeem_fields.xxxx" to the mix.
$validatedData = $request->validate([
'title' => 'sometimes|required|max:255',
'text' => 'sometimes|required_unless:redeem_type,9|max:255',
'custom_redeem_fields.email' => 'sometimes|email',
'custom_redeem_fields.phone' => ['sometimes', new ValidPhone],
'custom_redeem_fields.code' => 'sometimes',
'custom_redeem_fields.url' => 'sometimes|url'
]);
$ticket = Ticket::find($id)
$ticket->update($validatedData);
Now, with the "custom_redeem_fields.xxxxx" this falls apart - the entire json object stored in "custom_redeem_fields" is overwritten with the most recent update, rather than just updating the key included in the validatedData array. So if I save:
[
"title" => "Monty Pythons Flying Circus"
"text" => "Monty Pythons Flying Circus is a British surreal sketch comedy series created by and starring the comedy group Monty Python, consisting of Graham Chapman, ..."
"custom_redeem_fields" => [
"email" => "bob#example.com",
"phone" => "503.555.5555",
"code" => "1xoicvjq",
"url" => "https://example.com/"
]
]
and then I send:
"custom_redeem_fields" => ["email" => "pat#example.com"]
the custom redeem fields returns:
"custom_redeem_fields" => ["email" => "pat#example.com"]
rather than:
"custom_redeem_fields" => ["email" => "pat#example.com", "phone" => "503.555.5555", "code" => "1xoicvjq", "url" => "https://example.com/"]
It seems that validation rules need json keys to be notated with dot syntax (custom_redeem_fields.url), and eloquent needs arrow syntax (custom_redeem_fields->url), but I'm not sure what's the most straightforward way to transition between the two, which seems very not-laravel, and the documentation is certainly lacking in this department...
Any help would be appreciated.
Thanks!
Wouldn't array_merge() solve your problem, it would overwrite values you provide with the second parameter. If you give it the already existing ones as the first, it would combine the two as you want.
$customRedeemInput = [...];
$model->custom_redeem_fields = array_merge($model->custom_redeem_fields, $customRedeemInput);
$model->save();

yii2 formwizard checkbox field value set to 1 even if not selected

I am using yii2-formwizard and I want to insert a checkbox as form input field for the field is_legal in a tabular step. So in fieldConfig array, reading the documentation, I inserted the following code:
'is_legal' => [
'options' => [
'type' => 'checkbox',
'template' => '{input}{beginLabel}{labelTitle}{endLabel}{error}{hint}',
],
'labelOptions' => ['label' => \Yii::t('app', 'Legal Representative')],
],
If I select the checkbox or not the value of the field is always 1 as shown on: .
However, when I add another instance of the model, in the preview step I have NA as value of the legal representative field :
Yes, you are correct about it. It incorrectly shows the value even if the check box is not checked i have updated the section and added a fix.
//check if single checkbox input
if (inputType.attr("type") == 'checkbox') {
return inputType.is(":checked") ? inputType.val() : '';
}
To get the latest code you need to repeat the steps for running composer using,
composer update
and clear the browser cache along with clearing the assets folder in the web directory.
Normally when i am working locally with extensions or if there is an update for an extension which includes javascript updates i add the following settings under the components array in my local config file that takes care of getting the latest files from, and the assets are force copied every time to the web/assets/ directory whenever you refresh the browser or the page reloads.
'components'=>[
'assetManager' => [
'forceCopy' => true,
],
]
Note: Dont leave it open on live site as it would make the page load slower.

Transactional Emails - Default Styling and Variables in Magento 1.9

I'm looking for 2 specific variables.
Wishlist - The var_message variable has some styling to it that im trying to edit.
Abandoned Carts - pulls on this extension URL : connector/email/basket/code/secret/quote_id/*****
And im unable to find the location of the file that is accessed by that URL or command.
Any assistance that can be provided would be greatly appreciated.
Also if someone could tell me how i might trace the locations of these things without "Just knowing" that would be grand too.
Kind Regards,
the correct variable name is message (not var_message)
variable message is populated in controller Mage_Wishlist_IndexController
inside method sendAction
here it is:
$emailModel = Mage::getModel('core/email_template');
$sharingCode = $wishlist->getSharingCode();
foreach ($emails as $email) {
$emailModel->sendTransactional(
Mage::getStoreConfig('wishlist/email/email_template'),
Mage::getStoreConfig('wishlist/email/email_identity'),
$email,
null,
array(
'customer' => $customer,
'salable' => $wishlist->isSalable() ? 'yes' : '',
'items' => $wishlistBlock,
'addAllLink' => Mage::getUrl('*/shared/allcart', array('code' => $sharingCode)),
'viewOnSiteLink' => Mage::getUrl('*/shared/index', array('code' => $sharingCode)),
'message' => $message
)
);
}
$wishlist->setShared(1);
$wishlist->save();
and the actual content of the message comes from a form input and gets fetched over here:
$message = nl2br(htmlspecialchars((string) $this->getRequest()->getPost('message')));
there is no actual styling or css assigned to it. In fact most of styles are defined inline in email templates

Sylius New Field on Taxon Forms not Saving

I've added a new field to the Taxon entity, and have the new field showing up on the form on the edit Taxon page in the admin panel. My problem is that I can't get my new field to save to the Taxon. I receive a success message, but only existing Taxon fields save, while my new field clears. Here's my code (note I'm making changes to Sylius core rather than extending, as proof of concept. Once I have this working, I'll extend properly):
src/Sylius/Bundle/TaxonomyBundle/Form/Type/TaxonType.php:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('translations', 'sylius_translations', [
'type' => 'sylius_taxon_translation',
'label' => 'sylius.form.taxon.name',
])
->add('my_new_field', 'text', [
'label' => 'My New Field',
'required' => false
])
->addEventSubscriber(new AddCodeFormSubscriber())
->addEventSubscriber(new BuildTaxonFormSubscriber($builder->getFormFactory())
);
}
src/Sylius/Component/Core/Model/Taxon.php:
protected $my_new_field;
and
public function getMyNewField() {
return $this->my_new_field;
}
public function setMyNewField($myNewField) {
$this->my_new_field = $myNewField;
}
After updating the Taxon model, I ran doctrine:diff and then ran the migration to add my new field to the model. I can confirm, the field was added to the database.
The field also shows up on Taxons now, on the edit screen. I can input text into the field and it posts correctly, but does not ever save to the taxon.
I would think I'm missing some controller logic, but it seems that taxons go through the standard ResourceController's updateAction() and I haven't been able to figure out how to make this aware of the new field which needs to be saved.
You're probably missing ORM mapping in Taxon.orm.xml file.
Remember:
if you've added your properties on Sylius\Component\Taxonomy\Model\Taxon you have to update src/Sylius/Bundle/TaxonomyBundle/Resources/config/doctrine/model/Taxon.orm.xml.
if you've added your properties on Sylius\Component\Core\Model\Taxon you have to update src/Sylius/Bundle/CoreBundle/Resources/config/doctrine/model/Taxon.orm.xml
It's also probably better to extend Taxon model from Core component.

UPDATED: Magento add customer attribute filter to order grid

I have extended the Mage_Adminhtml_Block_Sales_Order_Grid class with a custom module to add several customer attributes (Magento EE 1.10) to the grid.
Two of the attributes I added are text fields (i.e. they live in the customer_entity_varchar table, and I was able to add them to the collection and display them in the grid. So far so good.
A third attribute is a select, so the values live in the customer_entity_int, the eav_attribute_option and the eav_attribute_option_value tables. I added the necessary values to the collection (using $collection->getSelect()->joinLeft(.....). Again, so far so good.
My problem is being able to display and filter the attribute at the same time.
Inside the _prepareColumns() function in my MyCompany_MyModule_Block_Adminhtml_Order_Grid class, if I add a column like this, - as expected - I can display the values of the attribute on each row, but I don't get a drop down filter in the header:
protected function _prepareColumns()
{
...
$this->addColumn('bureau', array(
'header' => Mage::helper('sales')->__('Bureau'),
'index' => 'bureau',
'type' => 'text'
));
...
}
Following the example of status, and adding the column like this, gives me the drop down filter in the header, but it no longer displays the values for the attribute in each row:
protected function _prepareColumns()
{
...
$this->addColumn('bureau', array(
'header' => Mage::helper('sales')->__('Bureau'),
'index' => 'bureau',
'type' => 'options',
'options' => $this->_getBureauOptions(),
'filter_index' => 'value_option_table.option_id'
));
...
}
protected function _getBureauOptions()
{
$bureau = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('bureau')
->getFirstItem();
$bureauOptions = $bureau->getSource()->getAllOptions(false);
$optionsArr = array();
foreach ($bureauOptions as $option) {
$optionsArr[$option['value']] = $option['label'];
}
return $optionsArr;
}
Any advice / explanation would be much appreciated.
UPDATE:
It turns out that my code also causes a SQL error in a multi-website environment when an admin user only has permissions for some websites:
"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'store_id' in where clause is ambiguous"
#clockworkgeek had the answer to the first part of my question.
The problem was that my joinLeft() was retrieving text values from the attribute options, while I should have been retrieving integer values when using 'type => 'options'.
Once I changed my joinLeft() to only retrieve integer values from customer_entity_int (actually a simpler join), the filtering and display worked flawlessly - thank you sir.
I will re-post my second issue (about SQL errors caused by permissions) as a separate question.