Showing posts with label attribute. Show all posts
Showing posts with label attribute. Show all posts

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, 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);

Friday, 13 December 2013

get custom options programatically Magento

Hi with the below code you can get the product custom  option attributes all data


$product = Mage::getModel("catalog/product")->load(1); //product id 1
$i = 1;
echo "<pre>";
foreach ($product->getOptions() as $o) {
    echo "<strong>Custom Option:" . $i . "</strong><br/>";
    echo "Custom Option TITLE: " . $o->getTitle() . "<br/>"; //Colors
    echo "Custom Option TYPE: " . $o->getType() . "<br/>"; //drop_down
    echo "Custom Option Values: <br/>";
    $values = $o->getValues();
    foreach ($values as $v) {
        print_r($v->getData());
    }
    $i++;
    echo "<br/>";
}

Monday, 4 February 2013

How to update atribute value of all products at the same time MAGENTO?




In magento there is a very good option in the admin to update all the attributes at the same time.
like if there are 100 products whose price has been changed and there are same price for all and you want to change them quickly and not by updating each single product , magento has a good option for you


In the admin go to 

 system -> manageproduct-> and there will be checkbox on the left in front of each product 

Select the multiple products you want to update and than on the right top there is a option of action ,click on the dropdown and select  update attribute and than press submit . You will see the atribute of products and than click on the CHANGEcheckbox of attribute which ever you want to update for selected products and than press save . The attribute for all the products will be updated 

You can also select all if you want to update any attribute which has same value for all products  .

Monday, 14 January 2013

Get custom attribute value in magento admin orders

with this code you can get any custom attribute value in magento admin order section , you can show only in admin or in frontend depends upon your need
   

 <div style="color: black; font-size: 14px; font-weight: bold;">proviser <span><?php $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_item->getSku(), array('provider')); echo $product->getprovider(); ?>  </span>  </div>

in above code just replace your attribute code which you specify in admin for that like for my case i use "provider "
just replace provider with your attribute code and place this in your 

/public_html/app/design/adminhtml/default/default/template/sales/items/column/name.phtml file