steffen-wirth
1/11/2018 - 11:23 AM

Advanced Example how to create 2 custom fields, fill them in order process, display them in backend order list as new columns and make them

Advanced Example how to create 2 custom fields, fill them in order process, display them in backend order list as new columns and make them editable in order detail view

// File location: OrderMod/Views/backend/order/view/ordermod/list/list.js
//{block name="backend/order/view/list/list" append}
//{namespace name="backend/swag_customer_preferences/main"}
Ext.define('Shopware.apps.Order.view.ordermod.list.List', {

    /**
     * Defines an override applied to a class.
     * @string
     */
    override: 'Shopware.apps.Order.view.list.List',

    /**
     * Overrides the getColumns function of the overridden ExtJs object
     * and inserts two new columns
     * @return
     */
    getColumns: function() {
        var me = this;

        var columns = me.callOverridden(arguments);

        var columnRandom1 = {
            header: 'Random 1',
            dataIndex:'attribute[ordermodRandom1]',
            flex: 1,
            sortable: false,
            renderer: function (p,v,r){
                return r.getAttributesStore.data.items[0].data.ordermodRandom1;
            }
        };
        var columnRandom2 = {
            header: 'Random 2',
            dataIndex:'attribute[ordermodRandom2]',
            flex: 1,
            sortable: false,
            renderer: function (p,v,r){
                return r.getAttributesStore.data.items[0].data.ordermodRandom2;
            }
        };
        return Ext.Array.insert(columns, 2, [columnRandom1, columnRandom2]);
    }
});
//{/block}
// File location: OrderMod/Views/backend/order/detail/ordermod/overview.js
//{block name="backend/order/view/detail/overview" append}
//{namespace name="backend/swag_customer_preferences/main"}
Ext.define('Shopware.apps.Order.view.ordermod.detail.Overview', {

    /**
     * Defines an override applied to a class.
     * @string
     */
    override: 'Shopware.apps.Order.view.detail.Overview',

    createEditElements: function() {
            var me = this;

            var fields = me.callOverridden(arguments);

            var fieldRandom1 = {
                fieldLabel: 'Random 1',
                name:'attribute[ordermodRandom1]',
                xtype: 'textfield'
            };
            var fieldRandom2 = {
                fieldLabel: 'Random 2',
                name:'attribute[ordermodRandom2]',
                xtype: 'textfield'
            };

            fields = Ext.Array.insert(fields, 2, [fieldRandom1, fieldRandom2]);

            return fields;
     }
});
//{/block}
// File location: OrderMod/Views/backend/order/model/ordermod/attribute.js
//{block name="backend/order/model/attribute/fields" append}
{ name: 'ordermodRandom1', type: 'string', useNull: true },
{ name: 'ordermodRandom2', type: 'string', useNull: true },
//{/block}
<?php
class Shopware_Plugins_Frontend_OrderMod_Bootstrap extends Shopware_Components_Plugin_Bootstrap
{
	
	/**
	 * (non-PHPdoc)
	 * @see Shopware_Components_Plugin_Bootstrap::install()
	 */
	public function install()
	{
		$this->_createEvents();
		$this->installOrderAttributes();

		return array('success' => true, 'invalidateCache' => array('backend', 'proxy','template'));
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Shopware_Components_Plugin_Bootstrap::uninstall()
	 */
	public function uninstall()
	{
		$this->getEntityManager()->removeAttribute('s_order_attributes', 'ordermod', 'Random1');
		$this->getEntityManager()->removeAttribute('s_order_attributes', 'ordermod', 'Random2');
		
		$metaDataCacheDoctrine = $this->getEntityManager()->getConfiguration()->getMetadataCacheImpl();
		$metaDataCacheDoctrine->deleteAll();
		
		$this->getEntityManager()->generateAttributeModels('s_order_attributes');

		parent::uninstall();
		return true;
	}
	
	/**
	 * returns shopware model manager
	 * @return Ambigous <\Shopware\Components\Model\ModelManager, mixed, multitype:>
	 */
	public function getEntityManager()
	{
		return Shopware()->Models();
	}

	/**
	 * overwrite sql save order statement
	 * @param Enlight_Event_EventArgs $args
	 * @return string
	 */
	public function onSaveOrder(Enlight_Event_EventArgs $args)
	{
		$orderAttributes = $args->getReturn();
		$orderNumber = $orderAttributes['ordernumber'];
		$order = $this->getEntityManager()->getRepository('Shopware\Models\Order\Order')->findOneBy(
			array('number' => $orderNumber)
		);
		
		$orderAttributeModel = $this->getEntityManager()->getRepository('Shopware\Models\Attribute\Order')->findOneBy(
			array('orderId' => $order->getId())
		);
		
		if ($orderAttributeModel instanceof \Shopware\Models\Attribute\Order) {
			$orderAttributeModel->setOrdermodRandom1(rand(1,49));
			$orderAttributeModel->setOrdermodRandom2(rand(1,49));
			$this->getEntityManager()->persist($orderAttributeModel);
			$this->getEntityManager()->flush();
		}
		
		$args->setReturn($orderAttributes);
	}
	
	/**
	 * method for integrate new order attributes in shopware order backend
	 * @param Enlight_Event_EventArgs $args
	 */
	public function onBackendOrderPostDispatch(Enlight_Event_EventArgs $args)
	{

		$view = $args->getSubject()->View();

		$this->Application()->Snippets()->addConfigDir($this->Path() . 'Snippets/');

		$args->getSubject()->View()->addTemplateDir(
		    $this->Path() . 'Views/'
		);
		
		if ($args->getRequest()->getActionName() === 'load') {
			$view->extendsTemplate('backend/order/model/ordermod/attribute.js');
 			$view->extendsTemplate('backend/order/view/ordermod/list/list.js');
			$view->extendsTemplate('backend/order/view/ordermod/detail/overview.js');
		}

	}
	
	/**
	 * hooks method for add new order attributes
	 * @param Enlight_Hook_HookArgs $args
	 */
	public function afterGetOrderDataQuery(Enlight_Hook_HookArgs $args)
	{
		/* Diese Methode wird nicht benötigt, da im Order Model bereits eine Verknüpfung
		zu den Order-Attributen enthalten ist - somit werden bereits alle Felder aus s_order_attributes mit geladen
		*/
	}

	/**
	 * (non-PHPdoc)
	 * @see Shopware_Components_Plugin_Bootstrap::getVersion()
	 */
	public function getVersion()
	{
		return '1.0.0';
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Shopware_Components_Plugin_Bootstrap::getInfo()
	 */
	public function getInfo()
	{
		$info = array(
			'version' => '1.0.0',
			'label' => 'Order-Modification Sample',
			'author' => 'shopware.de',
			'copyright' => 'Copyright © 2013, shopware AG',
			'support' => 'info@shopware.de',
			'link' => 'http://www.shopware.de'
		);
		
		return $info;
	}
	
	/**
	 * install new order attributes
	 * @return multitype:boolean multitype:string
	 */
	public function installOrderAttributes()
	{
		Shopware()->Models()->addAttribute(
			's_order_attributes', 
			'ordermod',
			'Random1',
			'DECIMAL(12,4)',
			false,
			0.0000);

		Shopware()->Models()->addAttribute(
			's_order_attributes',
			'ordermod',
			'Random2',
			'DECIMAL(12,4)',
			false,
			0.0000);
		
		$metaDataCacheDoctrine = Shopware()->Models()->getConfiguration()->getMetadataCacheImpl();
		$metaDataCacheDoctrine->deleteAll();
		
		Shopware()->Models()->generateAttributeModels(array('s_order_attributes'));
	}

	/**
	 * create events for plugin
	 */
	protected function _createEvents()
	{
		$this->subscribeEvent('Shopware_Modules_Order_SendMail_FilterVariables','onSaveOrder');

		$this->subscribeEvent('Enlight_Controller_Action_PostDispatch_Backend_Order','onBackendOrderPostDispatch');

		/*
		$event = $this->createEvent(
			'Shopware\Models\Order\Repository::getOrdersQueryBuilder::after', 
			'afterGetOrderDataQuery');
		$this->subscribeEvent($event);
		*/

	}
}