Sunday, July 10, 2011

Getting product list by category from magento web service


Never thought that in Magento Community Edition 1.5, retrieving product list by category via a web service function could be such a pain.

Let me explain ...

In Magento, you can assign a product to categories.

A product can also have what in Magento's lingo is called an 'attribute set'.



To get products by category, you use web service function catalog_category.assignedProducts. For example, to get products under category id 44 in store 1, use:


$responseInfo = $proxy->call($sessionId, 'catalog_category.assignedProducts', array(44, 1));

Notice that the category id and store id are passed in an array.

A major drawback with this function is that it has no filter options for pagination purposes. So one can't query for items 30 to 40 for a given category id. Strange omission indeed ...




Function catalog_product.list however allows some sort of workaround when it comes to pagination. However, it does not return products based on their category, it uses 'attribute set' instead. Bummer ...

Here is an example usage of this web service function. In the following, we are fetching records which have 'attribute set' 49 and id between 44 to 48.


$filters = array(
'type_id' => array( '=' => 'simple' ),
// // Fetch products which belong to a certain 'attribute set' NOT category.
'set' => array('=' => 49),
// // Fetch products from id 44 to 48, inclusive. Could be used for pagination I guess ...
'product_id' => array('in' => range(44,48)),
// // Fetch active products only
'status' => array('=' => 1)
);
$responseInfo = $proxy->call($sessionId, 'catalog_product.list', array($filters));


catalog_category.assignedProducts could be the prefect function to fetch products by category as this is how most stores display their wares, not using Magento's 'attribute set'. Unfortunately this function is rather spartan and has no filter options unlike catalog_product.list.

A somewhat inelegant workaround to achieve pagination using catalog_category.assignedProducts is by retrieving all products under a specified category and iterate manually to get the range of products which we want to display. IMHO, this may not work so well should the number of items under said category is huge.

Tuesday, September 21, 2010

Simple Git example for SVN users


In my case, using Git for the first time wasn't exactly a picnic, being so used to SVN. The learning curve was steep and requires a lot of thorough reading plus research (i.e Google). IMHO, it doesn't have the up-and-get-go ease of use as SVN, understandable as Git is far more advance.



So here's another Git guide for SVN users, albeit a simple one. Here, the central Git repository is hosted at GitHub and the OS used is Linux.



Say you have a GitHub a/c with u/p : myname/mypassword.



To access GitHub, create the SSL as described here.



Set global git parameters so you can execute commands without being asked for passwords and what not. For the user name and email, use the same one when you registered with GitHub.


git config --global user.name "myname"
git config --global user.email your_email@some_domain.com



Get a fresh copy of your repository from GitHub. In this case the GitHub url for the repository is git@github.com:some_user_name/RepositoryName.git. After this, a new directory called RepositoryName will be created. 'cd' into it so you may execute the rest of the git relevant commands.


git clone git@github.com:some_user_name/RepositoryName.git
cd RepositoryName



From here, begins your usual edit/compile/test cycle.



To see which files have been modified/added/deleted, you can use either of these:


git status
git diff --color



To commit a modified file, type the following. Note that unlike SVN, this does not go to the GitHub repository.


git commit app/views/projects/show.html.erb -m "fixed some bug"



To commit a new file, type the following. This also does not go to the GitHub repository immediately like in SVN.


git add app/views/projects/form.html.erb
git commit app/views/projects/form.html.erb -m "added new form"



To push what you've just committed to the GitHub repository:


git push



To get updates from the GitHub repository:


git pull



Hope that helps, especially when you're used to the SVN way of doing things.



Of course, do Google around for more info. There's tons out there.

Wednesday, May 5, 2010

The security header is not valid

Regarding Paypal API credentials, here's a summary to avoid confusion:

* If you're using SANDBOX mode, you need to use a SANDBOX account API credentials.
* If you're using LIVE mode, you need to use a LIVE account API credentials.

If you use a LIVE account to test in Paypal's SANDBOX environment, Paypal will respond with "The security header is not valid" and vice versa.

There must be a good reason why the returned message is so vague.