Reverse Name search using Kanavan Autosearch in Magento 1.7 - magento

I am using karavan autosearch extention for magento1.7. and I want to modify the searching technique. In that search technique if we provide a full or partial name, the search engine works perfectly. but I want it works for reverse name. I mean If the exact name is 'test product', then if I use 'product test', that result will show same product in dropdown product list, what is now became empty. I have debug it and found that this search engine also using magento default search technique.
Any kind of Idea is acceptable.Please help me..
Thanks in advance..

Karavan extension uses default Magento search Model as backbone
app\code\local\Mage\CatalogSearch\Model\Resource\Search\collection.php
Find method _getSearchEntityIdsSql() and change there as u required.
$words = array();
if(str_word_count($this->_searchQuery)>1){
$words = explode(" ",$this->_searchQuery);
}
$ifValueId = $this->getConnection()->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
foreach ($tables as $table => $attributeIds) {
foreach($words as $word){
$selects[] = $this->getConnection()->select()
->from(array('t1' => $table), 'entity_id')
->joinLeft(
array('t2' => $table),
$this->getConnection()->quoteInto(
't1.entity_id = t2.entity_id AND t1.attribute_id = t2.attribute_id AND t2.store_id = ?',
$this->getStoreId()),
array()
)
->where('t1.attribute_id IN (?)', $attributeIds)
->where('t1.store_id = ?', 0)
->where($resHelper->getCILike($ifValueId, $word, $likeOptions));
}
if ($selects) {
$likeCond = '(' . join(' and ', $selects) . ')';
}
}
Some what like this.
Note: DOnt overwrite BAse Class

Related

How to customize product URL?

How to customize the product URL to add certain attribute such as EAN on URL part? For example I want make url like `[domain]/cars/[sku]/tata-suv.html
Thanks
So, I am gonna be short and quick here.
1) You have to customize Mage_Catalog_Model_Url::getProductRequestPath. You have add following statements.
$sku = $product->getSku();
$requestPath = 'cars/' . $sku . "/" . $requestPath;
May be Add this just before following line:
if (strlen($requestPath) > self::MAX_REQUEST_PATH_LENGTH + self::ALLOWED_REQUEST_PATH_OVERFLOW) {
$requestPath = substr($requestPath, 0, self::MAX_REQUEST_PATH_LENGTH);
}
2) Customize the collection Mage_Catalog_Model_Resource_Url::_getProducts add sku in select field list:
$select = $adapter->select()
->useStraightJoin(true)
->from(array('e' => $this->getTable('catalog/product')), array('entity_id', 'sku'))
->join(
array('w' => $this->getTable('catalog/product_website')),
'e.entity_id = w.product_id AND w.website_id = :website_id',
array()
)
->where('e.entity_id > :entity_id')
->order('e.entity_id')
->limit($this->_productLimit);
Notice, sku in array('entity_id', 'sku')

Magento Attribute Values - add description field

Looking for a way to add a description field to invididual attribute values in Magento. Please note I'm referring to Attribute Value options, not the actual Attribute itself.
As an example:
Attribute = colour
Attribute values:
Red, Green, Blue
I want to add a description field for each of the 3 colours (1 for Red, 1 for Green, 1 for Blue). The purpose of this is to show a tooltip on the frontend to give more information about each colour option.
Does anyone know how to do this? There are lots of solutions round which apply to the Attribute itself (colour) but not the individual options (Red, Green, Blue).
The descriptions should be editable from within the Admin panel. I don't want a solution which relies on editing these straight in the database using, for example, phpMyAdmin.
I understand that the values are stored in the 'eav_attribute_option_value' table and that a further column may be needed to store the Description. No idea how to get all that set up in the Admin panel. Ideas?
EDIT: I've added a screenshot of where the Description text would need adding. So next to each colour (on the screenshot: Black, Blue, Green, Grey, Red, White, etc) - each one needs to have a description next to it.
This maybe 11 months out of date, but for anyone else having this problem then perhaps this can assist you. I hope it will save you from the hours of head banging against a wall like myself and my colleague experienced. For my purpose, I was trying to create an Image URL field for Magento version 1.9 to support a product migration from an older platform.
This answer has been partially answered here - Creating new options for Magento attributes but there are some extra things I had to figure out:
1.) This answer assumes you have created your own module (if you do not know how then start here: http://www.smashingmagazine.com/2012/03/01/basics-creating-magento-module/)
2.) It also leaves you to create the extra fields in catalog/product/attribute/options.phtml yourself. But to save you time here are the amendments I made to to get it to appear in the admin. Create a new table head option on line 88:
<th><?php echo Mage::helper('catalog')->__('YOUR_ATTRIBUTE_NAME_HERE') ?></th>
Next create a new td on line 101:
<td class="a-left"><input class="input-text" type="text" name="option[YOUR_ATTRIBUTE_NAME_HERE][{{id}}]" value="{{TABLE_COLUMN_NAME_HERE}}" <?php if ($this->getReadOnly()):?> disabled="disabled"<?php endif;?>/></td>
And also, most of the logic is done in Javascript so we need to replicate the field here on line 126:
'<td><input class="input-text" type="text" name="option[YOUR_ATTRIBUTE_NAME_HERE][{{id}}]" value="{{TABLE_COLUMN_NAME_HERE}}" <?php if ($this->getReadOnly()):?> disabled="disabled"<?php endif;?>/><\/td>'+
3.) The longest part for me was creating the custom logic for _saveOption method. I overrode the parent class, but to save you the trouble here is my logic:
protected function _saveOption(Mage_Core_Model_Abstract $object)
{
$option = $object->getOption();
if (is_array($option)) {
$adapter = $this->_getWriteAdapter();
$optionTable = $this->getTable('eav/attribute_option');
$optionValueTable = $this->getTable('eav/attribute_option_value');
$stores = Mage::app()->getStores(true);
if (isset($option['value'])) {
$attributeDefaultValue = array();
if (!is_array($object->getDefault())) {
$object->setDefault(array());
}
foreach ($option['value'] as $optionId => $values) {
$intOptionId = (int) $optionId;
if (!empty($option['delete'][$optionId])) {
if ($intOptionId) {
$adapter->delete($optionTable, array('option_id = ?' => $intOptionId));
}
continue;
}
$sortOrder = !empty($option['order'][$optionId]) ? $option['order'][$optionId] : 0;
$imgUrl = !empty($option['image_url'][$optionId]) ? $option['image_url'][$optionId] : 0;
if (!$intOptionId) {
$data = array(
'attribute_id' => $object->getId(),
'sort_order' => $sortOrder,
'image_url' => $imgUrl
);
$adapter->insert($optionTable, $data);
$intOptionId = $adapter->lastInsertId($optionTable);
} else {
$data = array('sort_order' => $sortOrder, 'image_url' => $imgUrl);
$where = array('option_id =?' => $intOptionId);
$adapter->update($optionTable, $data, $where);
}
if (in_array($optionId, $object->getDefault())) {
if ($object->getFrontendInput() == 'multiselect') {
$attributeDefaultValue[] = $intOptionId;
} elseif ($object->getFrontendInput() == 'select') {
$attributeDefaultValue = array($intOptionId);
}
}
// Default value
if (!isset($values[0])) {
Mage::throwException(Mage::helper('eav')->__('Default option value is not defined'));
}
$adapter->delete($optionValueTable, array('option_id =?' => $intOptionId));
foreach ($stores as $store) {
if (isset($values[$store->getId()])
&& (!empty($values[$store->getId()])
|| $values[$store->getId()] == "0")
) {
$data = array(
'option_id' => $intOptionId,
'store_id' => $store->getId(),
'value' => $values[$store->getId()]
);
$adapter->insert($optionValueTable, $data);
}
}
}
$bind = array('default_value' => implode(',', $attributeDefaultValue));
$where = array('attribute_id =?' => $object->getId());
$adapter->update($this->getMainTable(), $bind, $where);
}
}
return $this;
}
My custom field was named image_url so I added it to the $data variable to be inserted. This will insert values into the column "image_url" of the eav_attribute_option table, but you can manipulate it to store in eav_attribute_option_value in the same method.
4.) For some reason that stack overflow post stated that this _saveOption method would be fired on save but mine was not, therefore I also overrode the _afterSave method in the same class which looks like this:
protected function _afterSave(Mage_Core_Model_Abstract $object)
{
$this->_clearUselessAttributeValues($object);
$this->_saveStoreLabels($object)
->_saveAdditionalAttributeData($object)
->saveInSetIncluding($object)
->_saveOption($object);
return $this;
}
5.) Now it will attempt to save your new value. But it will cause an error since your custom table column most likely doesn't exist yet. You are welcome to create this manually if it is appropriate for you. Unfortunately I needed to create this programmatically for my situation, so for those of you in the same boat (this is a slightly unclean approach) but for speed I re-routed the app/code/core/Mage/Core/Model/Resource/Setup.php by creating the local revision here: app/code/local/Mage/Core/Model/Resource/Setup.php and add this to line 154 in the constructor class:
$installer = $this;
$installer->getConnection()->addColumn($installer->getTable('eav/attribute_option'), 'YOUR_COLUMN_NAME_HERE', 'VARCHAR(256) NULL');
$installer->endSetup();
6.) Okay, everything should now be saving to the database, but we still need to read the value into our <td> - this had me stumped for a while, but figured out that the Javascript is responsible for replacing the {{id}} and {{sort_order}} tags in the HTML on line 230. Therefore we need to add our new column to this getOptionsValues() method. I added the following code in on line 70 of catalog/product/attribute/options.phtml:
<?php foreach ($this->getOptionValues() as &$val) {
$imgUrl = $this->getImageUrl($val->id);
if ($imgUrl != "0") {
$val->_data["YOUR_TABLE_COLUMN_NAME_HERE"] = $imgUrl;
}
} ?>
Then, in your YOUR_MODULE_Block_Adminhtml_Options class add the method getImageUrl() that the above calls:
/**
* Retrieve results from custom column
*
* #return Mage_Core_Model_Mysql4_Store_Collection
*/
public function getImageUrl($option_id)
{
//Get the resource model
$resource = Mage::getSingleton('core/resource');
//Retrieve the read connection
$readConnection = $resource->getConnection('core_read');
//Retrieve our table name
$table = $resource->getTableName('eav/attribute_option');
$query = 'SELECT ' . $this->custom_col . ' FROM ' . $table . ' WHERE option_id = '
. (int)$option_id . ' LIMIT 1';
//Execute the query and store the result
$imgUrl = $readConnection->fetchOne($query);
return $imgUrl;
}
And there you have it. I really hope that this helps anyone in a similar situation.
Try this out
By default Magneto only provide custom options without any description, if you want to customize with description then you must change in following files:
Step 1:-
In File
app\design\adminhtml\default\default\template\catalog\product\edit\option\type\select.phtml
Find the below code:
'<th class="type-sku"><?php echo Mage::helper('catalog')->__('SKU') ?></th>'+
Add these after just after
'<th class="type-description"><?php echo Mage::helper('catalog')->__('Description') ?></th>'+
Find the below code:
'<td><input type="text" class="input-text" name="product[options][{{id}}][values][{{select_id}}][sku]" value="{{sku}}"></td>'+
Add these after just after
'<td><input type="text" class="input-text" name="product[options][{{id}}][values][{{select_id}}][description]" value="{{description}}"></td>'+
Step 2:-
In File
app\code\core\Mage\Adminhtml\Block\Catalog\Product\Edit\Tab\Options\Option.php
Find the below code:
$value['sku'] = $this->htmlEscape($option->getSku());
Add these code just after
$value['description'] = $this->htmlEscape($option->getDescription());
Find the below code:
'sku' => $this->htmlEscape($_value->getSku()),
Add these code just after
'description' => $this->htmlEscape($_value->getDescription()),
Step 3:-
Add field in “catalog_product_option_type_value” table description.
Let me know if you have some query.

Is it possible to use a PyroCMS get_many_by with a LIKE?

$base_where = $this->input->post('f_OrderNumber') ? $base_where + array('OrderNumber' => '*' . $this->input->post('f_OrderNumber') . '*') : $base_where;
$orders = $this->orders_m->get_many_by($base_where);
Except I want the OrderNumber to be LIKE the POST
If your "orders_m" model is extending from MY_Model (which it probably is), you can use all the standard Codeigniter Active Record functions.
if( $this->input->post('f_OrderNumber') )
{
$this->orders_m->like('OrderNumber', $this->input->post('f_OrderNumber'));
}
$orders = $this->orders_m->get_all(); // if you want everything OR
$orders = $this->orders_m->get_many_by('field', 'value'); // if you have other parameters

Optimizing PyroCMS code for Keywords

I built this method following the the tagged() method from blog module
public function genres($genre = null)
{
$this->db->order_by('name', 'ASC');
$result = $this->db->get('keywords');
$genres = $result->result();
if($genre)
{
$this->load->model('genres_m');
// decode encoded cyrillic characters
$genre = rawurldecode($genre) OR redirect('generos');
$time[] = time();
// Count total blog posts and work out how many pages exist
$pagination = create_pagination(lang('ebooks:routes:genres') . '/' . $genre, $this->genres_m->count_genres_by($genre, array('entry_active' => 1)), NULL, 4);
$time[] = time();
// Get the current page of blog posts
$books = $this->genres_m
->limit($pagination['per_page'])
->order_by('info_title', 'ASC')
->get_genres_by($genre, array('entry_active' => 1));
$time[] = time();
foreach ($books AS &$book)
{
$book->books_info_genre = Keywords::get($book->books_info_genre, 'blog/tagged');
$book->url = site_url(lang('ebooks:routes:ebook') . '/' . $book->info_title . '/' . $book->id);
}
$time[] = time();
// Set meta description based on post titles
//$meta = $this->_posts_metadata($books);
$name = str_replace('-', ' ', $genre);
// Build the page
$this->template
->title($this->_template_title(lang('ebooks:of').' '.$name))
->set_metadata('description', $this->_template_title(lang('ebooks:of').' '.$name))
->set_metadata('keywords', $this->_template_title(lang('ebooks:of').' '.$name))
->set('genres', $genres)
->set('books', $books)
->set('genre', $genre)
->set('time', $time)
->set('pagination', $pagination)
->build('genres-list');
}
else
{
$this->template->title($this->_template_title(lang('ebooks:genres_by')))
->set_metadata('description', $this->_template_title(lang('ebooks:genres_by')))
->set_metadata('keywords', $this->_template_title(lang('ebooks:genres_by')))
->set('genres', $genres)
->build('genres-list');
}
}
And, this is the model:
public function count_genres_by($genre, $params)
{
return $this->db->select('*')
->from('downloads_books_book_info')
->join('keywords_applied', 'keywords_applied.hash = downloads_books_book_info.books_info_genre')
->join('keywords', 'keywords.id = keywords_applied.keyword_id')
->where('keywords.name', str_replace('-', ' ', $genre))
->where($params)
->count_all_results();
}
public function get_genres_by($genre, $params)
{
return $this->db->select('*')
->from('downloads_books_book_info')
->join('keywords_applied', 'keywords_applied.hash = downloads_books_book_info.books_info_genre')
->join('keywords', 'keywords.id = keywords_applied.keyword_id')
->where('keywords.name', str_replace('-', ' ', $genre))
->where($params)
->get()
->result();
}
As you can see in the first part of code, I got the time() four times, giving the delays:
18:49 - 19:03 - 19:41 - 19:41
I have a DB with about 5K entries. How can I optimize this code?
You can use 2.2.0-beta1 and take advantage of the Search system, which will store all keywords as text and knock out a few joins for your queries.
Otherwise you can build your own index table using blog events, which will store keywords next to blog id's.
The main problem is that your are running an SQL query on EVERY single returned item, whilst it would be quicker to work out all the keywords in one go. Even a SQL sub-query would be slightly quicker.

Image in Code Igniter anchor() function

I have a model to fetch data from mysql database. I am using the table and pagination library to display data. When displaying the data there is a column called "DELETE", which is used for deleting each rows. Instead of using the text "DELETE" I want to use an image. I have tried a lot to add it in the model but it is not working. Would you please kindly help me?
Thanks in Advance :)
function batch_list()
{
$config['per_page'] = 15;
$this->db->select('batchname, class, batchinstructor');
$this->db->order_by("batchid", "desc");
$rows = $this->db->get('batch',$config['per_page'],$this->uri->segment(3))->result_array();
$sl = $this->uri->segment(3) + 1; // so that it begins from 1 not 0
foreach ($rows as $count => $row)
{
array_unshift($rows[$count], $sl.'.');
$sl = $sl + 1;
$rows[$count]['batchname'] = anchor('batch_list/get/'.$row['batchname'],$row['batchname']);
$rows[$count]['Edit'] = anchor('update_student/update/'.$row['batchname'],'Edit');
$rows[$count]['Delete'] = anchor('report/'.$row['batchname'],'<img src="base_url().support/images/icons /cross.png" alt="Delete" />"'); //<<< This is where I actually tried/want to add the image
}
return $rows;
}
Please clarify what you mean by not working.
Anyway,
for now I am guessing your problem is your escaping, try:
anchor('report/'.$row['batchname'], '<img src="'.base_url().'support/images/icons/cross.png" alt="Delete" />');
or if you are using the html helper you can use
anchor('report/'.$row['batchname'], img('support/images/icons/cross.png'));
(if you want the alt attribute you will need to use the array form)
$img = array(
'src' => 'support/images/icons/cross.png',
'alt' => 'Delete'
);
anchor('report/'.$row['batchname'], img($img));

Resources