Tuesday, December 29, 2009

how to get customer address (for MagentoPycho)

Problem:how to get customer address from session?

Possible solution
Here is how you retrieve the primary billing address object:
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
if ($customerAddressId){
$address = Mage::getModel('customer/address')->load($customerAddressId);
}
If you want the address in html format do this:
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
if ($customerAddressId){
$address = Mage::getModel('customer/address')->load($customerAddressId);
$htmlAddress = $address->format('html')
}
if you want specific parts of the address just print it out and see how that part is called
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
if ($customerAddressId){
$address = Mage::getModel('customer/address')->load($customerAddressId);
echo "
"; print_r($address->getData());echo "
"; }
for example if you want the street do this:
$street = $address->getData('street');
It works the same for the rest of the of the address attributes (firstname, lastname, phone, ...)

Exporting reviews (for rpeters83)

Problem: how to export reviews from Magento.
Possible solution:


Here is how you can retrieve all the comments.
Create a new php file on the same level as index.php of Magento. let's call it reviews.php
the content of this file should look like this:
<?php 
$mageFilename = 'app/Mage.php';

require_once $mageFilename;

Varien_Profiler::enable();

Mage::setIsDeveloperMode(true);

ini_set('display_errors', 1);

umask(0);
Mage::app('default');
Mage::register('isSecureArea', 1);

header('Content-Type: text/xml; charset=utf-8');
echo "<?xml version =\"1.0\" encoding =\"UTF-8\"?>\n";
echo "<reviews>\n";
$collection = Mage::getModel('review/review')->getCollection();
foreach ($collection as $item){
$review = Mage::getModel('review/review')->load($item->getId());
echo "\t<review>\n";
echo "\t\t<id>".$review->getId()."</id>\n";
echo "\t\t<created_at>".$review->getCreatedAt()."</created_at>\n";
echo "\t\t<status_id>".$review->getStatusId()."</status_id>\n";
echo "\t\t<store_id>".$review->getStoreId()."</store_id>\n";
echo "\t\t<title>".$review->getTitle()."</title>\n";
echo "\t\t<detail>".$review->getDetail()."</detail>\n";
echo "\t\t<nickname>".$review->getNickname()."</nickname>\n";
echo "\t\t<customer_id>".$review->getCustomerId()."</customer_id>\n";
echo "\t\t<product_id>".$review->getEntityPkValue()."</product_id>\n";
echo "\t\t<stores>".implode(",", $review->getStores())."</stores>\n";
echo "\t</review>\n";
}
echo "</reviews>";
?>
Just call the page in your browser (www.yoursite.com/review.php)
This will export all the reviews in xml format. If you want an other format you can manage from this.
The status id is one of these
APPROVED       = 1;
PENDING        = 2;
NOT_APPROVED   = 3;

I hope this helps.

Monday, December 28, 2009

Switching language and currency when having multiple websitecodes (for sonician)

Problem (and maybe better solutions) found here:
http://www.magentocommerce.com/boards/viewthread/71171/

Hi again
There is a way of running a store view directly.
When you call something like this:
Mage::run('website1', 'website');
the default store view from website1 will run.
Let's say in website1 you have a store view called store_view_1. You can run it like this.
Mage::run('store_view_1');
As for the currency, you can set a default currency for each store view.
For example you can have to identical store views (in English), one with default currency 'EUR' and one 'GBP'.
Let's say the first one is called store_view_eur, and the second one store_view_gbp.
You can run them like this.
if (some condition){
Mage::run('store_view_eur');
}
else{
Mage::run('store_view_gbp');
}
As for the IP location, sorry but I don't think I can help you much there.

Thursday, December 24, 2009

Websitecode, currency, language (for sonician)

Problem:
get through code the current values for website, currency and language.
Hello,
Website:
//for name
Mage::app()->getWebsite()->getName();
//for code
Mage::app()->getWebsite()->getCode();
Currency:
Mage::app()->getStore()->getCurrentCurrencyCode();
Language:
This is a little tricky. You can get the ISO code for the language, because by standards French for France is different from French for Belgium
Mage::app()->getLocale()->getLocale();//will return something like fr_FR, fr_BE or en_US
if you want to go further and see only the language code it's simple
$isoCode = Mage::app()->getLocale()->getLocale();
$parts = explode("_", $isoCode);
$language = $parts[0];
$country = $parts[1];

Currency Rounding (for jjones17)

Problem:
Round the currency to the nearest ten ore (ten cents)

Possible solution:

Here is what you can try.
In lib/Zend/Currency.php, in the $_options member of the class change precision from 2 to 1.
This will have the desired effect but also side effects.
All the other currencies besides Norwegian Krone they will all be rounded in the same way.
All prices will look like this 123.4 instead of 123.40.
In order to avoid the first problem instead of changing the precision you can do a "quick and dirty" job.
in Mage_Directory_Model_Currency class for method formatTxt() add this right at the beginning.
if ($this->getCode() == 'NOK'){//if you need it for an other //currency just replace the code here
$options['precision'] = 1;
}

If you want to avoid the last problem (123.4 instead of 123.40) in the same method formatTxt() add add a 0 to the return value
return Mage::app()->getLocale()->currency($this->getCode())->toCurrency($price, $options)."0";

If you don't mind these 2 side effects than I recommended using the first solution. It's less code.