【问题标题】:Magento - How to get the Order ID and 2-letter state abbreviation of an orderMagento - 如何获取订单的订单 ID 和 2 个字母的状态缩写
【发布时间】:2015-06-16 16:31:03
【问题描述】:

所以我必须将变量发送到附属网站,它们需要订单 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,但没有返回任何内容。所以我想知道我做错了什么/为什么它没有打印出任何东西。

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

我们将不胜感激任何一个问题的答案。

【问题讨论】:

  • 您显示此信息的上下文是什么?在将其转换为订单之前,不会将订单 ID 分配给报价。
  • 我只是在标题中。
  • 如果我的理解是正确的,当有人完成结帐但 $order 在那之前是空的时,这将起作用?
  • 您应该如何将数据发送给您的会员,何时需要发送?
  • 下单后发送。他们给了我已经输入的代码,我只需要获取与订单对应的实际值。

标签: php magento checkout magento-1.9


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    最好的方法是通过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();
    

    应该可以。

    【讨论】:

    • 当我将此代码放入 success.phtml 页面时,OrderId 确实有效,但我的其他变量(金额、stateId 和 countryId)为空。有没有办法让这四个都起作用?
    • $order = Mage::getModel('sales/order')->loadByIncrementId($orderId); $total = $order->getGrandTotal(); $subtotal = $order->getSubtotal(); 所有这些都应该工作
    猜你喜欢
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    相关资源
    最近更新 更多