Skip to content

Instantly share code, notes, and snippets.

@sthamann
Last active March 19, 2021 12:14
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save sthamann/5343247 to your computer and use it in GitHub Desktop.
Save sthamann/5343247 to your computer and use it in GitHub Desktop.
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
<?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);
*/
}
}
// 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}
// 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/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}
@testusernetwork1
Copy link

I have installed the above plugin in my shopware4.3.3.

When i tried to finish checkout,The following error occured in the below link
http://www.********.de/checkout/finish

Here is the Error:
Fatal error: Call to undefined method Shopware\Models\Attribute\Order::setOrdermodRandom1() in /home/raptis-family/www/htdocs/engine/Shopware/Plugins/Local/Frontend/OrderMod/Bootstrap.php on line 62
503 Service Unavailable

Please help me to find a solution

Thankyou

@tsia
Copy link

tsia commented Sep 4, 2015

the set methods are generated automatically by using prefix and column from the addAttribute() call. (2nd and 3rd parameter).

just have a look inside cache/doctrine/attributes/Order.php. In my case around line 240 i found the according get and set methods.

@dielobby
Copy link

I´m looking for a possibility to get the variables in the email templates. Can you give me a tip how I can do this?

@dielobby
Copy link

I´ve found out, that the variables are in the system mails, but not in the status mails. Is there a possibility to get it there?

@renanogueira
Copy link

Hi Guys! How could I create 2 input fields into the last page of shopping and use the set method?
For example: http://imgur.com/a/jdqSj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment