Tuesday 25 October 2016

Magento2 code snippets

How to get customer data by id in Magento2 ?


$customerID = 10;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')
->load($customerID);
$customerEmail = $customerObj->getEmail();



-> How to get customer collection in Magento2 ?


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')->getCollection();
 foreach($customerObj as $customerObjdata ){
        print_r($customerObjdata ->getData());
 }




-> How to get order collection in Magento2 ?

 $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
     $orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
     foreach($orderDatamodel as $orderDatamodel1){
     print_r($orderDatamodel1->getData());

     }
    
    
    

 -> How to  get the order items from the order in Magento2 ?


$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
     $orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
     foreach($orderDatamodel as $orderDatamodel1){
     $getid =  $orderDatamodel1->getData("increment_id");
         $orderData = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($getid);
          //echo "<pre>";
          $getorderdata = $orderData->getData();
          $orderItems = $orderData->getAllVisibleItems();
          foreach($orderItems as $orderItems){
                    print_r($orderItems->getData());
        }
     }
    
    
    
    
    

- > How to get all storesin Magento2 ?


 function getallstores(){
$objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
    $connection = $objectManager->get('Magento\Framework\App\ResourceConnection')->getConnection('\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION');
    $result1 =
    $connection->fetchAll("SELECT store_id ,name FROM store");
    return $result1;
}


-> How to get attribute options by attribute code in Magento2

<?php
$objectManager1 =  \Magento\Framework\App\ObjectManager::getInstance();
$manufacturerOptions = $objectManager1->create('\Magento\Catalog\Model\Product\Attribute\Repository')->get('manufacturer')->getOptions();
foreach ($manufacturerOptions as $manufacturerOption) {
echo $manufacturerOption->getValue();
echo $manufacturerOption->getLabel();

?>
    

Monday 24 October 2016

How to get order collection in magento2 ?


With below piece of code we can easily get a order collection in magento2  easily by using object manager


> to get order collection 
 
<?php $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
     $orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
     foreach($orderDatamodel as $orderDatamodel1){
     print_r($orderDatamodel1->getData());
   
     }



- > to get order collection with order items data

     <?php $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
     $orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
     foreach($orderDatamodel as $orderDatamodel1){
     $getid =  $orderDatamodel1->getData("increment_id");
         $orderData = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($getid);
          //echo "<pre>";
          $getorderdata = $orderData->getData();
          $orderItems = $orderData->getAllVisibleItems();
          foreach($orderItems as $orderItems){
                    print_r($orderItems->getData());
        }
     }


Saturday 22 October 2016

How to get a product attribute options by code Magento2 ?

How to get a product attribute options by code Magento2


with the below code we can get the attribute options at any place of magento2

like templates , controllers etc


$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$manufacturerOptions = $objectManager->create('\Magento\Catalog\Model\Product\Attribute\Repository')->get('manufacturer')->getOptions();
foreach ($manufacturerOptions as $manufacturerOption) ;
echo $manufacturerOption->getValue();
echo $manufacturerOption->getLabel();















Friday 21 October 2016

How to make a view order link in magento2 ?

 
Hi with the below code we can easily amke a view order link in a 
magento2 
 
$this->getUrl('sales/order/view', ['order_id' => $orderId]); // $orderId is a 
entity id of a order not a increment id of a order
 

Wednesday 3 August 2016

How to get full product url from ID Magento ?

$store = Mage::app()->getStore();
$product_id = "1"// change your product id here 
 $path = Mage::getResourceModel('core/url_rewrite')
    ->getRequestPathByIdPath('product/'.$product_id, $store);

$url = $store->getBaseUrl($store::URL_TYPE_WEB) . $path;

Monday 25 July 2016

How to add new option to attribute programatically Magento ?

   

we can easily do this witht the below peace of code. It will add a new option to the existing attribute.

   $arg_attribute = 'your_attribute_code'; // enter your attribute code here
        $arg_value = 'new option value';
        $attr_model = Mage::getModel('catalog/resource_eav_attribute');
        $attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
        $attr_id = $attr->getAttributeId();
        $option['attribute_id'] = $attr_id;
        $option['value']['any_option_name'][0] = $arg_value;
        $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
        $setup->addAttributeOption($option);

Get Category and subcategory ids array in Magento

Hi with the below code you will get the array of parent child categories.



$rootcatId  = Mage::app()->getStore()->getRootCategoryId();
    $root_cat   = Mage::getModel('catalog/category')->load($rootcatId);
    $categories = $this->get_child_categories($root_cat);
    echo "<pre>";
    print_r($categories);
    echo "</pre>";


    function get_child_categories($parent) {
        $cat_model = Mage::getModel('catalog/category');
        $categories = $cat_model->load($parent->getId())->getChildrenCategories();
        $ret_arr = array();
        foreach ($categories as $cat)
        {
            $ret_arr[] = array(
                                'cat_id' => $cat->getId(),
                                'cat_name' => $cat->getName(),
                                'cat_url' => $cat->getUrl(),
                                'child_cats' => $this->get_child_categories($cat),
                                );
        }
        return $ret_arr;

    }

Wednesday 29 June 2016

How to create multiple custom options programmatically Magento ?


 To create a multiple custom options of a product in single time you can use  below  piece of code

<?php
require_once 'app/Mage.php';
Mage::app();
$productId = 905;   // your product id
$product = Mage::getModel('catalog/product')->load($productId);
                    $options = array(
                        array(
                        'title' => 'Color',
                        'type' => 'drop_down',
                        'is_required' => 1,
                        'sort_order' => 0,
                        'values' => array(
                                        array(
                                            'title' => 'Red',
                                            'price' => 10.50,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        ),
                                        array(
                                            'title' => 'Gold',
                                            'price' => 0,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        )
                                    )
                        ),
                        array('title' => 'size',
                        'type' => 'drop_down',
                        'is_required' => 1,
                        'sort_order' => 0,
                        'values' => array(
                                        array(
                                            'title' => 'small',
                                            'price' => 10.50,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        ),
                                        array(
                                            'title' => 'large',
                                            'price' => 0,
                                            'price_type' => 'fixed',
                                            'sku' => '',
                                            'sort_order' => 0,
                                        )
                                    )
                        )
            );
//print_r($options);die;
 foreach($options as $options1){
$product->getOptionInstance()->unsetOptions();
$optionsnew = array($options1);
$product->setProductOptions($optionsnew);
$product->setCanSaveCustomOptions(true);
$product->save();
}
?>

Wednesday 6 January 2016

Declaration of Zend_Pdf_FileParserDataSource_File::__construct() must be compatible with Zend_Pdf_FileParserDataSource::__construct()

Recently i was printing the pdf from magento admin and i got the following error


Declaration of Zend_Pdf_FileParserDataSource_File::__construct() must be compatible with Zend_Pdf_FileParserDataSource::__construct()

and on searching the issue i have found

This an incompatibility issue between PHP Version 5.4.4 and zend Framwork .

you can fix it by

change in this function lib/Zend/Pdf/FileParserDataSource.php.

change
abstract public function __construct();
to
abstract public function __construct($filePath);