0

How do I get a document for a specific order in Magento (1.x) and (2.x) if possible please.

{
    "id": 100,
    "customer_id": 10,
    "date_created": "Wed, 14 Nov 2012 19:26:23 +0000",
    "date_modified": "Wed, 14 Nov 2012 19:26:23 +0000",
    "date_shipped": "",
    "status": "New",
    "subtotal_ex_tax": "99.0000",
    "subtotal_inc_tax": "99.0000",
    "subtotal_tax": "0.0000",
    "base_shipping_cost": "0.0000",
    "shipping_cost_ex_tax": "0.0000",
    "shipping_cost_inc_tax": "0.0000",
    "shipping_cost_tax": "0.0000",
    "shipping_cost_tax_class_id": 2,
    "base_handling_cost": "0.0000",
    "handling_cost_ex_tax": "0.0000",
    "handling_cost_inc_tax": "0.0000",
    "handling_cost_tax": "0.0000",
    "handling_cost_tax_class_id": 2,
    "base_wrapping_cost": "0.0000",
    "wrapping_cost_ex_tax": "0.0000",
    "wrapping_cost_inc_tax": "0.0000",
    "wrapping_cost_tax": "0.0000",
    "wrapping_cost_tax_class_id": 3,
    "total_ex_tax": "99.0000",
    "total_inc_tax": "99.0000",
    "total_tax": "0.0000",
    "items_total": 1,
    "items_shipped": 0,
    "payment_method": "cash",
    "payment_provider_id": null,
    "payment_status": "",
    "refunded_amount": "0.0000",
    "order_is_digital": false,
    "store_credit_amount": "0.0000",
    "gift_certificate_amount": "0.0000",
    "ip_address": "50.58.18.2",
    "geoip_country": "",
    "geoip_country_iso2": "",
    "currency_id": 1,
    "currency_code": "USD",
    "currency_exchange_rate": "1.0000000000",
    "default_currency_id": 1,
    "default_currency_code": "USD",
    "staff_notes": "",
    "customer_message": "",
    "discount_amount": "0.0000",
    "coupon_discount": "0.0000",
    "shipping_address_count": 1,
    "is_deleted": false,
    "billing_address": {
      "first_name": "Martin",
      "last_name": "Sheen",
      "company": "",
      "street_1": "12345 Fake Street",
      "street_2": "",
      "city": "Austin",
      "state": "Texas",
      "zip": "12345",
      "country": "United States",
      "country_iso2": "US",
      "phone": "",
      "email": "elsie@example.com"
    },
    "products": [
        {
            "id": 16,
            "order_id": 115,
            "product_id": 111,
            "order_address_id": 16,
            "name": "Some Product",
            "sku": "",
            "price": "33.0000",
            "qty": 3,
        }
    ],
    "shipping_addresses": [
        {
          "first_name": "Martin",
          "last_name": "Sheen",
          "company": "",
          "street_1": "12345 Fake Street",
          "street_2": "",
          "city": "Austin",
          "state": "Texas",
          "zip": "12345",
          "country": "United States",
          "country_iso2": "US",
          "phone": "",
          "email": "elsie@example.com"
        }
    ],
    "coupons": [
        {
          "id": 1,
          "coupon_id": 1,
          "order_id": 115,
          "code": "557D2DEA0CCAFA1",
          "amount": "5.0000",
          "type": 1,
          "discount": "4.6600"
        }
    ]
  }

I accept Magento probably doesn't spit documents like this out by default, and that some work will be needed to create such a structure, but I have no idea what the field names are or would be (not all fields essential, just shipping & billing address, customer details, status, totals and sub-totals, items, discounts & coupons).

If anything is not available from the above-abbreviated list, please let me know. Also how to say an order is shipping in Magento, and where to add tracking numbers?

Thanks in advance.

0

It looks like Magento doesn't support some of the things the API I was trying to shape it around does, but here is the initial code I came up with.

$_status = $_REQUEST['status'];

if(!file_exists('app/Mage.php')) {
    die("This must live within a valid Magento Installation\n");
}

require_once('app/Mage.php');
Mage::app();

$orders = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('status', $_status)
    ->addAttributeToSelect('*')
    ->addFieldToSelect('*')
    ->setOrder('created_at', 'asc');

$orders = [];
foreach($orders as $order) {
    $orderData = [];

    $billing_address = $order->getBillingAddress();
    $shipping_address = $order->getShippingAddress();
    $customer = $order->getCustomer();

    $orderData['id'] = $order->getId();
    $orderData['billing_address'] = getAddressData($billing_address);
    $orderData['shipping_address'] = getAddressData($shipping_address);

    $orderData['discount_amount'] = $order->getDiscountAmount();
    $orderData['subtotal_ex_tax'] = $order->getSubtotal();
    $orderData['shipping_cost_tax'] = $order->getTaxAmount();

    $orderData['currency_code'] = $order->getOrderCurrencyCode();
    $orderData['shipping_cost_inc_tax'] = $shipping_address->getShippingInclTax();
    // Guess at handling
    $orderData['handling_cost_inc_tax'] = $order->getGrandTotal()-$shipping_address->getShippingInclTax()-$order->getSubtotal();
    $orderDatap'payment_provider_id'] = $order->getPayment()->getMethod()->getCode();

    $orderData['products'] = [];
    foreach($order->getAllItems() as $item) {
        $orderData['products'][] = [
            'sku' => $item->getSku(),
            'price_ex_tax' => $item->getPrice(),
            'quantity' => $item->getQuantity()
        ]
    }
    $orders[] = $orderData;
}

die(json_encode($orders, JSON_PRETTY_PRINT));

function getAddressData($obj) {
    $out = [];
    $out['first_name'] = $obj->getData('firstname');
    $out['last_name'] = $obj->getData('lastname');
    $out['street_1'] = $obj->getStreet(1);
    $out['street_2'] = $obj->getStreet(2);
    $out['phone'] = $obj->getTelephone();
    $out['zip'] = $obj->getPostcode();
    $out['city'] = $obj->getData('city');
    $out['state'] = $obj->getData('region');

    $out['country_iso2'] = $obj->getCountryModel()->getIso2Code();
    $out['email'] = $obj->getEmail();
    return $out;
}

I'll still need to heavily refactor & test the code, as well as find the differences between Magento1.x and 2.x. If you're wondering where I got this information from I had to look at the source code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.