Example #1
0
 /**
  * Product create or update in WMS
  *
  * @param int $pid            
  * @return Zoffio_WmsApi_Model_Observer
  */
 public function productCreateOrUpdateWMS(int $pid)
 {
     // load Product using the Id
     $product = Mage::getModel('catalog/product')->load($pid);
     $product_type = $product->getData('type_id');
     // Do call api only if the added product is simple
     if ($product_type === 'simple') {
         // Establish the connection to WMS api
         $proxy = new SoapClient(self::$_apiUrl);
         $sessionId = $proxy->login(self::$_apiUser, self::$_apiPassword);
         // Product details
         $product_sku = $product->getData('sku');
         $product_categories = $product->getCategoryIds();
         $product_name = $product->getData('name');
         $product_description = $product->getData('description');
         $product_short_description = $product->getData('short_description');
         $product_price = $product->getData('price');
         $product_weight = $product->getData('weight');
         $product_status = $product->getData('status');
         $product_url_key = $product->getData('url_key');
         $product_url_path = $product->getData('url_path');
         $product_visibility = $product->getData('visibility');
         $product_meta_title = $product->getData('meta_title');
         $product_meta_keyword = $product->getData('meta_keyword');
         $product_meta_description = $product->getData('meta_description');
         // Load product inventory
         $product_inventory = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
         // Mage::log($product_inventory->getData(), null, 'shebin.log');
         // Load product Inverntry details
         $product_qty = $product_inventory->getData('qty');
         $product_is_in_stock = $product_inventory->getData('is_in_stock');
         $product_manage_stock = $product_inventory->getData('manage_stock');
         $product_use_config_manage_stock = $product_inventory->getData('use_config_manage_stock');
         // Get product image
         $product_image = $product->getImage();
         // Load Product Main Image details if Product image exists
         if ($product_image != "no_selection" && $product_image != null) {
             $product_image_url = Mage::getModel('catalog/product_media_config')->getMediaUrl($product_image);
             // get the contents of the image file from the Url
             $product_image_content = file_get_contents($product_image_url);
             $product_image_name = pathinfo($product_image_url, PATHINFO_FILENAME);
             /* get image mime type */
             $file_info = new finfo(FILEINFO_MIME);
             // object oriented
             // approach!
             $product_image_mime_type = substr($file_info->buffer($product_image_content), 0, strpos($file_info->buffer($product_image_content), ';'));
             // File Object to be send via the API
             $product_image_file = array('name' => $product_image_name, 'content' => base64_encode($product_image_content), 'mime' => $product_image_mime_type);
             // Image deails to be added or created at WMS
             $product_image_details = array('file' => $product_image_file, 'types' => array('thumbnail', 'small_image', 'image'), 'exclude' => 0);
         }
         // Product details to be sent to WMS
         $product_details = array('categories' => $product_categories, 'websites' => array(1), 'name' => $product_name, 'description' => $product_description, 'short_description' => $product_short_description, 'price' => $product_price, 'weight' => $product_weight, 'status' => $product_status, 'url_key' => $product_url_key, 'url_path' => $product_url_path, 'visibility' => $product_visibility, 'tax_class_id' => $product_tax_class_id, 'meta_title' => $product_meta_title, 'meta_keyword' => $product_meta_keyword, 'meta_description' => $product_description, 'stock_data' => array('qty' => $product_qty, 'is_in_stock' => $product_is_in_stock, 'manage_stock' => $product_manage_stock, 'use_config_manage_stock' => $product_use_config_manage_stock));
         // Get attribute set
         $attributeSets = $proxy->catalogProductAttributeSetList($sessionId);
         $attributeSet = current($attributeSets);
         // TODO removal of product image
         // Fetch product details using the SKU from WMS
         try {
             $wms_product_exist = $proxy->catalogProductInfo($sessionId, $product_sku);
             // Mage::log($wms_product_exist, null, 'shebin.log');
         } catch (Exception $e) {
             Mage::log("WMS: No such products!", 1, 'wms-api.log');
         }
         // If Product not exists in WMS then create
         if ($wms_product_exist == null || $wms_product_exist == "") {
             try {
                 $result = $proxy->catalogProductCreate($sessionId, $product_type, $attributeSet->set_id, $product_sku, $product_details);
                 // set the product WMS ID.
                 Mage::log($result, null, 'wms-api.log');
                 $product->setData('wms_pid', $result);
                 $product->getResource()->saveAttribute($product, 'wms_pid');
                 // Mage::log("CreateProduct:", null, 'shebin.log');
                 // Mage::log($result, null, 'shebin.log');
             } catch (Exception $e) {
                 Mage::log($e->getMessage(), 3, 'wms-api.log');
                 Mage::log($e->getTraceAsString(), 3, 'wms-api.log');
                 Mage::log($product_details, null, 'wms-api.log');
             }
             // Product image add to WMS
             if ($product_image != "no_selection" && $product_image != null) {
                 try {
                     $result = $proxy->catalogProductAttributeMediaCreate($sessionId, $product_sku, $product_image_details);
                     // Mage::log($result." :Image Create on Update", null,
                     // 'shebin.log');
                 } catch (Exception $e) {
                     Mage::log($e->getMessage() . 'Create fail', 3, 'wms-api.log');
                     Mage::log($product_image_details, null, 'wms-api.log');
                 }
             }
         } else {
             // else Update the existing product
             try {
                 $result = $proxy->catalogProductUpdate($sessionId, $product_sku, $product_details);
                 // Mage::log("UpdateProduct:", null, 'shebin.log');
                 // Mage::log($result, null, 'shebin.log');
             } catch (Exception $e) {
                 Mage::log($e->getMessage(), 3, 'wms-api.log');
                 Mage::log($product_details, null, 'wms-api.log');
             }
             // if product image does not Exists create product image else
             // update
             if ($product_image != "no_selection" && $product_image != null) {
                 try {
                     // check exixstence of the product image
                     $result2 = $proxy->catalogProductAttributeMediaInfo($sessionId, $product_sku, $product_image);
                     // Mage::log($result2 , null, 'wms-api.log');
                 } catch (Exception $e) {
                     Mage::log($e->getMessage() . " WMS: No products image!", 1, 'wms-api.log');
                     // Mage::log($proxy->catalogProductAttributeMediaList(
                     // $sessionId, $product_sku), 1, 'wms-api.log');
                 }
                 // Check product image Exist or not
                 if ($result2 == null || ($result2 = "")) {
                     try {
                         $result = $proxy->catalogProductAttributeMediaCreate($sessionId, $product_sku, $product_image_details);
                         // Mage::log($result." :Image Create on Update",
                         // null,
                         // 'shebin.log');
                     } catch (Exception $e) {
                         Mage::log($e->getMessage() . 'Create fail', 3, 'wms-api.log');
                         Mage::log($product_image_details, null, 'wms-api.log');
                     }
                 } else {
                     // Product image Update
                     try {
                         $result = $proxy->catalogProductAttributeMediaUpdate($sessionId, $product_sku, $product_image, $product_image_details);
                         // Mage::log($result."Image update on Update", null,
                         // 'shebin.log');
                     } catch (Exception $e) {
                         Mage::log($e->getMessage() . " Update fail", 3, 'wms-api.log');
                         Mage::log($product_image_details, null, 'wms-api.log');
                     }
                 }
             }
         }
     } else {
         Mage::log($product_type . " products will not be added to WMS!", 5, 'wms-api.log');
         return $this;
     }
     $proxy->endSession($sessionId);
     // closing session
     return $this;
 }