php 高级示例如何创建2个自定义字段,按顺序填充它们,将它们作为新列显示在后端顺序列表中并制作它们

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 高级示例如何创建2个自定义字段,按顺序填充它们,将它们作为新列显示在后端顺序列表中并制作它们相关的知识,希望对你有一定的参考价值。

// 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);
		*/

	}
}

以上是关于php 高级示例如何创建2个自定义字段,按顺序填充它们,将它们作为新列显示在后端顺序列表中并制作它们的主要内容,如果未能解决你的问题,请参考以下文章

基于 Woocommerce 3 中的 2 个自定义字段的产品正常价格计算

php 如何将自定义过滤器字段添加到没有过滤器插件的Tribe事件日历(在此示例中 - 按类别和标记过滤)。示例:https:

php 将自定义字段(按顺序)添加到电子邮件中

php 将自定义字段(按顺序)添加到电子邮件中

php 将自定义字段(按顺序)添加到电子邮件中

如何修复此 WordPress 功能使其不返回 404 页面?