尝试使用Observer Magento 1.7获得Subtotal,Grandtotal等

时间:2013-10-17 19:20:41

标签: magento cart observers subtotal

我正在尝试从订单中获取小计和Grandtotal,但结果始终为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>

2 个答案:

答案 0 :(得分: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的答案可能部分正确。 非常感谢。

答案 1 :(得分: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的订单详情,并使用观察者。