将 XML 从 URL 导入/解析到 Magento 中
我有一个 URL,其中包含产品的 XML。
我想做的是,如果可能的话,解析内容,然后将详细信息作为产品导入到 Magento 中。
这是否可能?如果可以,我需要采取哪些步骤来执行此操作?
谢谢
编辑:
import_products.php
require_once('app/Mage.php');
class import_products extends Mage_Core_Model_Abstract
{
public static function url_contents($url)
{
$crl = curl_init();
$timeout = 5;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}
}
import.php
require_once('import_products.php');
$app = Mage::app('default');
$product = Mage::getSingleton('catalog/product');
$objDOM = new DOMDocument();
$objDOM->load("http://www.example.com/products.xml");
$note = $objDOM->getElementsByTagName("car");
// for each note tag, parse the document and get values for
// tasks and details tag.
foreach( $note as $value )
{
$car_ids = $value->getElementsByTagName("Car_ID");
$car_id = $car_ids->item(0)->nodeValue;
$makes = $value->getElementsByTagName("Make");
$make = $makes->item(0)->nodeValue;
$models = $value->getElementsByTagName("Model");
$model = $models->item(0)->nodeValue;
$descriptions = $value->getElementsByTagName("Description");
$description = $descriptions->item(0)->nodeValue;
$short_descriptions = $value->getElementsByTagName("Specification");
$short_description = $short_descriptions->item(0)->nodeValue;
$prices = $value->getElementsByTagName("Price");
$price = $prices->item(0)->nodeValue;
$simple = 'simple';
$product->setAttributeSetId(4);
$product->setSku($task);
$product->setName($detail);
$product->setTypeId($simple);
$product->setPrice($price);
$product->setValue($price);
$product->setDescription($description);
$product->setShortDescription($short_description);
$product->setWeight(100);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$product->setTaxClassId(2);
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));
try {
$product->save();
echo "Saved";
}
catch (Exception $ex) {
echo "<pre>".$ex."</pre>";
}
//echo "$car_id :: $make :: $model :: $description :: $short_description :: $price <br /><br />";
}
I have a URL, which contains XML of products.
What I'm looking to do, is, if possible, parse the contents and then import the details as products into Magento.
Is this possible and If so, what would be the steps I need to do to carry this out?
Thanks
EDIT:
import_products.php
require_once('app/Mage.php');
class import_products extends Mage_Core_Model_Abstract
{
public static function url_contents($url)
{
$crl = curl_init();
$timeout = 5;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}
}
import.php
require_once('import_products.php');
$app = Mage::app('default');
$product = Mage::getSingleton('catalog/product');
$objDOM = new DOMDocument();
$objDOM->load("http://www.example.com/products.xml");
$note = $objDOM->getElementsByTagName("car");
// for each note tag, parse the document and get values for
// tasks and details tag.
foreach( $note as $value )
{
$car_ids = $value->getElementsByTagName("Car_ID");
$car_id = $car_ids->item(0)->nodeValue;
$makes = $value->getElementsByTagName("Make");
$make = $makes->item(0)->nodeValue;
$models = $value->getElementsByTagName("Model");
$model = $models->item(0)->nodeValue;
$descriptions = $value->getElementsByTagName("Description");
$description = $descriptions->item(0)->nodeValue;
$short_descriptions = $value->getElementsByTagName("Specification");
$short_description = $short_descriptions->item(0)->nodeValue;
$prices = $value->getElementsByTagName("Price");
$price = $prices->item(0)->nodeValue;
$simple = 'simple';
$product->setAttributeSetId(4);
$product->setSku($task);
$product->setName($detail);
$product->setTypeId($simple);
$product->setPrice($price);
$product->setValue($price);
$product->setDescription($description);
$product->setShortDescription($short_description);
$product->setWeight(100);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$product->setTaxClassId(2);
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));
try {
$product->save();
echo "Saved";
}
catch (Exception $ex) {
echo "<pre>".$ex."</pre>";
}
//echo "$car_id :: $make :: $model :: $description :: $short_description :: $price <br /><br />";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以在Magento中获取参数并使用简单的xml将字符串转换为xml(
如果它是你想要获取的文件)你可以使用以下代码设置一个php文件来获取url的内容
}
你可以使用该函数,然后我们在上面的位置
您应该能够执行此操作
然后您可以使用简单的 xml 并将节点分配给产品数组,您可以在其中拥有产品集合,然后循环遍历这些产品并以编程方式创建产品在 Magento 中,如果有类似的内容,显然可以更改可能为您可能需要的内容进行硬编码的值。
you could get the parameter in Magento and use simple xml to turn the string into xml
if it is a file you are trying to get you can setup a php file with the following code in it to get the contents of the url
}
You can use that function and then where in the above we had
You should be able to do this
Then you could use simple xml and assign the nodes to a products array, where you could have a collection of products and then loop through those and programmatically create the products in Magento with something like this, obviously change the values that might be hard coded for whatever you might need.