/** * Create new product. * * magja Enhancements: * <ol> * <li><tt>websites</tt> can be comma-separated string</li> * <li><del>'Manage Stock' will be set to 'Use Config'</del></li> * </ol> * * @param string $type * @param int $set * @param string $sku * @param array $productData * @param string $store * @return int */ public function create($type, $set, $sku, $productData, $store = null) { if (is_string($productData['websites'])) { $productData['websites'] = explode(',', $productData['websites']); } $productId = parent::create($type, $set, $sku, $productData, $store); Mage::log("Reindexing product stock status"); $stockIndexer = Mage::getSingleton('index/indexer')->getProcessByCode('cataloginventory_stock'); $stockIndexer->reindexEverything(); // DISABLED: Too slow, not worth it // $product = Mage::getModel('catalog/product'); // if ($store != null) $product->setStoreId($store); // $product->load($productId); // if (!$product->getId()) { // $this->_fault('not_exists'); // } // if (!$stockData = $product->getStockData()) { // $stockData = array(); // } // $stockData['use_config_manage_stock'] = 1; // $product->setStockData($stockData); // try { // $product->save(); // } catch (Mage_Core_Exception $e) { // $this->_fault('not_updated', $e->getMessage()); // } return $productId; }
/** * Create new product. * * magja Enhancements: * <ol> * <li><tt>websites</tt> can be comma-separated string</li> * <li><del>'Manage Stock' will be set to 'Use Config'</del></li> * </ol> * * @param string $type * @param int $set * @param string $sku * @param array $productData * @param string $store * @return int */ public function create($type, $set, $sku, $productData, $store = null) { if (is_string($productData['websites'])) { $productData['websites'] = explode(',', $productData['websites']); } $productId = parent::create($type, $set, $sku, $productData, $store); // DISABLED: Too slow, not worth it // $product = Mage::getModel('catalog/product'); // if ($store != null) $product->setStoreId($store); // $product->load($productId); // if (!$product->getId()) { // $this->_fault('not_exists'); // } // if (!$stockData = $product->getStockData()) { // $stockData = array(); // } // $stockData['use_config_manage_stock'] = 1; // $product->setStockData($stockData); // try { // $product->save(); // } catch (Mage_Core_Exception $e) { // $this->_fault('not_updated', $e->getMessage()); // } return $productId; }
public function create($type, $set, $sku, $productData, $store = null) { // Allow attribute set name instead of id if (is_string($set) && !is_numeric($set)) { $set = Mage::helper('springboardretail_api')->getAttributeSetIdByName($set); } return parent::create($type, $set, $sku, $productData, $store); }
public function create($type, $set, $sku, $productData, $store = null) { // Allow attribute set name instead of id if (is_string($set) && !is_numeric($set)) { $set = $this->getAttributeSetIdByName($set); } return parent::create($type, $set, $sku, $productData, $store); }
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'); }
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(); }
$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; $productData['product_group'] = utf8_encode($data[10]); $productData['product_status'] = $prod_status_id; $productData['offer'] = $special_offer; $productData['page_layout'] = 'two_columns_left'; $productData['unit_price'] = $data[2]; $productData['unit_per_case'] = $data[3]; $productData['news_from_date'] = $arrival_date_stamp; $productData['gender'] = $gender_id; $productData['product_shipping_type'] = $product_type_id; try { $new_product_id = $api->create('simple', '4', $simple_sku, $productData); echo "SKU: " . $simple_sku . "has been Saved. <br />"; } catch (Exception $ex) { //Handle the error echo 'Caught exception: ', $ex->getMessage(), "\n"; } //print_r($new_product_id); $stockItem = Mage::getModel('cataloginventory/stock_item'); $stockItem->loadByProduct($new_product_id); $stockItem->setData('use_config_manage_stock', 1); $stockItem->setData('qty', trim($data[4])); $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);
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(); }