我有自定义观察者.php文件,该文件在创建订单后创建xml文件。 有关于发票和送货地址的所有信息,但我也需要导出项目。我尝试了更多的样式来让它工作,但仍然没有适合我。我必须向观察者添加什么.php以包含所有项目信息?
<?php
class JULEC_Model_Observer extends Mage_Core_Model_Abstract {
public function exportObjednavky(Varien_Event_Observer $observer) {
$orderIds = $observer->getOrderIds();
for($i = 0; $i < count($orderIds); $i++) {
$order = Mage::getModel('sales/order')->load($orderIds[$i]);
$billing = $order->getBillingAddress();
$shipping = $order->getShippingAddress();
$fileContent = '<?xml version="1.0" encoding="utf-8" ?>
<mrpEnvelope>
<body>
<mrpRequest>
<request command="IMPEO0" requestId="' . date("Y-m-d-H-i-s") . '">
</request>
<data>
<params>
<paramvalue name="cisloSkladu">1</paramvalue>
<paramvalue name="stredisko">0</paramvalue>
<paramvalue name="cisloZakazky">0</paramvalue>
</params>
<objednavka formaUhrady="Dobierka" puvodniCislo="' . $order->getIncrementId() . '" datum="' . date("Y-m-d") . '" cenySDPH="F">
<mena kod="EUR" kurz="1" mnozstvi="1"/>
<adresa id="A' . $order->getIncrementId() . '" ulice="' . implode(' ', $billing->getStreet()) . '" mesto="' . $billing->getCity() . '" psc="' . $billing->getPostcode() . '">
<firma nazev="' . $billing->getLastname() . ' ' . $billing->getFirstname() . '" ico="X' . $order->getIncrementId() . '" dic=""/>
<osoba jmeno="' . $billing->getCompany() . '" prijmeni=""/>
<email>' . $billing->getEmail() . '</email>
<tel>' . $billing->getTelephone() . '</tel>
</adresa>';
$sameAddress = $this->compareAddresses($billing, $shipping);
if(!$sameAddress) {
$fileContent .= '
<adresa_dod id="" ulice="' . implode(' ', $shipping->getStreet()) . '" mesto="' . $shipping->getCity() . '" psc="' . $shipping->getPostcode() . '">
<firma nazev="' . $shipping->getLastname() . ' ' . $shipping->getFirstname() . '" ico="X' . $order->getIncrementId() . '" dic=""/>
<osoba jmeno="' . $shipping->getCompany() . '" prijmeni=""/>
<email>' . $shipping->getEmail() . '</email>
<tel>' . $shipping->getTelephone() . '</tel>
</adresa_dod>
';
}
//need to get here list of items with its values //
$fileContent .= '<ITEMS><item =" ???SKU??? "text=" ??? ITEM NAME ??? "price=" ??? ITEM PRICE ???? "qty=" ??? ITEM QTY ??? "tax=" ??? ITEM TAX RATE ??? " /></ITEMS>
</objednavka>
</data>
</mrpRequest>
</body>
</mrpEnvelope>
';
file_put_contents('var/export_objednavky/' . date("Y-m-d-H-i-s") . '-' . $order->getIncrementId() . '.xml', $fileContent);
}
}
public function compareAddresses($address1, $address2) {
if (!is_null($address1) && !is_null($address2)) {
if ($address1->getFirstname() != $address2->getFirstname())
return false;
if ($address1->getLastname() != $address2->getLastname())
return false;
if ($address1->getCompany() != $address2->getCompany())
return false;
if (implode(' ', $address1->getStreet()) != implode(' ', $address2->getStreet()))
return false;
if ($address1->getCountryId() != $address2->getCountryId())
return false;
if ($address1->getPostcode() != $address2->getPostcode())
return false;
if ($address1->getCity() != $address2->getCity())
return false;
}
return true;
}
}
如果我理解正确,那么您可以尝试如下内容:
foreach ($order->getAllItems() as $item) {
$itemSku = $item->getSku();
$itemName = $item->getName();
$itemPrice = $item->getPrice()
/* etc */
}