Guest User

Untitled

a guest
Sep 29th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.16 KB | None | 0 0
  1. <?php
  2. require_once '/home/*******/sites/www/app/Mage.php';
  3. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  4. function xml2array($contents, $get_attributes=1, $priority = 'tag') {
  5.     if(!$contents) return array();
  6.  
  7.     if(!function_exists('xml_parser_create')) {
  8.         //print "'xml_parser_create()' function not found!";
  9.         return array();
  10.     }
  11.  
  12.     //Get the XML parser of PHP - PHP must have this module for the parser to work
  13.     $parser = xml_parser_create('');
  14.     xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
  15.    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  16.     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  17.     xml_parse_into_struct($parser, trim($contents), $xml_values);
  18.     xml_parser_free($parser);
  19.  
  20.     if(!$xml_values) return;//Hmm...
  21.  
  22.     //Initializations
  23.     $xml_array = array();
  24.     $parents = array();
  25.     $opened_tags = array();
  26.     $arr = array();
  27.  
  28.     $current = &$xml_array; //Refference
  29.  
  30.     //Go through the tags.
  31.     $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
  32.     foreach($xml_values as $data) {
  33.         unset($attributes,$value);//Remove existing values, or there will be trouble
  34.  
  35.         //This command will extract these variables into the foreach scope
  36.         // tag(string), type(string), level(int), attributes(array).
  37.         extract($data);//We could use the array by itself, but this cooler.
  38.  
  39.         $result = array();
  40.         $attributes_data = array();
  41.          
  42.         if(isset($value)) {
  43.             if($priority == 'tag') $result = $value;
  44.             else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
  45.         }
  46.  
  47.         //Set the attributes too.
  48.         if(isset($attributes) and $get_attributes) {
  49.             foreach($attributes as $attr => $val) {
  50.                 if($priority == 'tag') $attributes_data[$attr] = $val;
  51.                 else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  52.             }
  53.         }
  54.  
  55.         //See tag status and do the needed.
  56.         if($type == "open") {//The starting of the tag '<tag>'
  57.             $parent[$level-1] = &$current;
  58.             if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
  59.                 $current[$tag] = $result;
  60.                 if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
  61.                 $repeated_tag_index[$tag.'_'.$level] = 1;
  62.  
  63.                 $current = &$current[$tag];
  64.  
  65.             } else { //There was another element with the same tag name
  66.  
  67.                 if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
  68.                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  69.                     $repeated_tag_index[$tag.'_'.$level]++;
  70.                 } else {//This section will make the value an array if multiple tags with the same name appear together
  71.                     $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
  72.                     $repeated_tag_index[$tag.'_'.$level] = 2;
  73.                      
  74.                     if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  75.                         $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  76.                         unset($current[$tag.'_attr']);
  77.                     }
  78.  
  79.                 }
  80.                 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
  81.                 $current = &$current[$tag][$last_item_index];
  82.             }
  83.  
  84.         } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
  85.             //See if the key is already taken.
  86.             if(!isset($current[$tag])) { //New Key
  87.                 $current[$tag] = $result;
  88.                 $repeated_tag_index[$tag.'_'.$level] = 1;
  89.                 if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
  90.  
  91.             } else { //If taken, put all things inside a list(array)
  92.                 if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
  93.  
  94.                     // ...push the new element into that array.
  95.                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  96.                      
  97.                     if($priority == 'tag' and $get_attributes and $attributes_data) {
  98.                         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  99.                     }
  100.                     $repeated_tag_index[$tag.'_'.$level]++;
  101.  
  102.                 } else { //If it is not an array...
  103.                     $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
  104.                     $repeated_tag_index[$tag.'_'.$level] = 1;
  105.                     if($priority == 'tag' and $get_attributes) {
  106.                         if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  107.                              
  108.                             $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  109.                             unset($current[$tag.'_attr']);
  110.                         }
  111.                          
  112.                         if($attributes_data) {
  113.                             $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  114.                         }
  115.                     }
  116.                     $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
  117.                 }
  118.             }
  119.  
  120.         } elseif($type == 'close') { //End of tag '</tag>'
  121.             $current = &$parent[$level-1];
  122.         }
  123.     }
  124.      
  125.     return($xml_array);
  126. }
  127.  
  128.     // On récupère le fichier de stock
  129.     $contents = file_get_contents('http://*******.fr/mag/feeds/stock.xml');//Or however you what it
  130.     $result = xml2array($contents,0);
  131.     $debug = false;
  132.     $jc_products = array(4458, 4459, 4452, 4451, 4453, 4412, 4413, 4406, 4408, 1059, 1058, 1052, 1051, 1053, 1032, 1033, 1030, 2259, 2258, 2252, 2251, 2253, 2208, 2243, 2210);
  133.     // On parcours chaque item
  134.     foreach($result['rss']['channel']['item'] as $item) {
  135.         // On check si c'est un produit de jc
  136.         if (in_array(str_replace('-ott', '', $item['ref']), $jc_products)) {
  137.             if($debug)
  138.                 echo ' - ' . $item['quantite'];
  139.             $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', str_replace('-ott', '', $item['ref']));
  140.             $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
  141.             if($debug)
  142.                 echo ' - ' . $stock->getQty() . '<br />';
  143.             // Si l'item n'est pas à jour on le met à jour tant que la modif n'a pas été prise en compte
  144.             if($item['quantite']!=$stock->getQty()) {
  145.                 do {
  146.                     $stock->setQty($item['quantite']);
  147.                     $stock->save();
  148.                 }
  149.                 while($item['quantite']!=$stock->getQty());
  150.                 if($debug)
  151.                     echo "update<br />";
  152.             }
  153.         }
  154.     }
Advertisement
Add Comment
Please, Sign In to add comment