Magento - 如何获取订单的订单 ID 和 2 个字母的州缩写

Magento - How to get the Order ID and 2-letter state abbreviation of an order

所以我必须将变量发送到附属网站,他们需要订单 ID、价格、2 个字母的州缩写和国家/地区缩写。我已经得到了价格和国家缩写。但我仍然需要订单 ID 和 2 个字母的州缩写。我现在的代码如下:

$order = Mage::getSingleton('checkout/session')->getLastRealOrderId();//doesnt work
$amount = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();//works
$stateId = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('region');//Gives full name of State
$countryId = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('country_id');//works

echo " display: $order $amount $stateId $countryId";//prints out the variables

我一直在查看此处的所有代码以获取订单 ID,但没有返回任何内容。所以我想知道我做错了什么 that/why 它没有打印出任何东西。

第二个问题是,我想知道是否有一种简单的方法来获取 2 个字母的州缩写?我也尝试过使用“region_id”而不是 'region',但这只是给了我数字代码(不是字母代码)。

如果能回答这些问题中的任何一个,我们将不胜感激。

最好的方法是通过 success.phtml 文件发送此信息,因为只有在该文件中您才能获得该信息并避免发送未完成订单(失败的贝宝交易等)的订单 ID

1) 根据您的结账方式,您的数据中可能有也可能没有州代码。

因此,如果您通过

获得州名
$stateId = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('region');

然后在之后添加此代码:

$states = Mage::getModel('directory/country')->load('US')->getRegions();//get list of states 
    foreach($states as $state){
        if($state[default_name] == $stateId){ //check if the names match
            echo $state[code]; //get code
        } 
    }   

2) 在成功页面上

$orderId = $this->getOrderId();

应该可以。

加载订单后,您可以调用 'region_code' 或 getRegionCode()。我正在使用 magento 1.9。不确定它是否在以前的版本中可用。