Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Amasty_Example_AmastyController extends Mage_Core_Controller_Front_Action
- {
- public function indexAction()
- {
- echo 'This is index controller. Please use specific actions below to create products.
- <br/>
- <a href="/example/amasty/createSimpleProduct">Create Simple Product</a>';
- }
- public function createSimpleProductAction()
- {
- $product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
- echo 'See <a href="/catalog/product/view/id/' . $product->getId() . '">created simple product</a>';
- }
- public function createSimpleProductAndRedirectAction()
- {
- $originalProductId = $this->getRequest()->getParam("id");
- // $productNameArray = explode(" - ",$this->getRequest()->getParam("id"));
- // $originalProductId = $productNameArray[0];
- $newImagePath = $this->getRequest()->getParam("image");
- $originalProduct = Mage::getModel('catalog/product')
- ->setStoreId(Mage::app()->getStore()->getId())
- ->load($originalProductId);
- if($product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE, true, $originalProduct,$newImagePath))
- {
- $this->getResponse()->setBody(Mage::helper('core')->jsonEncode("Your Image Saved1"));
- }
- }
- protected function _createProduct($type, $doSave=true, $originalProduct, $newImagePath="")
- {
- // required for some versions
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $product = Mage::getModel('catalog/product');
- // set madatory system attributes
- $rand = rand(1, 9999);
- $product
- ->setTypeId($type) // e.g. Mage_Catalog_Model_Product_Type::TYPE_SIMPLE
- ->setAttributeSetId(17) // default attribute set
- ->setSku('CDSB1_sku' . $rand) // generate some random SKU
- ->setWebsiteIDs(array(1))
- ;
- // make the product visible
- $product
- ->setCategoryIds(array(2,3))
- ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
- ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) // visible in catalog and search
- ;
- // configure stock
- $product->setStockData(array(
- 'use_config_manage_stock' => 1, // use global config ?
- 'manage_stock' => 0, // shoudl we manage stock or not?
- 'is_in_stock' => 1,
- 'qty' => 50,
- ));
- // optimize performance, tell Magento to not update indexes
- $product
- ->setIsMassupdate(true)
- ->setExcludeUrlRewrite(true)
- ;
- // finally set custom data
- if($_category instanceof Mage_Catalog_Model_Category && $_category->getId()):
- //$product->setName($originalProduct->getName());
- $product->setName($originalProduct->getName());
- endif;
- //$product->setDescription($originalProduct->getName());
- $product->setDescription('what ever you need here')
- ->setShortDescription('short desciption')
- // set up prices
- ->setPrice(599)
- //->setSpecialPrice(19.99)
- ->setTaxClassId(0) // Taxable Goods by default
- ->setWeight(1)
- ;
- $product->setDeliveryInfo('content goes here');
- $product->setInteriorExterior('content goes here');
- // add product images
- $images = array(
- 'thumbnail' => 'image.png', // displaying under cart page
- //'small_image' => 'image.png',
- 'image' => 'image.png', // displaying under my design
- );
- foreach ($images as $imageType => $imageFileName) {
- if($newImagePath != ""){
- $dir = Mage::getBaseDir('media') . DS . 'custom_product_preview/quote/';
- $path = $dir . $newImagePath;
- }else{
- $dir = Mage::getBaseDir('media') . DS . 'example/amasty/';
- $path = $dir . $imageFileName;
- }
- //echo $path."<br>";
- if (file_exists($path)) {
- try {
- $product->addImageToMediaGallery($path, $imageType, false);
- } catch (Exception $e) {
- echo $e->getMessage();
- }
- } else {
- echo "Can not find image by path: `{$path}`<br/>";
- }
- }
- $customerId = Mage::getSingleton('customer/session')->getCustomerId();
- $product->setCreatedByCustomerId($customerId);
- //$emaildate = Mage::getModel('core/date')->date('Y-m-d H:i:s');
- // $emaildate = date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));
- $emaildate = Mage::getSingleton('core/date')->gmtDate('Y-m-d H:i:s');
- $emailimage = Mage::helper('catalog/image')->init($product, 'thumbnail');
- if ($doSave)
- $product->save();
- // To save inside my_designs
- $data = array('title'=>fav1_id,'product_id'=>$product->getId(),
- 'customer_id'=> $customerId , 'store_id' => 1 , 'created_at' => $emaildate , 'value'=> $emailimage );
- $model = Mage::getModel('example/mydesign')->setData($data);
- try
- {
- $fav1_id = $model->save()->getId();
- //echo "Custom Design saved successfully";
- } catch (Exception $e)
- {
- echo $e->getMessage();
- }
- // To save inside my_designs end
- return $product;
- }
- protected function _getOptionIDByCode($attrCode, $optionLabel)
- {
- $attrModel = Mage::getModel('eav/entity_attribute');
- $attrID = $attrModel->getIdByCode('catalog_product', $attrCode);
- $attribute = $attrModel->load($attrID);
- $options = Mage::getModel('eav/entity_attribute_source_table')
- ->setAttribute($attribute)
- ->getAllOptions(false);
- foreach ($options as $option) {
- if ($option['label'] == $optionLabel) {
- return $option['value'];
- }
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment