public
Last active

  • Download Gist
gistfile1.phtml
HTML+PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
<?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"]);
}
}
}
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.