getRequest()->getPost('amorderattr')) { $session = Mage::getSingleton('checkout/type_onepage')->getCheckout(); $orderAttributes = $session->getAmastyOrderAttributes(); if (!$orderAttributes) { $orderAttributes = array(); } if (!Mage::registry('attributeClear')){ $orderAttributes = array_merge($orderAttributes, Mage::app()->getRequest()->getPost('amorderattr')); } $session->setAmastyOrderAttributes($orderAttributes); } } public function onSalesQuoteSaveAfter($observer) { $this->_prepareOrderAttributes(); } public function onCheckoutTypeOnepageSaveOrderAfter($observer) { $this->_prepareOrderAttributes(); $order = $observer->getOrder(); $session = Mage::getSingleton('checkout/type_onepage')->getCheckout(); $orderAttributes = $session->getAmastyOrderAttributes(); $attributes = Mage::getModel('amorderattr/attribute'); $attributes->load($order->getId(), 'order_id'); if ($attributes->getId()) { return false; } if (is_array($orderAttributes) && !empty($orderAttributes)) { $collection = Mage::getModel('eav/entity_attribute')->getCollection(); $collection->addFieldToFilter('is_visible_on_front', 1); $collection->addFieldToFilter('entity_type_id',Mage::getModel('eav/entity')->setType('order')->getTypeId()); $attributesList = $collection->load(); foreach ($attributesList as $attribute) { if ('checkboxes' == $attribute->getFrontend()->getInputType()) { if (array_key_exists($attribute->getAttributeCode(), $orderAttributes)) { $orderAttributes[$attribute->getAttributeCode()] = implode(',', $orderAttributes[$attribute->getAttributeCode()]); } } } $attributes->addData($orderAttributes); } $attributes->setData('order_id', $order->getId()); $this->_applyDefaultValues($order, $attributes); $attributes->save(); $session->setAmastyOrderAttributes(array()); Mage::register('attributeClear',true); } // this will be used when creating/editing order in the backend public function onSalesOrderSaveAfter($observer) { if (false !== strpos(Mage::app()->getRequest()->getControllerName(), 'sales_order') && 'save' == Mage::app()->getRequest()->getActionName() && !Mage::registry('amorderattr_saved')) { $order = $observer->getOrder(); $orderAttributes = Mage::app()->getRequest()->getPost('amorderattr'); $attributes = Mage::getModel('amorderattr/attribute'); $attributes->load($order->getId(), 'order_id'); if ($attributes->getId()) { return false; } if (is_array($orderAttributes) && !empty($orderAttributes)) { $attributes->addData($orderAttributes); } $attributes->setData('order_id', $order->getId()); $this->_applyDefaultValues($order, $attributes); // $attributes might be modified in that function $attributes->save(); Mage::register('amorderattr_saved', true); } } protected function _applyDefaultValues($order, $attributes) { $collection = Mage::getResourceModel('eav/entity_attribute_collection') ->setEntityTypeFilter( Mage::getModel('eav/entity')->setType('order')->getTypeId() ); $collection->getSelect() ->where('main_table.is_user_defined = ?', 1) ->where('main_table.apply_default = ?', 1); if ($collection->getSize() > 0) { foreach ($collection as $attributeToApply) { if (!$attributes->getData($attributeToApply->getAttributeCode()) && $attributeToApply->getDefaultValue()) { $attributes->setData($attributeToApply->getAttributeCode(), $attributeToApply->getDefaultValue()); } } } } public function modifyOrderGrid($observer) { $isVersion14 = ! Mage::helper('ambase')->isVersionLessThan(1,4); $layout = Mage::getSingleton('core/layout'); if (!$layout) return; $permissibleActions = array('index', 'grid'); if ( false === strpos(Mage::app()->getRequest()->getControllerName(), 'sales_order') || !in_array(Mage::app()->getRequest()->getActionName(), $permissibleActions) ) return; /** * Building the list of attribute field names for select query */ $attributes = Mage::getModel('eav/entity_attribute')->getCollection(); $attributes->addFieldToFilter('entity_type_id', Mage::getModel('eav/entity')->setType('order')->getTypeId()); $attributes->addFieldToFilter('show_on_grid', 1); $attributes->load(); if ($attributes->getSize()){ $fields = array(); foreach ($attributes as $attribute) { $fields[] = $attribute->getAttributeCode(); } $alias = $isVersion14 ? 'main_table' : 'e'; $attributes->getSelect() ->joinLeft( array('custom_attributes' => Mage::getModel('amorderattr/attribute')->getResource()->getTable('amorderattr/order_attribute')), "$alias.entity_id = custom_attributes.order_id", $fields ); } $grid = $layout->getBlock('sales_order.grid'); // Mage_Adminhtml_Block_Sales_Order_Grid if ( ($attributes->getSize() > 0) && ($grid) ) { $after = 'grand_total'; foreach ($attributes as $attribute) { $column = array(); switch ($attribute->getFrontendInput()) { case 'date': if ('time' == $attribute->getNote()) { $column = array( 'header' => Mage::helper('amorderattr')->__($attribute->getFrontend()->getLabel()), 'type' => 'datetime', 'align' => 'center', 'index' => $attribute->getAttributeCode(), 'filter_index' => 'custom_attributes.'.$attribute->getAttributeCode(), 'gmtoffset' => false, 'renderer' => 'amorderattr/adminhtml_order_grid_renderer_datetime', ); } else { $column = array( 'header' => Mage::helper('amorderattr')->__($attribute->getFrontend()->getLabel()), 'type' => 'date', 'align' => 'center', 'index' => $attribute->getAttributeCode(), 'filter_index' => 'custom_attributes.'.$attribute->getAttributeCode(), 'gmtoffset' => false, ); } break; case 'text': case 'textarea': $column = array( 'header' => Mage::helper('amorderattr')->__($attribute->getFrontend()->getLabel()), 'index' => $attribute->getAttributeCode(), 'filter_index' => 'custom_attributes.'.$attribute->getAttributeCode(), 'filter' => 'adminhtml/widget_grid_column_filter_text', 'sortable' => true, ); break; case 'boolean': $options = array(); foreach ($attribute->getSource()->getAllOptions(false, true) as $option) { $options[$option['value']] = $option['label']; } $column = array( 'header' => Mage::helper('amorderattr')->__($attribute->getFrontend()->getLabel()), 'index' => $attribute->getAttributeCode(), 'filter_index' => 'custom_attributes.'.$attribute->getAttributeCode(), 'type' => 'options', 'options' => $options, 'filter' => 'adminhtml/widget_grid_column_filter_select', ); break; case 'select': $options = array(); foreach ($attribute->getSource()->getAllOptions(false, true) as $option) { $options[$option['value']] = $option['label']; } $column = array( 'header' => Mage::helper('amorderattr')->__($attribute->getFrontend()->getLabel()), 'index' => $attribute->getAttributeCode(), 'filter_index' => 'custom_attributes.'.$attribute->getAttributeCode(), 'type' => 'options', 'options' => $options, ); break; case 'multiselect': $options = array(); foreach ($attribute->getSource()->getAllOptions(false, true) as $option) { $options[$option['value']] = $option['label']; } $column = array( 'header' => Mage::helper('amorderattr')->__($attribute->getFrontend()->getLabel()), 'index' => $attribute->getAttributeCode(), 'filter_index' => 'custom_attributes.'.$attribute->getAttributeCode(), 'type' => 'options', 'options' => $options, ); break; case 'checkboxes': $options = array(); foreach ($attribute->getSource()->getAllOptions(false, true) as $option) { $options[$option['value']] = $option['label']; } $column = array( 'header' => Mage::helper('amorderattr')->__($attribute->getFrontend()->getLabel()), 'type' => 'options', 'options' => $options, 'index' => $attribute->getAttributeCode(), 'filter_index' => 'custom_attributes.'.$attribute->getAttributeCode(), 'filter' => 'amorderattr/adminhtml_order_grid_filter_checkboxes', 'renderer' => 'amorderattr/adminhtml_order_grid_renderer_checkboxes', ); break; } $grid->addColumnAfter($attribute->getAttributeCode(), $column, $after); // Mage_Adminhtml_Block_Widget_Grid $after = $attribute->getAttributeCode(); } } } /** * Join columns to the Orders Collection. * @param Varien_Event_Observer $observer */ public function modifyOrderCollection($observer) { $isVersion14 = ! Mage::helper('ambase')->isVersionLessThan(1,4); $collection = $observer->getOrderGridCollection(); $attributes = Mage::getModel('eav/entity_attribute')->getCollection(); $attributes->addFieldToFilter('entity_type_id', Mage::getModel('eav/entity')->setType('order')->getTypeId()); $attributes->addFieldToFilter('show_on_grid', 1); $attributes->load(); if ($attributes->getSize()){ $fields = array(); foreach ($attributes as $attribute) { $fields[] = $attribute->getAttributeCode(); } $alias = $isVersion14 ? 'main_table' : 'e'; $collection->getSelect() ->joinLeft( array('custom_attributes' => Mage::getModel('amorderattr/attribute')->getResource()->getTable('amorderattr/order_attribute')), "$alias.entity_id = custom_attributes.order_id", $fields ); } } }