alaa / magento2-xml-feed-model
2.0.0
2018-12-23 21:24 UTC
Requires
- php: ~7.0.0|~7.1.0
- magento/framework: 101.0.*
- magento/module-store: 100.2.*
This package is auto-updated.
Last update: 2024-09-24 03:23:32 UTC
README
安装
composer install alaa/magento2-xml-feed-model
php -f bin/magento module:enable Alaa_XmlFeedModel
php -f bin/magento setup:upgrade
工作原理
设置
首先,需要创建一个配置文件。
<?php
return [
'order' => 'mapper/mapped_fields/order', // order is the parent node of the xml document => xml config path in the system.xml file
'order.customer' => 'mapper/mapped_fields/customer', // order/customer is the xml path to the customer node in the document => xml config path in the system.xml file
'order.customer.customer_address' => 'mapper/mapped_fields/customer_address', // order/customer/customer_address => xml config path in the system.xml file
'order.items' => '', // order/items contains children item nodes => empty xml config path, no data mapping is required for this node
'order.items.item' => 'mapper/mapped_fields/item', // order/items/item contains item nodes => xml config path in the system.xml file
];
然后,使用 system.xml
文件在后台创建字段映射
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="xml_feed_model_example">
<label>Xml Feed Model Example</label>
</tab>
<section id="mapper" showInStore="1" showInWebsite="1" showInDefault="1" translate="label" sortOrder="1">
<label>Order Feed Fields</label>
<tab>xml_feed_model_example</tab>
<resource>{YOUR ACL RESOURCE}</resource>
<group id="mapped_fields" showInStore="1" showInWebsite="1" showInDefault="1" translate="label" sortOrder="1">
<label>Mapped Fields</label>
<field id="order" showInStore="1" showInWebsite="1" showInDefault="1" translate="label" sortOrder="1">
<label>Order Fields</label>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<frontend_model>Alaa\XmlFeedModel\Block\Adminhtml\System\Config\Form\Field\FieldArray\SubjectFieldArray</frontend_model>
</field>
</group>
</section>
</system>
</config>
使用预定义的类 Alaa\XmlFeedModel\Block\Adminhtml\System\Config\Form\Field\FieldArray\SubjectFieldArray
进行数组键的映射
现在,应该能够添加所需的任意数量的映射。
用法
解析xml表示
$order = $this->orderRepository->get($orderId);
$orderSubject = new Subject('order', $order->getData()); // corresponds to 'order' => 'mapper/mapped_fields/order', in the configuration file
$orderSubject->addAttribute($orderSubject->getNodeName(), 'account', '111');
$customerSubject = new Subject('customer');
$customerSubject->setData($order->getData());
$customerSubject->addAttribute($customerSubject->getNodeName(), 'id', (string) $order->getCustomerId());
$customerAddress = new Subject('customer_address');
$customerAddress->setData($order->getBillingAddress()->getData());
$itemsSubject = new Subject('items');
foreach ($order->getItems() as $item) {
if ($item->getParentItem()) {
continue;
}
$itemsSubject->addChild(new Subject('item', $item->getData()));
}
$orderSubject->addChild($customerSubject);
$customerSubject->addChild($customerAddress);
$orderSubject->addChild($itemsSubject);
将主题写入xml
// Reading the configuration file
$file = $this->moduleDirReader->getModuleDir('etc', 'YOUR_MODULE'). '/order_mapped_fields.php';
// do the mappings
/** Alaa\XmlFeedModel\Model\MappedSubjectBuilder $this->mappedSubjectBuilder */
$mappedSubject = $this->mappedSubjectBuilder->build($file, $orderSubject);
// convert the data to xml
/** @var \Alaa\XmlFeedModel\Model\XmlConverter $this->xmlConverter */
$xml = $this->xmlConverter->convert($mappedSubject);
// write the xml to a file
$xml->asXML('var/order/subject.xml');
贡献
欢迎提出问题和贡献力量。
许可证
MIT