Category Archives: Magento

Magento get shipment id from order


Magento get shipment id from order : Shipments are created against the order in magento. You can create multiple shipments against a simple order. If you want to load shipment created against a order you can get it using order object to get shipments of that order. Here is an example of how to get shipment id from order object.


Magento get shipment id from order

Syntax to get shipment details such as shipment id from order is as –

  • Load order
  • Get Shipment Collection

Magento get shipment collection

Magento get shipment id from order

$orderIncrementId = 902;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$shipmentCollection = $order->getShipmentsCollection();
foreach($shipmentCollection as $shipment){
$shipmentIncrementId = $shipment->getIncrementId();
}

The above example will load all the shipments created against the order 902.

Magento get Tracking number from order


Magento get Tracking number from order : Tracking number in magento are used basically to track the orders. You can get tracking number from magento order_shipment_collection. Pass order object to get the shipment of the order in sales order_shipment_collection. Here we are going to explain how to get tracking number from magento order object.


Magento get Tracking number from order

Here is an example how to get tracking numbers from order object.

Magento get Tracking number from order

$order = Mage::getModel('sales/order')->loadByIncrementId($orderid);
$shipmentCollection = Mage::getResourceModel('sales/order_shipment_collection')
            ->setOrderFilter($order)
        ->load();
        foreach ($shipmentCollection as $shipment){
            foreach($shipment->getAllTracks() as $tracknumber)
            {
                $trackingnumbers[]=$tracknumber->getNumber();
            }

        }

The above example will give traking numbers generated in magento.

Note : This will give shipment increment id not actual tracking number.

Magento create product programmatically


Magento create product programmatically : It’s very common to create products in magento from admin panel but sometimes we need to create products programatically for some specific requirement. You can create products programatically and assign values dynamically as per your requirement. Here we are going to learn how to create simple product in magento programatically.


Magento create product programmatically

You can create products programmatically as below-

Magento create product programmatically:

public function createProduct(){
try{
    // first set current store
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    // load catalog produuct model
    $product = Mage::getModel('catalog/product');
    $product->setAttributeSetId(9)
               ->setTypeId('simple')
               ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
               ->setTaxClassId(2)
               ->setCreatedAt(strtotime('now'))
               ->setName("Test Product")
               ->setSku("test-sku")
               ->setWeight(50)
               ->setStatus(1)
               ->setPrice(90)
               ->setCategoryIds(array(1,4,8))
               ->setWebsiteIds(array(1,6))
               ->setDescription("Test Description")
               ->setShortDescription("Test Short Description")
               ->setFreeGroundShipping(50)
               ->setMetaTitle("Test")
               ->setMetaKeyword("Test")
               ->setMetaDescription("Test")
               ->setStockData(array(
                                     'manage_stock'=>0,
                                     'min_sale_qty'=>1,
                                     'max_sale_qty'=>3,
                                     'is_in_stock'=>1))
               ->setSetupFee(20)
               ->setsetupCost(10);
    $product->save();                
}catch(Exception $e){
Mage::log("Exception ---". $e->getMessage());
}

}

The above function will create simple product in magento which is decided by type id. You can add categories, price, name sku and quantity etc while creating product

Magento create category programmatically


Magento create category programmatically : You can create categories programatically in magento. Mage::getModel(‘catalog/category’); is used to initialize and create the category in magento. You can set category url, name, parent category and path etc in the category. Here is simple example of creating category programatically.


Magento create category programmatically

Here is syntax to create category programmatically in magento-

Here is syntax to create category programmatically in magento

 public function createCategory(){
    $parentId = 12;// id of parent category 
    $category = Mage::getModel('catalog/category');
    $category->setName('My Category');
    $category->setUrlKey('my-category');
    $category->setIsActive(1); // to make active
    $category->setDisplayMode('PRODUCTS');
    $category->setIsAnchor(1); // This is for active anchor
    $category->setStoreId(Mage::app()->getStore()->getId());
    $parentCategory = Mage::getModel('catalog/category')->load($parentId);
    $category->setPath($parentCategory->getPath());
    $category->save();

}

Which will create the category under the parent category with id 12.

Magento Set Path of category | Parent Category

You can set path of a category in magento as below –

Magento Set Path of category | Parent Category Example

$parentId = 89;  // parent category id 
$category = Mage::getModel('catalog/category');
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());

Magento Get Configurable Products collection


Magento Get Configurable Products collection : Configurable Products are collection of simple products. it can contain many products associated with it known as simple product or child product. Here we are going to explain the method to get configurable products from magento collection.


Magento Get Configurable Products collection

Here is syntax for Magento Configurable Products collection –

Magento Get Configurable Products collection



$productCollection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id','configurable');

foreach ($productCollection as $product) {
  $name = $product->getName();
  $id = $product->getId();
}

The above collection will return the configurable products. You can get the detail about the product.

Magento assign simple product to configurable programmatically


Magento assign simple product to configurable programmatically : You can assign simple products to configurable product programatically. First get the configurable product object and then get product ids. Now assign these ids to the configurable product with the help of the catalog/product_type_configurable resource. Here we are going to explain the example of assign simple products to the configurable products programmatically.


Magento assign simple product to configurable programmatically

Here is syntax to assign products to the configurable products-

Magento assign simple product to configurable programmatically


$productId = 23 // id of configurable product
$productCollection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id','configurable')
             ->load($productId); 

foreach ($productCollection as $product) {
  $simpleProductIds = array('1','9','5'); // Simple product Ids
  Mage::getResourceSingleton('catalog/product_type_configurable')
    ->saveProducts($product, $simpleProductIds);
}

First get the product collection of configurable product and then get object of the product $product now assign simple products to the configurable by simple products id ie. $simpleProductIds = array(‘1′,’9′,’5’); which is array of simple product ids.

Magento get simple product from configurable


Magento get simple product from configurable : As we know configurable products are collection of simple products. If we are working with magento we often need to get simple products from configurable products. You can get all child products of a configurable products using the configurable product’s id. Here we are going to explain the method to get all child items from configurable product.


Syntax : Magento get simple product from configurable

Here is the syntax for getting simple products from configurable products –

Magento get simple product from configurable

/*
 * First Load product by product id
 */
$product = Mage::getModel('catalog/product')->load($productId);

/*
 * Now Get All child products ids
 */
$childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());

/*
 * Now Get children products ie all  associated children products data
 */
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);

Pass the product id of the configurable product and get all child items data as above.

Magento Exclude Out Of Stock From Front Page


Magento Exclude Out Of Stock From Front Page: In you are working with magento you often need to exclude the out of stock products from front page. Many times we want to show the in stock items. It depends upon our need. Here we are going to explain the syntax and method to exclude the items from the front page that are out of stock.


Magento Exclude Out Of Stock From Front Page

Here is syntax to exclude out of stock products and show only in stock items.

Magento Exclude Out Of Stock From Front Page



getProductCollection()) && $products->getSize()){
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);
}
?>

The above method will filter the in stock items and exclude the out of stock items.

Magento Get product Attribute Value


Magento Get product Attribute Value : If you are working with magento you often need to work with products and their attributes. Magento provides flexibility to add product attributes dynamically. Here we are going to explain how to get product attribute value. Syntax and example is given below –


Magento Get product Attribute Value Syntax

Here is syntax to get product value syntax-

Magento Get product Attribute Value

$productId = 55;
$storeId = 1;
$product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId);
$attrText = $product->getAttributeText('your_attribute');

If you want to use option id Here is another example of the set attribute and get value.

Magento Set product Attribute Value And Get Attribute Value

Here is syntax to set and get attribute value in magento product-

Magento Set product Attribute Value And Get Attribute Value

$attributeOptionId = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'your_attribute', $storeId);

$product = Mage::getModel('catalog/product')
    ->setStoreId($storeId)
    ->setData('your_attribute', $attributeOptionId);
$attrText = $product->getAttributeText('your_attribute');