Saturday 2 May 2015

Magento Add Products on FrontEnd

<?php
$customer_data=Mage::getSingleton('customer/session')->getCustomer();
$userid = $customer_data->getId();
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$cats = Mage::getModel('catalog/category')->load(2)->getChildren();
$catIds = explode(',',$cats);
if($_POST['submit']!=''){
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
try{
$product
//    ->setStoreId(1) //you can set data in store scope
    ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
    ->setAttributeSetId($_POST['attribute_set_id']) //ID of a attribute set named 'default'
    ->setTypeId($_POST['product_type']) //product type
    ->setCreatedAt(strtotime('now')) //product creation time
//    ->setUpdatedAt(strtotime('now')) //product update time

    ->setSku($_POST['sku']) //SKU
    ->setName($_POST['product_name']) //product name
    ->setWeight(4.0000)
    ->setStatus($_POST['status']) //product status (1 - enabled, 2 - disabled)
    ->setTaxClassId(0) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
    ->setManufacturer(28) //manufacturer id

    ->setPrice($_POST['price']) //price in form 11.22
    ->setCost($_POST['price']) //price in form 11.22
    ->setSpecialPrice($_POST['special_price']) //special price in form 11.22

    ->setDescription($_POST['description'])
    ->setShortDescription($_POST['short_description'])


    ->setCategoryIds(array($_POST['main_catid'], $_POST['sub_catid'])); //assign product to categories
    if (isset($_FILES['media_gallery']['name']) && $_FILES['media_gallery']['name'] != '') {
$target =Mage::getBaseDir().'/media/catalog/product/';
$targetb = $target.$_FILES['media_gallery']['name'];
move_uploaded_file($_FILES['media_gallery']['tmp_name'],$targetb);
}
$product->addImageToMediaGallery($targetb, array('image', 'small_image', 'thumbnail'), true, false);
$product->setData('userid',$_POST['userid']);
$product->save();
$productID = $product->getId();


/* $id = $customer->getId();
$sql = "insert into pharmacy_products (product_id,doctor_email,doctor_password,doctor_mobile,user_type_id) VALUES ('".$id."','".$doctor_email."','".$doctor_password."','".$doctor_mobile."','".$user_type_id."')";
$write->query($sql); */
$message = $this->__('Insert Data Sucessfully');
echo "<p class='data-success'>".$message."</p>";
}catch(Exception $e){
Mage::log($e->getMessage());
}
}
?>

No comments:

Post a Comment