Magento - Retrieve Advanced Search results as a plain array - php

I am currently working on a Magento application and have a requirement to sort advanced search results based on the precedence of the categories in the store.
Basically, i have the algorithm prepared where i would loop through the advanced search results, run a query to retrieve the position of the products category and then sort the final result set before returning it back to the calling function.
But the issue I'm having is that i am unable to retrieve the search results as a plain array to work with. Could any one of the experts tell a way to retrieve this array please?
Regards,
Maximumus 69

assuming that you're working in list.phtml, this should work.
$_productCollection=$this->getLoadedProductCollection();
$_productCollection->toArray($requiredFields)
where $requiredFields is null (if you want all fields) or an array containing the fields that you're interested in.
Note that your choice to convert to array and then sort is particularly inefficient. You should be using Magento's inbuilt Collection sorting mechanisms. Read the documentation and API then then give setOrder('position') a try.
Good luck,
JD

Related

Sorting adjacency hierarchy for simple output looping

I'm pulling my hair out trying to avoid using multiple SQL queries. Could someone please point me in the right direction?
End result is a website navigation (ul/li) menu with nested links (dropdown menu) for some of the links.
The data comes from PDO as an array of objects. I believe my structure is referred to as an adjacency list, where each record has a parent ID which correlates to the id of the link under which it is nested, parent_id=0/NULL being top level. Every link also has a priority (a number by which to order them).
I require that the priority to only be relevant to the links on the same "level".
To output the html I really need the data in it's hierarchical order. I figured the uasort function could achieve this with a custom compare function, but I'm struggling to make it do anything comprehensible. Does uasort even make this possible?
Other avenues I thought might help were self joining SQL queries, or maybe looping through the array to build an object whereby "level2" links lived within the branch of it's parent (as you'd expect to see in an XML hierarchy).
Considering my current plans only require 2 levels and there won't be a million records to "sub-query" just tell me if you think I should concede and just use multiple queries.

elasticsearch returning all found aggregations

I'm using the example application from github.com/searchly/searchly-php-sample with Searchly service.
I've came a simple where I want the search results to return all the aggregations(continued as 'aggs') from the search results, not only the ones I specified.
Currently the code for the aggs is:
$searchParams['body']['aggs']['resolution']['terms']['field'] = 'resolution';
this returns the resolution agg but I can not find the way for it to return all of the possible aggs from the search results.
Is it possible or does it require me to save the aggs some where and then just list them when I do the actual search request?
Thank you!
As far as I know there is no way to do this directly - you have to specify each field you are interested in.
However if you can build up a list of all the fields in the index then you could generate the required aggregations fairly easily.
So, how to build up that list? I can think of three ways that might work
A) build it up by doing some pre-processing before you index each document into ElasticSearch
B) Use the GET MAPPING api to see what fields have been created by dynamic mapping (http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html)
C) Use a Scripted Metric Aggregation and write scripts that build up a de-duped list of fields in the documents (http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html)

Using Views module (Drupal 7) to display php array

I have a php array (let's call it $people) that I specifically created from LinkedIn API. The fact is I don't want to record it in my own database due to the huge amount of data and I don't want to use a Cron to update results.
So, the problem is I want to display results from my php array in a filterable/sortable table. Notice: I can parameter this array to catch results from a START parameter with a COUNT parameter; this is mandatory for the pagination.
How to use Views module of Drupal 7 to do that? Any idea?
Thanks in advance.
I didn't get the exact question but assuming you want to display some data in a paginated table, I can give you some hints. You can try the theme_table() to render a paginated table. Here is rough sample code https://drupal.org/node/156863
Integration with views in a relatively complex task. Easiest way is to go with https://drupal.org/project/data. But if you want integrate more, then start with the hook_views_data()

How to query for multiple types of an object (using a key-value table) and grouping the results together as a complete object

I hope I asked the question properly. I have a table of objects grouped by object_id. They are stored as a key / value. I thought this would be simple but I cannot find a solution anywhere. I'm trying to get the most efficient method of querying against this table to return a full object based on multiple meta_name values. Here's the table structure:
Here's the code I have so far, which works great to query one value:
SELECT data2.object_id,data2.object, data2.meta_name, data2.value_string, data2.value_text FROM meta_data AS data1
LEFT JOIN meta_data AS data2 ON(data1.object_id = data2.object_id)
data1.object="domain"
AND data1.meta_name = "category"
AND data1.value_string = "programmer"
This gives me the following results. This is great for a single taxonomy (domain in category programmer).
The problem comes when I want to query for all domains with category programmer AND color red AND possibly other meta_name = value_strings. I can find no solution for this outside of making multiple queries from PHP (which I want to avoid for obvious performance reasons).
I need to point out that objects will be created on the fly, and without a specific schema (which is the point of having this structure to begin with) so I cannot hard code and assume anything about an object (Objects may have more meta properties defined to them from the admin panel at any given time).
Again, I hope I am asking this question right, since I have been completely unlucky in finding a solution by searching online for the last 3 days.
Thank you so much ahead of time to the MySQL pro that can help me with this!
In situations like this solutions typically query all records to avoid multiple queries and then stitch data objects together to provide the desired format. Then you can develop simple find() methods on those objects to further filter the results (e.g. using array functions)
If you're interested in exact implementation, I encourage you to look at WordPress - you noted taxonomies. As an open source project you can review their code for an example of how this is done. Take a look at the Taxonomies API as well as Meta API.

Codeigniter - Add a product with the same ID if options are different

I'm currently building a simple ecommerce site and have ran into an interesting problem.
Basically, the products are in the products table however there are also a number of other options such as price, colour etc... some of these have an effect on the total price.
However, if the same product but with a different set of options exist then the product id is obviously the same and the item is not added into the codeigniter cart.
The easiest way that I have thought of is to allow products with the same id to be included in the cart, is this easily possible? Is there a better way of tackling this problem?
Any ideas will be very helpful!
OK so I found an answer to my problem which people may find helpful.
Basically I was using multidimensional arrays for the options which is not supported by Codeigniter's cart class.
This was then generating the same row id due to the fact that the arrays were showing up as array but may also have been breaking the implode function used to generate the hash in the Codeigniter Cart class.
To fix this you can simple replace this line within the CI_Cart class:
$rowid = md5($items['id'].implode('', $items['options']));
with this line:
$rowid = md5($items['id'].serialize($items['options']));
to fix the problem.
This then creates a storable representation of the multidimensional array as text that is then hashed and will always be unique if there is a multidimensional array used as the options value.

Categories