0

我正在尝试从订单中获取小计和总计,但结果始终为 NULL。

我尝试了很多我在网上找到的解决方案,但没有任何效果。我等待的事件是:sales_order_place_after

这是代码:

class MyCompany_MyObserver_Model_Observer {

public function send_email($observer) {

    $customer = Mage::getSingleton('customer/session')->getCustomer();
    //Get name and email of customer
    $email = $customer->getEmail();
    $fullname = $customer->getName();
    //Get the customer group and check that if Trade customer group a email gets sent
    $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();

到目前为止,所有代码都可以正常工作。我可以获取姓名、电子邮件和 groupID

无论如何,下面的代码都会中断!我已经尝试了所有我能找到的解决方案。我正在尝试使用 var_dump 来查看值..

        $session= Mage::getSingleton('checkout/session');
        //Get totals
        $number =  Mage::getModel('checkout/cart')->getQuote()->getTotals();
        $subtotal = number_format($number,2);
        $discount = $totals['discount']->getValue();
        $shippingtotal = $totals['shipping']->getValue();
        var_dump($grandtotal);
        //die();

        $grand_total = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();

        $shippingMethod = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod();

        $paymentMethod = Mage::getSingleton('checkout/session')->getQuote()->getPayment()->getMethodInstance()->getTitle();

        //Get billing and shipping address

        $billingaddress = Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress();

        $billingStreet = $billingaddress->getData('street');
        $billingPostcode = $billingaddress->getData("postcode");
        $billingCity = $billingaddress->getData("city");
        $billingRegion = $billingaddress->getRegionCode();
        $billingCountry = $billingaddress->getCountryModel()->getName();

        $shippingAddress = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();

        $shippingName = $shippingAddress->getName();
        $shippingCompany = $shippingAddress->getData("company");
        $shippingStreet = $shippingAddress->getData("street");
        $shippingPostcode = $shippingAddress->getData("postcode");
        $shippingCity = $shippingAddress->getData("city");
        $shippingRegion = $shippingAddress->getRegion();
        $shippingCountry = $shippingAddress->getCountryModel()->getName();

}
}

我的配置文件看起来像这样:我捕捉到了正确的事件吗?我唯一能得到的是:订单 ID 客户姓名和电子邮件。

<events>
  <checkout_onepage_controller_success_action>
    <observers>
      <sales_order_place_after>
        <type>singleton</type>
        <class>MyCompany_MyObserver_Model_Observer</class>
        <method>send_email</method>
      </sales_order_place_after>
    </observers>
  </checkout_onepage_controller_success_action>     
</events>
4

2 回答 2

1

很多小时后,我找到了解决问题的方法..

我通过这篇文章找到了答案:https ://magento.stackexchange.com/questions/6643/fatal-error-call-to-a-member-function-getdata-on-a-non-object @ProxiBlue 的帖子是解决方案。我发现如果您需要加载订单,这是正确的方法: $orderIds = $observer->getEvent()->getOrderIds(); 由于上一篇文章解释的原因!

所以@Rajiv Ranjan 的回答可能部分正确。非常感谢。

于 2013-10-18T12:50:32.313 回答
0

尝试以下代码以获取订单详细信息:

// Get incremented order id from checkout/session
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId(); 
// load order details by using order id
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);

可以使用上述对象获取其他详细信息:

$billing_address_data   = $order_details->getBillingAddress();
$shipping_address_data   = $order_details->getShippingAddress();
$payment_details = $order_details->getPayment();
$order_items = $order_details->getAllItems();

我正在使用上面的代码来获取关于success.phtml 的订单详细信息并使用观察者。

于 2013-10-18T06:48:04.633 回答