Header Ad

Wednesday, March 31, 2010

Adding fields to the Contact form in Magento

This is one of the reference link for adding new fields to the Contact form in magento.

http://www.magentocommerce.com/wiki/how_to/add_fields_to_contact_form

Calculating UPS Shipping Rate with PHP

This is one of the post which i came across for calculating the UPS Shipping charges in PHP.

Please find the code at http://code.google.com/p/ups-php

Things you will need

* UPS Online Tool Account – free but requires registration
* UPS Access Key – Comes with the online tool account
* cURL – Most LAMP (Linux, Apache, Mysql, PHP) web hosts have this installed by default.
* PHP – You must know a little bit about using php. After all you are about to create a custom UPS shipping calculator.
* XML – If you know what PHP is you should know what this is. If not read about it here.
* SSH access – Many web hosts offer this. You don’t HAVE to have it but it makes troubleshooting easier.

Basically you start off by getting setup with UPS and grabbing a copy of their XML manual. Now you need to construct an XML file that will be later sent off to UPS’s server. UPS will then send a response back to you in XML format. We then use PHP to turn the XML into variables that we can use and manipulate. Even add the shipping cost into the subtotal or final checkout cost. After all, whats the use of calculating UPS shipping if you are not going to charge the user for it.

Copy the UPS PHP Function I have created into a new .txt file and name it ups.php.

Go through this function and find the parts in the XML file that are in CAPS, You will need to add your UPS shipper number, access key, username, password, departure zip, etc…

Then create another .php file and make sure to include ups.php.

It might look something like this:
view plaincopy to clipboardprint?

1. require_once("ups.php")

require_once("ups.php")

Now all you have to do to get an accurate UPS rate for a package is call the ups function.

The basic syntax for the function is:
view plaincopy to clipboardprint?

1. ups($dest_zip,$service,$weight,$length,$width,$height)

ups($dest_zip,$service,$weight,$length,$width,$height)

So, for example if you wanted to send a five pound package to Beverly Hills CA using UPS ground you would do this:
view plaincopy to clipboardprint?

1. ups(90210,03,5,5,5,5)

ups(90210,03,5,5,5,5)

Here is an already written php test file that uses the ups function.

By now you may be wondering how I know that 03 is the code for UPS ground and you also may be wondering what the code is for 2nd Day Air.

You can find all this information in the huge manual with the name dtk_RateXML_V1.zip. You can find this manual on the UPS website after you login under UPS Rates and Services Selection but to save you the time I have listed them below.
UPS Service Selection Codes

* 01 – UPS Next Day Air
* 02 – UPS Second Day Air
* 03 – UPS Ground
* 07 – UPS Worldwide Express
* 08 – UPS Worldwide Expedited
* 11 – UPS Standard
* 12 – UPS Three-Day Select
* 13 Next Day Air Saver
* 14 – UPS Next Day Air Early AM
* 54 – UPS Worldwide Express Plus
* 59 – UPS Second Day Air AM
* 65 – UPS Saver

Feel free to edit the function. The XML part of the code should be altered to fit your needs. This function only has the basic elements needed to calculate a rate. There are other attributes you can specify in the XML part of the document, such as pickup type, packaging type, etc…

Unlike some UPS Rate calculators you do not need to update the fuel surcharges or anything else. These rates come straight from UPS and are as accurate as your XML file has specified.
Troubleshooting


If it’s not working and you think you may have messed up the PHP code you can test to see if it is working by sending your XML file with curl.

Log into your host via SSH, make an xml file named RequestXML.txt. Then run this command.

curl -d @RequestXML.txt https://www.ups.com/ups.app/xml/Rate

If you want to use UPS’s testing server you can use this URL:

https://wwwcie.ups.com/ups.app/xml/Rate
Special Instructions for Godaddy


Godaddy requires a minor change in the ups function. After reading this article:

You need to have a proxy to use cURL on Godaddy.

So under this line:
“curl_setopt($ch,CURLOPT_TIMEOUT, 60);”

add this:
“curl_setopt($ch,CURLOPT_PROXY,’http://proxy.shr.secureserver

How to Import Products in Magento

There’s a little confusion among some on how to import products into a Magento ecommerce store. I spent some time today researching and trying to find the best method on doing this. The reason for my research is because there was no simple documentation anywhere that I could find on how to import products. Magento actually has built a pretty robust import/export mechanism into the ecommerce cms that has a ton of flexibility to do many things. I’m not going to cover all of those. This is just for those of you who simply just want to import products into their Magento cart.

Step 1 – Add a new product manually
add a new product manually to the catalog, assign it to a category, and fill out all fields that will be necessary to your store. The obvious ones are price, description, quantity etc. It’s important that you fill out all fields that you know you are going to need for all the products you import.

Step 2 – Export Your Products
Now we want to export your product to a .csv file so that we can view the fields that are required to import. Go to System >> Import/Export >> Profiles. Now click on Export All Products then Run Profile. Click on the “Run Profile in Popup” button. Once the export is completed, go to your var/export directory on your Magento install and you will find the .csv file there for you to download.

Step 3 – Analyze The .CSV File
Now if you look at your .csv export file you will see the field names that you need to match up. Now just start filling yours in and creating your csv file ready for import. This step is extremely important. Otherwise Magento cannot match what you are trying to import and the importing will fail. At a most basic level, here are the fields that I imported:

* store
* attribute_set
* type
* sku
* category_ids
* status
* visibility
* tax_class_id
* weight
* price
* name
* description


Step 4 – Import Your New .CSV File
Now go to System >> Import/Export >> Profiles. Now click on Import All Products. Change “Type” under Data Format to CSV/Tab Seperated. Now click on Upload File, and browse for your .csv file and click “Save and Continue Editing”. Now go to “Run Profile” and select your file from the dropdown menu. Click the button underneath to run the import. And whalllah. All your products should now be imported.

That was the easiest most simple approach that I could find for Importing Products into a Magento Open Source Ecommerce Store. Like I said I tried this myself and it worked well for me. After that, I went in and manually updated all of my Images and
Quantity numbers. You could even import those if you want to. But for
my purpose I didn’t need to.

Monday, March 29, 2010

Magento - Retrieve products with a specific attribute value

Almost all Magento Models have a corresponding Collection object that can be used to fetch multiple instances of a Model.

To instantiate a Product collection, do the following

$collection = Mage::getModel('catalog/product')->getCollection();

Products are a Magento EAV style Model, so you'll need to add on any additional attributes that you want to return.

$collection = Mage::getModel('catalog/product')->getCollection();
//fetch name and orig_price into data
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');


There's multiple syntaxes for setting filters on collections. I always use the verbose one below, but you might want to inspect the Magento source for additional ways the filtering methods can be used.

The following shows how to filter by a range of values (greater than AND less than)

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');


//filter for products whose orig_price is greater than (gt) 100
$collection->addFieldToFilter(array(
array('attribute'=>'orig_price','gt'=>'100'),
));


//AND filter for products whose orig_price is greater than (lt) 130
$collection->addFieldToFilter(array(
array('attribute'=>'orig_price','lt'=>'130'),
));

While this will filter by a name that equals one thing OR another.

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');


//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection->addFieldToFilter(array(
array('attribute'=>'name','eq'=>'Widget A'),
array('attribute'=>'name','eq'=>'Widget B'),
));

Saturday, March 27, 2010

Ability to Get Products under a category in Magento

Hi,

We can use the following code to get the products under a category.



$_categories=$this->getCurrentChildCategories();

$_category = $this->getCurrentCategory();
$subs = $_category->getAllChildren(true);
$result = array();
foreach($subs as $cat_id) {
$category = new Mage_Catalog_Model_Category();
$category->load($cat_id);
$collection = $category->getProductCollection();
foreach ($collection as $product) {
$result[] = $product->getId();
}

}
shuffle($result);
?>

Monday, March 8, 2010

How to find in between value from an array in javascript

Take the following code is an simple example for finding bigger value in an array


var foo = new Array("222","323","314","431","422");
var biggest = 10;
for (x=0;xif (foo[x] > biggest) {
biggest = parseint(foo[x]); //right here
}
}
document.write(biggest);