public function saveAction()
 {
     $sku_list = $this->getRequest()->getParam('sku');
     foreach ($sku_list as $sku) {
         $model = Mage::getModel('import/product');
         $item = $model->getBySku($sku);
         $api = new Mage_Catalog_Model_Product_Api();
         $attribute_api = new Mage_Catalog_Model_Product_Attribute_Set_Api();
         $attribute_sets = $attribute_api->items();
         $productData = array();
         $productData['website_ids'] = array(1);
         $productData['categories'] = array($this->getRequest()->getParam('category'));
         $productData['status'] = 1;
         $productData['name'] = $item->title;
         $productData['description'] = $item->description;
         $productData['short_description'] = $item->short_description;
         $productData['price'] = $item->suggested_price;
         $productData['msrp'] = $item->suggested_price;
         $productData['weight'] = $item->weight;
         $productData['tax_class_id'] = 0;
         $new_product_id = $api->create('simple', $attribute_sets[0]['set_id'], $item->sku, $productData);
         $stockItem = Mage::getModel('cataloginventory/stock_item');
         $stockItem->loadByProduct($new_product_id);
         $stockItem->setData('use_config_manage_stock', 1);
         $stockItem->setData('qty', $item->stock);
         $stockItem->setData('min_qty', 0);
         $stockItem->setData('use_config_min_qty', 1);
         $stockItem->setData('min_sale_qty', 0);
         $stockItem->setData('use_config_max_sale_qty', 1);
         $stockItem->setData('max_sale_qty', 0);
         $stockItem->setData('use_config_max_sale_qty', 1);
         $stockItem->setData('is_qty_decimal', 0);
         $stockItem->setData('backorders', 0);
         $stockItem->setData('notify_stock_qty', 0);
         $stockItem->setData('is_in_stock', 1);
         $stockItem->setData('tax_class_id', 0);
         $stockItem->save();
         $image_type = substr(strrchr($item->image, "."), 1);
         $filename = "tmp." . $image_type;
         $path = Mage::getBaseDir('media') . DS . 'import' . DS;
         if (!is_dir($path)) {
             mkdir($path);
         }
         $fullpath = $path . $filename;
         $ch = curl_init($item->image);
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
         $raw = curl_exec($ch);
         curl_close($ch);
         if (file_exists($fullpath)) {
             unlink($fullpath);
         }
         $fp = fopen($fullpath, 'x');
         fwrite($fp, $raw);
         fclose($fp);
         $product = Mage::getModel('catalog/product')->load($new_product_id);
         $product->setMediaGallery(array('images' => array(), 'values' => array()));
         $product->addImageToMediaGallery($fullpath, array('image', 'small_image', 'thumbnail'), false, false);
         Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
         $product->save();
     }
     $this->_redirect('*/adminhtml_list');
 }
             $stockItem->save();
             /*****Associate simple product into Group product******/
             $products_links = Mage::getModel('catalog/product_link_api');
             $products_links->assign("grouped", $grouped_product_id, $id);
             /**************************************************/
             echo "SKU: " . $simple_sku . " Stock has been UPDATED. <br />";
         } catch (Exception $ex) {
             //Handle the error
             echo 'Caught exception: Stock Update Error: SKU: ' . $simple_sku . ":", $ex->getMessage(), "\n";
         }
     }
 } else {
     //echo "SKU does not exist";
     /*******insert simple product into databse*******/
     $api = new Mage_Catalog_Model_Product_Api();
     $attribute_api = new Mage_Catalog_Model_Product_Attribute_Set_Api();
     $attribute_sets = $attribute_api->items();
     $productData = array();
     $productData['website_ids'] = array(1);
     //	$productData['categories'] = array($new_subcat_id);
     $productData['status'] = 1;
     $productData['visibility'] = 1;
     //not visible individualy
     $productData['name'] = utf8_encode(trim($data[1]));
     $productData['description'] = utf8_encode($data[15]);
     $productData['short_description'] = utf8_encode($data[16]);
     $productData['price'] = $price;
     $productData['weight'] = 1.0;
     $productData['tax_class_id'] = 2;
     $productData['product_collection'] = $product_collection;
     $productData['no_to_show'] = $no_to_show_id;
 public function addproductAction()
 {
     $product = Mage::getModel('catalog/product');
     $post = $this->getRequest()->getPost();
     if ($post) {
         $api = new Mage_Catalog_Model_Product_Api();
         $attribute_api = new Mage_Catalog_Model_Product_Attribute_Set_Api();
         $attribute_sets = $attribute_api->items();
         // Product required params
         $v = $this->_getSession()->getVendor();
         $sku = Mage::helper('udropship')->getRegionCode($v->getRegionId()) . '-' . $v->getId() . '-' . Mage::helper('urls')->slugify($post['name']);
         $sku = Mage::helper('udropship')->checkSku($sku);
         $productData = array();
         $productData['website_ids'] = array(1);
         $productData['status'] = 1;
         $productData['name'] = utf8_encode($post['name']);
         $productData['description'] = utf8_encode($post['short_description']);
         $productData['short_description'] = utf8_encode($post['short_description']);
         $productData['weight'] = 10.0;
         $productData['tax_class_id'] = 2;
         $new_product_id = $api->create('simple', $attribute_sets[0]['set_id'], $sku, $productData);
         $stockItem = Mage::getModel('cataloginventory/stock_item');
         $stockItem->loadByProduct($new_product_id);
         $stockItem->setData('use_config_manage_stock', 1);
         $stockItem->setData('qty', 10000);
         $stockItem->setData('min_qty', 0);
         $stockItem->setData('use_config_min_qty', 1);
         $stockItem->setData('min_sale_qty', 0);
         $stockItem->setData('use_config_max_sale_qty', 1);
         $stockItem->setData('max_sale_qty', 0);
         $stockItem->setData('use_config_max_sale_qty', 1);
         $stockItem->setData('is_qty_decimal', 0);
         $stockItem->setData('backorders', 0);
         $stockItem->setData('notify_stock_qty', 0);
         $stockItem->setData('is_in_stock', 1);
         $stockItem->setData('tax_class_id', 0);
         $stockItem->save();
         $product = Mage::getModel('catalog/product')->load($new_product_id);
         // Image
         if (!empty($post['imageFile'])) {
             if ($tProduct = $this->getRequest()->getParam('temp_product', false)) {
                 // Image
                 $ext = '.' . pathinfo(parse_url($post['imageFile'], PHP_URL_PATH), PATHINFO_EXTENSION);
                 $filename = md5(mt_rand(0, 1111111)) . $ext;
                 // Create name
                 $imgPath = Mage::getBaseDir() . '/media/vendorimageupload/' . $filename;
                 file_put_contents($imgPath, file_get_contents($post['imageFile']));
                 // Move image from facebook to local server
                 $product->setMediaGallery(array('images' => array(), 'values' => array()));
                 $product->addImageToMediaGallery($imgPath, array('image', 'small_image', 'thumbnail'), true, false);
                 // Add to product gallery
                 @unlink($imgPath);
                 // Remove file that was added to magento
             } else {
                 $imgPath = Mage::getBaseDir() . $post['imageFile'];
                 $product->setMediaGallery(array('images' => array(), 'values' => array()));
                 $product->addImageToMediaGallery(Mage::getBaseDir() . $post['imageFile'], array('image', 'small_image', 'thumbnail'), true, false);
                 @unlink($imgPath);
             }
         }
         // Product data
         $product->setFlowerType(implode(',', $post['flower_type']));
         $product->setStyle(implode(',', $post['style']));
         $product->setSize(implode(',', $post['size']));
         $product->setOccasions(implode(',', $post['occasions']));
         $product->setColor(implode(',', $post['color']));
         $product->setUdropshipVendor($this->_getSession()->getId());
         $product->setPrice(number_format($post['price'], 2, null, ''));
         $cat = Mage::helper('udropship')->getCategoryByVendor($this->_getSession()->getId());
         if ($cat) {
             $product->assignAttrCategories($cat->getId());
         }
         // Mark product as invisible if it's 'Other goods'
         if (in_array(55, $product->getCategoryIds())) {
             $product->setVisibility(1);
         } else {
             $product->setVisibility(4);
         }
         Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
         try {
             $product->save();
             if ($tProduct = $this->getRequest()->getParam('temp_product', false)) {
                 Mage::getSingleton('core/resource')->getConnection('core_write')->query('DELETE FROM udropship_temp_product WHERE id=' . $tProduct);
             }
         } catch (Exception $e) {
         }
         Mage::helper('custom')->updateZipcodeIndex($product->getId(), $this->_getSession()->getVendor()->getLimitZipcode());
         // Update vendor qty
         Mage::helper('udropship/catalog')->massUpdateStock(true, true);
         // Add success message
         $this->_getSession()->addSuccess('Product ' . $product->getName() . ' was added');
         // Redirect
         $this->_redirect('udropship/vendor/manageproducts');
     }
     // Thing required for standalone page display
     $this->_setTheme();
     $this->loadLayout();
     $this->getLayout()->getBlock('addproduct')->setProduct($product);
     $this->renderLayout();
 }
Example #4
0
 public function addBatchProduct($data)
 {
     // Required keys
     $requiredData = array('imgSrc', 'productName', 'productPrice');
     // Get needed APIs instances
     $api = new Mage_Catalog_Model_Product_Api();
     $attribute_api = new Mage_Catalog_Model_Product_Attribute_Set_Api();
     $attribute_sets = $attribute_api->items();
     // Check if data was passed in correctly
     foreach ($requiredData as $req) {
         if (!isset($data[$req]) || empty($data[$req])) {
             throw new Exception('Please make sure that all keys of passed data array exist and not empty:' . implode(',', $requiredData));
         }
     }
     // Product required params
     $sku = strtoupper(preg_replace('/\\W/', '-', $data['productName']));
     $productData = array();
     $productData['website_ids'] = array(1);
     $productData['status'] = 1;
     $productData['name'] = utf8_encode($data['productName']);
     $productData['description'] = '';
     $productData['short_description'] = '';
     $productData['weight'] = 10.0;
     $productData['tax_class_id'] = 2;
     $new_product_id = $api->create('simple', $attribute_sets[0]['set_id'], $sku, $productData);
     $stockItem = Mage::getModel('cataloginventory/stock_item');
     $stockItem->loadByProduct($new_product_id);
     $stockItem->setData('use_config_manage_stock', 1);
     $stockItem->setData('qty', 10000);
     $stockItem->setData('min_qty', 0);
     $stockItem->setData('use_config_min_qty', 1);
     $stockItem->setData('min_sale_qty', 0);
     $stockItem->setData('use_config_max_sale_qty', 1);
     $stockItem->setData('max_sale_qty', 0);
     $stockItem->setData('use_config_max_sale_qty', 1);
     $stockItem->setData('is_qty_decimal', 0);
     $stockItem->setData('backorders', 0);
     $stockItem->setData('notify_stock_qty', 0);
     $stockItem->setData('is_in_stock', 1);
     $stockItem->setData('tax_class_id', 0);
     $stockItem->save();
     $product = Mage::getModel('catalog/product')->load($new_product_id);
     // Image
     $ext = '.' . pathinfo(parse_url($data['imgSrc'], PHP_URL_PATH), PATHINFO_EXTENSION);
     $filename = md5(mt_rand(0, 1111111)) . $ext;
     // Create name
     $imgPath = Mage::getBaseDir() . '/media/vendorimageupload/' . $filename;
     file_put_contents($imgPath, file_get_contents($data['imgSrc']));
     // Move image from facebook to local server
     $product->setMediaGallery(array('images' => array(), 'values' => array()));
     $product->addImageToMediaGallery($imgPath, array('image', 'small_image', 'thumbnail'), true, false);
     // Add to product gallery
     @unlink($imgPath);
     // Remove file that was added to magento
     // Product data
     $product->setUdropshipVendor(Mage::getSingleton('udropship/session')->getId());
     $product->setPrice(number_format($data['productPrice'], 2, null, ''));
     $cat = Mage::helper('udropship')->getCategoryByVendor(Mage::getSingleton('udropship/session')->getId());
     if ($cat) {
         $product->setCategoryIds(array($cat->getId()));
     }
     Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
     $product->save();
 }