I have created a custom module which displays a tab and a section in admin configuration panel to manage customer attributes.
I have loaded all customer attributes with a check box each.
This is my code for displaying all customer attributes as checkboxes. I want the checkbox value selected from here to be added as a column in Manage Customer Grid.
Model/Attributes.php
$attributes = Mage::getModel('customer/entity_address_attribute_collection');
$result = array();
foreach ($attributes as $attribute)
{
if (($label = $attribute->getFrontendLabel()))
$result[$attribute->getId()] = $label;
}
$attributes1 = Mage::getModel('customer/entity_attribute_collection');
$result1 = array();
foreach ($attributes1 as $attribute1)
{
if (($label1 = $attribute1->getFrontendLabel()))
$result1[$attribute1->getId()] = $label1;
}
$final = array_merge($result, $result1);
return $final;
Now based on selection of these check boxes, I would like to add an extra column in 'Manage Customer' Grid.
I tried retrieving the value of selected checkbox but I just get the array index number.
Mage::getStoreConfig('sectionname/groupname/fieldname');
Can some one tell me how to fetch the the selected checkbox value and add a column based on the selection represented by that checkbox?
Thanks in advance.
When you use array_merge you are destroying the correct indexes, which are supposed to be the attribute IDs. Also it's good practice to give your variables meaningful names.
$result = array();
$addressAttributes = Mage::getModel('customer/entity_address_attribute_collection');
foreach ($addressAttributes as $addressAttribute)
{
if (($addrlabel = $addressAttribute->getFrontendLabel()))
$result[$addressAttribute->getId()] = $addrlabel;
}
$customerAttributes = Mage::getModel('customer/entity_attribute_collection');
foreach ($customerAttributes as $customerAttribute)
{
if (($custlabel = $customerAttribute->getFrontendLabel()))
$result[$customerAttribute->getId()] = $custlabel;
}
return $result;
I suppose the next step is to remove the columns that your grid's parent will add, these are stored in a grid's protected _columns property. Not all columns are to be removed, such as the massaction column. Then add your chosen columns back in.
protected function _prepareColumns()
{
parent::_prepareColumns();
// remove the excess columns here
$attributeIds = Mage::getStoreConfig('sectionname/groupname/fieldname');
$attributes = Mage::getModel('eav/entity_attribute')->getCollection()
->addFieldToFilter('attribute_id', array('in' => $attributeIds));
foreach ($attributes as $attribute)
{
$options = array();
if ($attribute->getFrontendInput() == 'select') {
foreach ($attribute->getSource()->getAllOptions() as $value) {
$options[$value['value']] = $value['label'];
}
}
$this->addColumn($attribute->getCode(), array(
'index' => $attribute->getCode(),
'header' => $attribute->getFrontendLabel(),
'type' => $attribute->getFrontendInput(),
'options' => $options
));
}
return $this;
}
This way may lose useful formatting like column widths, etc. so a more sophisticated way would be to work out which columns are already in place and leave them, then only remove those that haven't been selected.
I would, in your module, set in config.xml that you are overwrting the Block Mage_Adminhtml_Block_Customer_Grid with your own block (which inherits from Mage_Adminhtml_Block_Customer_Grid) and in your own block make a function
protected function _prepareColumns()
{
$this->addColumn('mycolumn', array(
'header' => Mage::helper('customer')->__('My Column'),
'index' => 'key',
));
return parent::_prepareColumns();
}
Without knowing more about your data its hard to give better advice, but this should be enough to get you started.
Related
I have a form (queform plugin on wordpress) that I am trying to populate based on some table information.
Initially I had a shortcode plugged in which is running the query and puts the results in a comma separated list - However, when using the drop down all of the table tags were being displayed, so I thought that I need to query in place of the shortcode.
I think I am close, but missing something. If I use concat the drop down displays the number 1. If I use group_concat the drop down displays the number 15 (which is how many rows there are on the table).
<?php
add_action('quform_pre_display_7', function (Quform_Form $form) {
$element = $form->getElement('quform_7_3');
if ($element instanceof Quform_Element_Multi) {
$options = array();
global $wpdb;
$results = $wpdb->query("SELECT CONCAT(Name) FROM `NAME_TABLE`");
$results = array_map('trim', explode(',', $results));
foreach ($results as $index => $option) {
$options[] = array('label' => $option, 'value' => $option, 'id' => $index + 1);
}
$element->setOptions($options);
}
});
I figured out all that was needed was to change it from "query" to "get_var" and it set the menu options.
I try to add multiple custom values to my product options while adding the product to cart. It works fine but the problem is that it loops through all options and stores only the last option to the $optionData array.
protected $customOption;
\Magento\Catalog\Api\Data\CustomOptionInterface $customOption
$this->customOption = $customOption;
Execute
$customOptionInterface = $this->customOption;
$optionData = array();
foreach ($product->getOptions() as $o) {
$optionKeyIndex = array_search($o->getTitle(), array_column($optionsArray, 'name'));
$optionData[] = $customOptionInterface->setdata(array(
"option_id" => $o->getId(),
"option_value" => "TEST"
));
}
$productOption = $this->productOption;
$extAttribute = $productOption->getExtensionAttributes();
$extAttribute->setCustomOptions($optionData);
$productOption->setExtensionAttributes($extAttribute);
$cartItem->setProductOption($productOption);
$newItem = $this->itemRepository->save($cartItem);
The reason for it has to be the "$customOptionInterface->setdata". Without it all individual array will be added to $optionData but of course I can't use it this way. Any idea what's wrong?
I appreciate your help :)
I want to apply search filters in my project. I have options tables where options are being saved with the option values with parent id of option id. For example brand is saving as option with parent id set to 0 and all brands have brand id as their parent id set and while saving product I am saving product options in product_options table. Now i want to apply filters in product listing page. I am using following code for filtration:
$conditions = array();
$product_options = $this->ProductOption->find('list',array('fields'=>array('product_id'),'conditions'=>array('ProductOption.option_value_id'=>$data['data']['options'])));
$conditions = array_merge($conditions,array('Product.id'=>array_unique($product_options)));
$prod_info = $this->paginate('Product',$conditions);
$this->set(compact('prod_info'));
When I search any product with their brand name it works fine but if I try to search with the price (also an option) then it gives other brand products also which have price equal to filter price option. Please check following link to understand problem correctly.
http://primemart.in/Food-Processors-Ii4zRGAKYAo=
Please anyone help me to come out my problem.
Thanks.
Please have a look on my code which I used to pass conditions in and to get results
$product_options = $this->ProductOption->find('list',array(
'fields'=>array('product_id'),
'conditions'=>array('ProductOption.option_value_id'=>$data['data']['options'])
));
//$this->Option->unBindModel(array('belongsTo'=>'Product'));
$product_options = $this->Option->find('all', array(
'conditions'=>array('Option.id'=>$data['data']['options'])
));
//pr($product_options);
$opt_arr = array();
foreach ($product_options as $op) {
$opt_arr[$op['Option']['parent_id']][] = $op['Option']['id'];
}
$conditions_arr = array();
foreach($opt_arr as $opt) {
$key_arr = array();
foreach($opt as $op) {
$key_arr['OR']['ProductOption.option_value_id'][] = $op;
}
$conditions_arr['AND'][] = $key_arr;
}
$pr_options = $this->ProductOption->find('list', array(
'conditions'=>$conditions_arr,
'fields'=>array('product_id')
));
$conditions = array_merge($conditions, array('Product.id'=>array_unique($pr_options)));
I would try code bellow. I assume that $conditions constist of the other conditions you mention in your question.
$conditions = ... // other conditions you mentioned
$conditions = array('AND'=>array($conditions, array('Product.id'=>array_unique($product_options))));
$prod_info = $this->paginate('Product',$conditions);
I got a marker with a category, say : "luggage" i would like to know what parent categories is luggage to set a specific marker on a map.
I created a top categories list of each category i need to display. Something like :
$education = [ "adultedu", "collegecounseling", "collegeuniv", "educationservices", "elementaryschools", "highs|chools", "preschools", "privatetutors", "religiousschools", "specialed", "specialtyschools", "artschools", "cprclasses", "cookingschools", "cosmetology_schools"]
I got the same lists for transports, shopping, health etc...
What is the best way to search in which category my keyword is from ?
Use an array and iterate through while using in_array:
$arrays = array(
'education' => &$education,
'transport' => &$transport,
'shopping' => &$shopping,
'health' => &$health
);
$searchingFor = 'foo';
$searchingForCategory = null;
foreach ($arrays as $category => $array) {
if (in_array($searchingFor, $array)) {
$searchingForCategory = $category;
break;
}
}
var_dump($searchingForCategory); //education / transport / shopping / health / NULL
You have to check each array with in_array() function.
if (in_array('luggage', $education)) {
// found
}
Also you can add all top categories to one array, lets say $topCategories ;)
Then you can check all with simple loop:
$foundInCategory = '';
foreach ($topCategories as $name => $subCategories) {
if (in_array('luggage', $subCategories)) {
$foundInCategory = $name;
break;
}
}
It'll add category name in which subcategory was found to variable $foundInCategory.
I am looking to change the order in which a list is being brought together in our Magento cart.
We have the following coding:
public function getOptionList()
{
$options = false;
if (Mage::getStoreConfig('SCP_options/cart/show_custom_options')) {
$options = parent::getOptionList();
}
if (Mage::getStoreConfig('SCP_options/cart/show_config_product_options')) {
if ($this->getConfigurableProductParentId()) {
$attributes = $this->getConfigurableProductParent()
->getTypeInstance()
->getUsedProductAttributes();
foreach($attributes as $attribute) {
$options[] = array(
'label' => $attribute->getFrontendLabel(),
'value' => $this->getProduct()->getAttributeText($attribute->getAttributeCode()),
'option_id' => $attribute->getId(),
);
}
}
}
return $options;
}
Which displays the following: http://awesomescreenshot.com/053ffeie9
We are looking to swap the Attributes and Options over so the Attributes display at the top of the item.
In the coding we've established that the top if statement is for options and the bottom if statement is for the attributes, so theory being it should just be a case of swapping them over... unfortunately it doesn't seem as simple as that.
If we swap them the attributes don't show but the options do.
Would someone be able to recommend anything to try as we're getting very frustrated trying to sort this.
swap them and then use array_merge on the bottom half. When you swap them, the second part was just assigning options and wiping out the attributes part. This puts the two together instead of overwriting:
if (Mage::getStoreConfig('SCP_options/cart/show_custom_options')) {
$options = array_merge($options, parent::getOptionList();
}