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());
}
}
?>

Wednesday 30 July 2014

Php Snippets


Php Program for getting current goldrate

<?php
$price = 0;
$tenK = 0;
$fourteenK = 0;
$twentyFourK = 0;
$gs = 0;
$goldSpot = 0;
$content = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json'); 
$metals = json_decode($content);
$price = $metals->inr->gold;
$tenK= round(((41.7/100)*$price),2);
$fourteenK= round(((58.5/100)*$price),2);
$eighteenK= round(((75/100)*$price),2);
$twentyK= round(((83.3/100)*$price),2);
$twentytwoK= round(((91.8/100)*$price),2);
$twentyFourK= round((1*$price),2);
?>
Output :
KaratPrice
24KRs.2507.18
22KRs.2301.59
20KRs.2088.48
18KRs.1880.38
14KRs.1466.7

How to display post by particular category with direct sql query?

SELECT *
FROM `wp_postmeta` AS wpmt
INNER JOIN wp_postmeta AS pmt
INNER JOIN wp_posts AS pst
WHERE pmt.meta_key = '_thumbnail_id'
AND wpmt.meta_key = '_wp_attached_file'
AND pmt.meta_value = wpmt.post_id
AND pst.post_type = 'post'
AND pmt.post_id = pst.ID
AND pmt.post_id
IN (

SELECT pst.ID
FROM wp_terms
INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr ON wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_posts p ON p.ID = wpr.object_id
WHERE taxonomy = 'category'
AND p.post_type = 'post'
AND wp_terms.name = 'ISO'
ORDER BY object_id
)

Here i have use category name iso hence i give wp_terms.name = 'ISO'

Usefull Urls for php coders

Convert php values to currency

International format (Germany) with 2 decimals:
<?php
$number = 1234.56;
setlocale(LC_MONETARY,"de_DE");
echo money_format("%.2n", $number);
?>
The output of the code above will be: 1 234,56 EUR

Difference between two days

<?php
$date1 = date('y-m-d h:i:s');//today date
$date2 = date('Y-m-d h:i:s', strtotime($tvalue->billdate.' + 2 day')); // after 2 days
$diff = strtotime($date2) - strtotime($date1);// subtract 2 days
$diff_in_hrs = $diff/3600;
$diff_in_sec = $diff_in_hrs*3600;
echo gmdate("H:i:s", $diff_in_sec);// remaining hours
?>

Email send for html format in php

<?php
$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: ' . $from_email;
//$headers .= "\r\nReply-To: " . $from_email;
//$headers .= "\r\nX-Mailer: PHP/" . phpversion();
$to = $emailid;
$subject= 'Customer Billing Details';
$message   = '<html><body><div>';
$message  .= '<h2>Our mail</h2>';
$message  .= 'A message to our Customers';
$message  .= '<a href="http://dev.probytes.net/uncer/index.php/?option=com_users&view=billing&rando='.$randno.'">Click to get your bill</a>';
$message  .= '</div></body></html>';
mail($to, $subject, $message, $headers);
?>