Last active
August 14, 2017 13:46
-
-
Save neilbradley/1ddfe6c772b870c7b03d712c1accf4c7 to your computer and use it in GitHub Desktop.
Get all Magento orders between two dates with status new and placed by a customer with a specific customer attribute set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('app/Mage.php'); //Path to Magento | |
umask(0); | |
Mage::app(); | |
$fromDateStart = '2017-05-01'; | |
$toDateEnd = '2017-08-10'; | |
$fromDate = date('Y-m-d H:i:s', strtotime($fromDateStart)); | |
$toDate = date('Y-m-d H:i:s', strtotime($toDateEnd)); | |
$tableNameCustomerEntity = Mage::getSingleton('core/resource')->getTableName('customer_entity'); | |
$entityType = Mage::getModel('eav/entity_type')->loadByCode('customer'); | |
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entityType, 'YOUR_CUSTOMER_ATTRIBUTE'); | |
$orderCollection = Mage::getResourceModel('sales/order_collection'); | |
$orderCollection->getSelect() | |
->joinLeft(array('_table_'.$attribute->getAttributeCode() => $tableNameCustomerEntity.'_'.$attribute->getBackendType()), | |
'_table_'.$attribute->getAttributeCode().'.entity_id = main_table.customer_id ' . | |
' AND _table_'.$attribute->getAttributeCode().'.attribute_id = '.$attribute->getAttributeId(), | |
array($attribute->getAttributeCode() => '_table_'.$attribute->getAttributeCode().'.value') | |
); | |
$orderCollection->getSelect() | |
->where('_table_' . $attribute->getAttributeCode() . '.value IS NOT NULL'); | |
$orderCollection->addAttributeToFilter('status', array('in' => array('new'))) | |
->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate)); | |
$orders = $orderCollection; | |
foreach ($orders as $order) { | |
print '<pre>'; | |
print_r($order->getData()); | |
print_r($order->getItemsCollection()->getData()); | |
print_r($order->getShippingAddress()->getData()); | |
print_r($order->getItemsCollection()->getData()); | |
print_r($order->getBillingAddress()->getData()); | |
print_r($order->getPayment()->getData()); | |
$orderCustomerId = $order->getCustomerId(); | |
print_r($orderCustomerData = Mage::getModel('customer/customer')->load($orderCustomerId)); | |
print_r($order->getDiscountDescription()); | |
print '</pre>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment