Showing posts with label get. Show all posts
Showing posts with label get. Show all posts

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















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

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;

    }

Thursday, 8 October 2015

How to get category tree structure in Magento programatically ?

In many places we will need to get the category tree in magento programmatically. With the below peace of code you can get the tree anywhere in magento



   $rootCatId = Mage::app()->getStore()->getRootCategoryId(); // it will fetch your current store root id
   $catlistHtml = getTreeCategories($rootCatId, false);
   echo $catlistHtml;

function getTreeCategories($parentId, $isChild){
    $allCats = Mage::getModel('catalog/category')->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('is_active','1')  // is acategory active
                ->addAttributeToFilter('include_in_menu','1') //  to check include_in_menu yes or no
                ->addAttributeToFilter('parent_id',array('eq' => $parentId))  // filter parent id
                ->addAttributeToSort('position', 'asc');
              
    $class = ($isChild) ? "sub-cat-list" : "cat-list";
    $html .= '<ul class="'.$class.'">';
    foreach($allCats as $category)
    {
        $html .= '<li><span>'.$category->getName()."</span>";
        $subcats = $category->getChildren();
        if($subcats != ''){
            $html .= getTreeCategories($category->getId(), true);
        }
        $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
}


Please let me know if there is other such easy way to do this.I will love to hear and update my post.

thanks

Wednesday, 7 October 2015

How to get products of particular category in Magento ?

Hi it is very easy to get products of particular category in magento.
Check the below peace of code , with this you can get the products of your desored category


$products =
              Mage::getModel('catalog/category')->load('put your category id here')
            ->getProductCollection()
            ->addAttributeToSelect('*') ;
          

                foreach($products as $productsss){
                                  print_r($productsss->getData());
                    }


you can also add the filters in the above collection to get your desired products
like if you want to get only enabled products in the particular category like

$products = Mage::getModel('catalog/category')->load($catid)
            ->getProductCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('status', 1) ;


with this only products enabled will be show in list. You can add as many as filters you want.

Hope it will help somebody.

Thanks


Sunday, 31 May 2015

How to get customer session in external file Magento ?

Hi sometime we need to access the customer session in nexternal file and with the below code you can access


<?php
// Include Magento application
require_once ( "path_to_your_magento/app/Mage.php" );
umask(0);

// Initialize Magento
Mage::app();

// You have two options here,
// "frontend" for frontend session or "adminhtml" for admin session
Mage::getSingleton("core/session", array("name" => "frontend"));

$session = Mage::getSingleton("customer/session");

if($session->isLoggedIn())
{
    echo "Logged in";
}else{
    echo "Not logged in";
}

Wednesday, 13 May 2015

Get all statuses of particular state in Magento

Hi by below code you can get the all statuses of a particular state 

$complete_statuses = Mage::getResourceModel( 'sales/order_status_collection' )->joinStates()->addFieldToFilter('state','complete');
foreach ($complete_statuses as $complete_statuses_array ){
    $stausarray[] =  $complete_statuses_array->getStatus();
}


in this i have get the statuses of a complete state. You can replace  state
in ->addFieldToFilter('state','complete');  according to your needs


Friday, 8 May 2015

Get collection count in magento

HI you can get the colection count by below code


Mage::getModel('catalog/product')->getCollection()->getSize();  :)

Tuesday, 20 May 2014

Get current product data in custom file in magento

HI in some project i need to develop a module through which i need to insert a newly created product attribute  on product page with the modue xml file . For that i need to havethe current product data in my module file.

for that i have use the below function

$product_id = Mage::registry('current_product')->getId(); 
 
with this when your file will include in the porduct view page.it will load
 product id from your file.You can get any value from this.

Wednesday, 16 April 2014

Get all images tags from the remote url through php

In some project i need to get all image tags from the remote url
I have google a lot but not getting any particular solution.
But atlast i got a very good code which works like a charm

you can find that code on

https://docs.google.com/file/d/0B9cbYvYQ9b0YY2xxUW5kUms0NWM/edit

in this file you will see the below code at very last of file


// Create DOM from URL or file
$html = file_get_html('https://www.google.co.in/'); // replace your url

// Find all images
foreach($html->find('img') as $element) // to get all image src  from the url
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element) // to get all anchor tags  from the url
       echo $element->href . '<br>';


with this script you can get any paramaeter from the remote url.  Remember to copy the full file from the downlaod file from here






      

Thursday, 26 December 2013

Get products from particular category Magento

 <?php $categoryIds = array(3,4);//category id

                                    $collection = Mage::getModel('catalog/product')
                                    ->getCollection()
                                    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                                    ->addAttributeToSelect('*')
                                    ->addAttributeToFilter('category_id', array('in' => $categoryIds))
                    ?>
                   
                    <?php $_collectionSize = $collection->count() ?>
    <?php //$_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($collection as $product): ?>
        <?php if ($i++%4==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>">
                        <img src="<?php echo Mage::helper('catalog/image')->init($product, 'small_image')->resize(197, 167); ?>" alt="<?php echo $product->getName()?>" border="0" />
                    </a>
                <h2 class="product-name"><a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>"><?php echo $product->getName() ?></a></h2>
               <div class="price-box">
               <?php echo Mage::helper('core')->currency($product->getPrice(),true,false);?>
               </div>
                <div class="actions">
                    <?php if($product->isSaleable()): ?>
                        <button class="button" onclick="setLocation('<?php echo Mage::getUrl('checkout/cart/add/')?>product/<?php echo $product->getId() ?>/')" title="<?php echo $this->__('Köp');?>" type="submit"><span><span><?php echo $this->__('Köp');?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>

                </div>
            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php endforeach ?>

Sunday, 24 November 2013

Get product attribute on cart page magento

 
Hi to get the product atribute on cart page 
 
place the below code in 
 
in app/design/frontend/default/your_theme/template/checkout/cart/item/default.phtml 
 
<?php 
$_item = $this->getItem();
$_product = $_item->getProduct()->load();
?>
<?php echo $_product->getCustomAttribute(); ?>// place ths where ever you show 
and replace the CustomAttribute  with your attribute code
 
 
thanks