【问题标题】:Try to get Subtotal, Grandtotal and more by using Observer Magento 1.7尝试使用 Observer Magento 1.7 获取 Subtotal、Grandtotal 等
【发布时间】:2013-10-17 19:20:41
【问题描述】:

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

【问题讨论】:

    标签: magento cart observers subtotal


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      试试下面的代码来获取订单详情:

      // 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 上的订单详细信息并使用观察者。

      【讨论】:

      • 感谢拉吉夫·兰詹。我会试试的!
      • 它一直在做同样的事情。如果我回显前。 $payment_details 什么都不是。 @Rajiv Ranjan
      • 首先尝试echo '&lt;pre&gt;'; print($order_details-&gt;getData()); 看看结果。 $billing_address_data &amp; $shipping_address_data 有价值吗?或者您可以在第一步调试到echo $order_id;
      • 我可以看到'echo $order_id;'但我什么也得不到。我试图回显和'回显'
        ';打印($order_details->getData());'他们没有运气..你认为我试图抓住的事件有问题吗? 'sales_order_place_after' @Rajiv Ranjan
      • 不,如果您得到的订单 ID 符合预期,那么上面的代码应该可以工作。这个$order_details = Mage::getModel('sales/order')-&gt;loadByIncrementId($order_id);是magento加载顺序的核心功能。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多