ajax call in magento 2 - ajax

I have created a custom module in Magento2 where currently I'm selecting a customer and entered multiple order numbers, it will saved into a custom table in the database.
Currently I have to entered order numbers in a textarea but now I want to display all orders after choosing the customer which are not in Complete status.
From this section admin choose orders by clicking on the right side checkbox and Saved it.
Can anyone help me to solve this problem?
Existing Module’s Layout
Want to modify the above layout to the following one: In this desired system when admin change the customer name order numbers associated with that customer will be listed.
New invoice
Code I have used to create the customer dropdown field:
$fieldset->addField('customer_id', 'select', array(
'label' => __('Customer'),
'name' => 'customer_id',
'title' => __('Customer'),
'values' => $this->getCustomerOptionArray()
));
protected function getCustomerOptionArray()
{
$options = array();
$options[] = array(
'value' => 0,
'label' => __('Select Customer'),
);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')->getCollection();
foreach ($customerObj as $customerObjdata ){
$options[] = array(
'value' => $customerObjdata->getId(),
'label' => $customerObjdata->getName(),
);
}
return $options;
}

Related

Magento How to add custom field into Role Creation Form and save its value into database

I have a requirement to add an extra field on to the Role Creation form.
I was able to do by 2 ways
- Using Event Observer
- Or by rewriting blocks
But I want to save this field into the data base and want to populate this value on edit role page.
I tried -
$fieldset = $form->addFieldset('navigator_information', array(
'legend' => 'Navigator Information',
'class' => 'fieldset-wide'
)
);
$options = array();
$options['client'] = 'client';
$options['report'] = 'report';
//add new field
$fieldset->addField('navigator_role', 'select',
array(
'name' => 'navigator_role',
'label' => Mage::helper('adminhtml')->__('Navigator Roles'),
'id' => 'navigator_role',
'class' => 'required-entry',
'values' => $options,
'required' => true,
)
);
and it shows me the new fieldset and new field but How to store this value
into db?
Any help would be appreciated.
I have uploaded screen shot that shows new field

I want to add products in cart with custom labels on cart page

I have tried several methods to add products to cart programatically but i want to show the quote item labels as well one cart page. Can anyone help me to do this or any references?
You can use the below code in your observer.
$item = ( $item->getParentItem() ? $item->getParentItem() : $item);
$additionalOptions = array(array(
'code' => 'option_code',
'label' => 'Some_option_Label',
'value' => 'Option_Value'
));
$item->addOption(array(
'code' => 'additional_options',
'value' => serialize($additionalOptions),
));
// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);
here is the code you are looking for;)
<?php
$loadProductData = Mage::getModel('catalog/product')->load($productId);
$quote = Mage::getSingleton('checkout/session')->getQuote();
$OrderquoteItem = Mage::getModel('sales/quote_item');
$quoteItem = $OrderquoteItem->setProduct($loadProductData);
//custom options to show user on cart page
$a_options = array(
'options' => array(
'label' => 'OptionLabel :',
'value' => "OptionaValue",
));
//add above options array to this cart item which is going to get added on cart
$quoteItem->addOption(array(
'code' => 'additional_options',
'value' => serialize($a_options),
));
// set price and quantity
$quoteItem->setQuote($quote)
->setQty($productOptions['qty'])
->setOriginalCustomPrice($productOptions['price'])
->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

Not saving data when add multiple select attribute in product grid

I have created one custom module with add associated products concept. Created successfully. And Its working well.
But when i add "Multi select attribute" column in product grid with that option values, That entity value not saved.
If i removed that option values from that brand attribute drop down, Its saving fine.
I have shown my code below what i did for add multi select attribute column in product grid
under _prepareColumns() method
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'brand'); // attribute code here
foreach ( $attribute->getSource()->getAllOptions(true, true) as $option)
{
if($option['value'] != '')
$valArr[$option['value']] = $option['label'];
}
$this->addColumn('brand', array(
'header'=> Mage::helper('catalog')->__('Brand'),
'align' => 'left',
'index' => 'brand',
'type' => 'options',
'options' => $valArr,
'renderer' => 'Mage_Adminhtml_Block_Catalog_Product_Renderer_Brands', // Will have to create the renderer.
'filter_condition_callback' => array($this, '_filterBrandCondition')
));
When i hide 'options' => $valArr, , All are working fine.
I can't able to understand, why its happening. Please suggest me your ideas. Thanks in advance.
Have you already created the _filterBrandCondition function ?
What Mage_Adminhtml_Block_Catalog_Product_Renderer_Brands look like ?

Add my custom attribute to customer object in Magento (1.7.x)

I have created an custom attribute using the Magento installer script below:
$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->endSetup();
On the onepage checkout screen I have a dropdown where a customer selects an organisation from a dropdown menu - once they click the 'Continue' button it posts an ajax request and goes to the next step 'Billing' e.g the post request looks like this..
org[organisation_id] - 12345
org[name] - ACME Inc
My function that deals with this is follows - can anyone assist how to add this to the customer & order object?
public function saveOrgAction()
{
if ($this->_expireAjax()) {
return;
}
if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost('org', array());
// todo:hook up saving the org id passed here to customer & order object
// UPDATE... I believe this is correct?
$customer = Mage::getSingleton('customer/session')->getCustomer(); // get customer object
$customer->setOrganisationId($org_id)->save();
if (!isset($result['error'])) {
$result['goto_section'] = 'billing';
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
What is the best way to add this 'organisation_id' attribute to the customer object (note I am using Magento 1.7.2)
$customer->setOrganisationId('123')->save();
if you need it in the order you have to set it in the current quote object
via
$quote->setOrganisationId('123');
and use this explanation to push the attribute automatically through quote to order conversion. But dont forget to create the correct labled column in sales_flat_order!
explanation how to push any attribute automatically through quote to order conversion

Magento: only existing countries in drop-down filter for "Country" column in Grid View

I have created a custom Grid View with the "Country" column:
$this->addColumn('ship_country', array(
'header' => $this->__('Country'),
'index' => 'countrycode',
'type' => 'country'
));
The collection list which is displayed in the grid has only 2 countries: Ireland and UK.
Problem: the Filter Header for country column shows the drop-down list with all possible, over 200, countries stored inside Magento.
Question: is it possible to force filter to show only Ireland and UK in the drop-down?
Like this:
I have solved this problem with the following code:
$this->addColumn('ship_country', array(
'header' => $this->__('Country'),
'index' => 'countrycode',
'type' => 'options',
'options' => Mage::helper('mymodule')->getCountries(),
));
Where Helper getCountries() method looks something like this:
public function getCountries() {
$collection = Mage::getModel('mymodule/entity')->getCollection();
$collection->getSelect()->group('countrycode');
$countries = array();
foreach($collection as $item)
$countries[$item->getcountrycode()] = Mage::getModel('directory/country')->load($item->getcountrycode())->getName();
return $countries;
}

Resources