Skip to content

Instantly share code, notes, and snippets.

@roderik
Created July 29, 2008 05:50
Show Gist options
  • Save roderik/3024 to your computer and use it in GitHub Desktop.
Save roderik/3024 to your computer and use it in GitHub Desktop.
<?php
class Loveno_AdminRest_ProductController extends Mage_Adminhtml_Controller_Action {
public function preDispatch() {
parent::preDispatch();
$session = Mage::getSingleton('admin/session');
$request = $this->getRequest();
$user = $session->getUser();
if (!$user) {
//var_dump('no login');
$request->setDispatched(FALSE);
}
}
public function indexAction() {
echo "[".date(DATE_RFC822).'] <a href="/adminrest/product/cat">Start by importing the categories.</a>';
}
public function catAction() {
echo "[".date(DATE_RFC822)."] Start importing categories<br />";
$client = new SoapClient('http://magento.tenedos.local.vanderveer.be/api/?wsdl');
$session = $client->login('test', 'testkey');
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('catalog/category_collection')->addAttributeToSelect('*');
$products->getEntity()->setStore($storeId);
$products->load();
$allCategories = $client->call($session, 'category.tree');
$this->removeCats($client, $session, $allCategories);
$list = $products->toArray();
foreach ($list as $product) {
if ($product["entity_id"] > 3){
echo "[".date(DATE_RFC822)."] -->" . $product['name'] . " <br />";
$current = $product;
$pstring = $current["entity_id"];
$cont = true;
while ($cont){
if($product["entity_id"] != $current["entity_id"]){
$pstring = $current["entity_id"] . "/" . $pstring;
}
$current = $this->getParentFor($current);
if($current["parent_id"] == 0){
$cont = false;
}
}
$pstring = '1/' . $pstring;
$product['category_id'] = $product["entity_id"];
$product['attribute_set_id'] = "12";
$product['path'] = $pstring;
$client->call($session,'category.create',array($product["parent_id"],$product));
}
}
echo "[".date(DATE_RFC822)."] Done importing categories<br />";
echo "[".date(DATE_RFC822).'] <a href="/adminrest/product/prod">Continue by importing the products.</a>';
}
private function getParentFor($current){
$products = Mage::getResourceModel('catalog/category_collection')->addAttributeToSelect('*');
$storeId = Mage::app()->getStore()->getId();
$products->getEntity()->setStore($storeId);
$products->addFieldToFilter('entity_id', $current["parent_id"]);
$collection = $products->load()->getItems();
$product = $collection[$current["parent_id"]];
$product = $product->toArray();
return $product;
}
private function removeCats(&$client, &$session, $cats){
foreach ($cats["children"] as $child){
$this->removeCats($client, $session, $child);
if ($child["category_id"] > 3){
$client->call($session, 'category.delete', $child["category_id"]);
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment