Get the ordered product size and color in order items admin grid in Magento 1.9.2 - magento

I am showing a list of items ordered from all the orders from 'sales_flat_order_item table' in Magneto 1.9.2. I get the name, SKU, and all other values from the following collection.
protected function _prepareCollection() {
$collection = Mage::getModel('sales/order_item')->getCollection();
$collection->getSelect()->join(array('o' => 'sales_flat_order'), 'main_table.order_id = o.entity_id', array('increment_id','created_at'));
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
Now I want to get the size and color of the item ordered. I am new to Magento, so I have no clue where I can get these values from.

You can access them in sales_flat_order_item table collection.
$data = $collection->getData();
$optionsData = unserialize($data['product_options']);
echo "<pre>";print_r($optionsData);
// check the [attributes_info] for the custom options
The data in product_options is serialized, so if you unserialize it you end up with something like below :
Array
(
[info_buyRequest] => Array
(
[uenc] => aHR0cDovLzUwLjU2LjIxMy4xNTcvbWVuL3RlZXMta25pdHMtYW5kLXBvbG9zL2NoZWxzZWEtdGVlLTUxNC5odG1sP29wdGlvbnM9Y2FydA,,
[product] => 409
[related_product] =>
[super_attribute] => Array
(
[92] => 20
[180] => 80
)
[qty] => 6
[return_url] =>
)
[attributes_info] => Array
(
[0] => Array
(
[label] => Color
[value] => Black
)
[1] => Array
(
[label] => Size
[value] => S
)
)
[simple_name] => Chelsea Tee
[simple_sku] => mtk004
[product_calculations] => 1
[shipment_type] => 0
[giftcard_lifetime] =>
[giftcard_is_redeemable] => 0
[giftcard_email_template] =>
[giftcard_type] =>
)

Related

how to make multi dimension array from sql return rows in codeigniter

how to make multi dimension array from sql return rows in codeigniter.
return values from model holding all values in $res.
$res = $this->user_model->get_room_book_join(['rooms.hotel_id' => 1]);
if ($res) {
echo '<pre>';
print_r($res);
}
I getting this type of array.this type can not helping me.
Array( [0] => Array
(
[room_id] => 1
[room_no] => 101
[room_desc] => Double Bed Deluxe Non Air Conditioned
[status] => available
[category_id] => 1
[hotel_id] => 1
[tariff_type] => normal
[room_rate] => 1000
[persons] => 0
[date_start] => 0000-00-00
[date_end] => 0000-00-00
[overview_id] => 1
[rom_id] => 1
[hot_id] => 1
[cus_id] => 2
[bok_id] => 2
[dates] => 2017-04-12
)
[1] => Array
(
[room_id] => 2
[room_no] => 101
[room_desc] => Double Bed Deluxe Non Air Conditioned
[status] => available
[category_id] => 1
[hotel_id] => 1
[tariff_type] => normal
[room_rate] => 1000
[persons] => 0
[date_start] => 0000-00-00
[date_end] => 0000-00-00
[overview_id] => 1
[rom_id] => 1
[hot_id] => 1
[cus_id] => 2
[bok_id] => 2
[dates] => 2017-04-13
)
)
Actually I want this type of array.how can i make it.please help advance thanks.
array(
[0]=>array(
[room_id]=>1
[dates]=>array(
[0]=>2014-04-12
[1]=>2014-04-13
)
)
[1]=>array(
[room_id]=>2
[dates]=>array(
[0]=>2014-04-12
[1]=>2014-04-13
)
)
)
Hi, you can try this code, this may help you
$query = $this->db->get();
$result = $query->result_array();
$main_array=array();
$actual_array=array();
foreach($result as $res){
$main_array['room_id']=$res['room_id'];
$main_array['dates']=array($res['date_start'],$res['date_end']);
$actual_array[]=$main_array;
}
//echo ""; print_r($actual_array);
return $actual_array;
try this
// after if ($res) {
for ($i=0; $i < count($res); $i++) {
$res[$i] = array(
'room_id' => $res[$i]['room_id'],
'dates' => array($res[$i]['date_start'],$res[$i]['date_end'])
);
}

How can I sort an array by specific value containing class object inside it?

I want to sort this array by product price in ascending order. How can I do this?
Array
(
[0] => stdClass Object
(
[product_id] => 16
[product_name] => Ferrari 4802
[product_details] => I know this car is good. Because I have driven this car 10 year.
[product_quantity] => 1
[product_price] => 2560
[shipping_cost] => 1000
[product_image] => ./admin_assets/image/uploads/product_image/ea28444f93d1fea413c95861bf306fbf.jpg
[product_details_image] => ./admin_assets/image/uploads/product_details/ea28444f93d1fea413c95861bf306fbf.jpg
[thumbnails_image] => ./admin_assets/image/uploads/thumbnails/ea28444f93d1fea413c95861bf306fbf.jpg
[slidder_image] => ./admin_assets/image/uploads/slidders/ea28444f93d1fea413c95861bf306fbf.jpg
[status] => 1
[slidder] =>
[category_id] => 4
)
[1] => stdClass Object
(
[product_id] => 15
[product_name] => Laborghini urus
[product_details] => I know this car is good. Because I have driven this car 10 year.
[product_quantity] => 2
[product_price] => 500
[shipping_cost] => 1000
[product_image] => ./admin_assets/image/uploads/product_image/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[product_details_image] => ./admin_assets/image/uploads/product_details/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[thumbnails_image] => ./admin_assets/image/uploads/thumbnails/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[slidder_image] => ./admin_assets/image/uploads/slidders/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[status] => 1
[slidder] =>
[category_id] => 4
)
[2] => stdClass Object
(
[product_id] => 14
[product_name] => Laborghini hurican023
[product_details] => I know this car is good. Because I have driven this car 10 year.
[product_quantity] => 4
[product_price] => 50
[shipping_cost] => 1000
[product_image] => ./admin_assets/image/uploads/product_image/65e660ef6e3ebcb0773d87c048c93e1b.png
[product_details_image] => ./admin_assets/image/uploads/product_details/65e660ef6e3ebcb0773d87c048c93e1b.png
[thumbnails_image] => ./admin_assets/image/uploads/thumbnails/65e660ef6e3ebcb0773d87c048c93e1b.png
[slidder_image] => ./admin_assets/image/uploads/slidders/65e660ef6e3ebcb0773d87c048c93e1b.png
[status] => 1
[slidder] =>
[category_id] => 4
)
)
Because the array is generated by a database query you can easily have the query do the sorting for you.
You don't show your model code but assuming you are using Query Builder (a.k.a. Active Record) you need the following in your model before you execute $this->db->get().
$this->db->order_by('product_price', 'ASC');
Here is some example code showing a full query. It assumes the table is named 'products', you want to select all the table's fields, and you want only a particular 'category_id'.
In the Model file:
public function product_by_category($category)
{
$query = $this->db
->where('category_id', $category)
->order_by('product_price', 'ASC')
->get('products');
//return all records or NULL if no records are found
return $query->num_rows() > 0 ? $query->result() : NULL;
}

How to merge multiple arrays in PHP and output the values together?

I have a situation while developing an application on Laravel. I have three arrays in the following order. I need to merge them and display their values in the following order too. Here , it goes.
stone_name['ruby','diamond','catseye'];
stone_weight[112,223,445];
stone_rate[1000,2500,670];
I need to merge these three arrays and display the out put in this order.
stone_info = array(stone_name[0] , stone_weight[0] , stone_rate[0]);
So that the final result will be like :
stone_info = array("ruby",112,1000);
I was in the same situation and I had done it in this way. May be this can help you out.
<?php
$stone_name = ['ruby','diamond','catseye'];
$stone_weight = [112,223,445];
$stone_rate = [1000,2500,670];
$result = mergeArrays($stone_name, $stone_weight, $stone_rate);
function mergeArrays($stone_name, $stone_weight, $stone_rate) {
$result = array();
foreach ($stone_name as $key => $name ) {
$result[] = array( 'stone_name' => $name, 'stone_weight' => $stone_weight[$key], 'stone_rate' => $stone_rate[ $key ] );
}
return $result;
}
print_r($result);
Output:
Array
(
[0] => Array
(
[stone_name] => ruby
[stone_weight] => 112
[stone_rate] => 1000
)
[1] => Array
(
[stone_name] => diamond
[stone_weight] => 223
[stone_rate] => 2500
)
[2] => Array
(
[stone_name] => catseye
[stone_weight] => 445
[stone_rate] => 670
)
)
DEMO

magento category loop not showing important product information

I am trying to organize the category page by dividing the products into subcategories. When I loop through the subcategories (on catalog\product\list.phtml) with the code:
$child_cat = Mage::getModel('catalog/category')->load($child_id);
$_productCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($child_cat);
and then proceed to loop through the products...
foreach ($_productCollection as $_product):
It does not seem to differentiate between products that are set to be visible and products that are not (both are shown). It also does not show images, prices, or any other information. The only correct information I get is the product URL.
Why is this happening and how do I fix it?
By default, when you load a product collection you'll get a bare amount of information about the products. Ex:
Array
(
[entity_id] => 9
[entity_type_id] => 4
[attribute_set_id] => 4
[type_id] => simple
[sku] => DLKJFER343
[has_options] => 0
[required_options] => 0
[created_at] => 2012-12-07 16:04:58
[updated_at] => 2012-12-11 16:21:37
[cat_index_position] => 0
[stock_item (Varien_Object)] => Array
(
)
)
You need to explicitly tell Magento to load extra information or to filter by a value like this:
$_productCollection = Mage::getResourceModel( 'catalog/product_collection' );
// Filter by enabled products
$_productCollection->addAttributeToFilter( 'status', 1 );
// Load all product information
$_productCollection->addAttributeToSelect( '*' );
$_productCollection->addCategoryFilter( $category );
Now you'll see something like this (using $_product->debug() to dump out some information):
Array
(
[entity_id] => 3
[entity_type_id] => 4
[attribute_set_id] => 4
[type_id] => simple
[sku] => DLKJFER343
[has_options] => 0
[required_options] => 0
[created_at] => 2012-12-05 18:47:39
[updated_at] => 2012-12-11 16:20:25
[cat_index_position] => 0
[status] => 1
[visibility] => 4
[enable_googlecheckout] => 1
[tax_class_id] => 2
[is_recurring] => 0
[weight] => 3.0000
[price] => 534.2500
[name] => Sample Product
[url_key] => some-product
[is_returnable] => 2
[msrp_enabled] => 2
[msrp_display_actual_price_type] => 4
[image] => /w/r/something.png
[small_image] => /w/r/something_sm.png
[thumbnail] => /w/r/something_th.png
[options_container] => container2
[url_path] => some-product.html
[image_label] => One image label
[small_image_label] => Another image label
[thumbnail_label] => An image label
[description] => Long winded blah, blah, blah.
[short_description] => Blah, blah, blah.
[stock_item (Varien_Object)] => Array
(
)
)
The media gallery information (labels, etc) is a different beast and must be specifically requested through the getMediaGalleryImages() method of the Mage_Catalog_Model_Product object.
HOWEVER this method will return NULL if called while looping through a product collection. Oddly, you cannot access this data without explicitly loading the product model (for reasons I won't go into in this response). So, we need to try something like this:
$product = Mage::getModel( 'catalog/product' )->load( $_product->getId() );
$images = $product->getMediaGalleryImages();
There's a performance hit doing this in your collection loop though, so be careful.
EDIT:
Mage_Catalog_Block_Product->getLoadedProductCollection()
Long, long, long story short (this method digs deep)... as far as I can tell getLoadedProductCollection() will only show attributes which have Used In Product Listing set to Yes.
The reason resides in Mage_Catalog_Model_Resource_Config...
public function getAttributesUsedInListing()
{
// ... some code above omitted...
->where('main_table.entity_type_id = ?', (int)$this->getEntityTypeId())
// THIS RIGHT HERE IS WHY
->where('additional_table.used_in_product_listing = ?', 1);
return $adapter->fetchAll($select);
}

Magento - How to set media gallery images programmatically?

I try to set a new magento product image e.g. che_3.png with:
$visibility = array (
'thumbnail',
'small_image',
'image'
);
$product->addImageToMediaGallery( $file, $visibility, true, false);
The product image is moved from the
temporary directory: /media/tmp/catalog/product/upload/sessionid/original/che_3.png
to the
destination directory: /media/catalog/product/c/h/che_3.png
Now, if i want to call the image filepath - that is saved by magento - is wrong. The media_gallery object looks like this:
[media_gallery] => Array
(
[images] => Array
(
[0] => Array
(
[value_id] => 89
[file] => /c/h/che_3_2_1.png //che_3.svg is already in here
[label] =>
[position] => 1
[disabled] => 0
[label_default] =>
[position_default] => 1
[disabled_default] => 0
)
[1] => Array // why this second item?
(
[value_id] => 88
[file] => /c/h/che_3_2.png
[label] =>
[position] => 1
[disabled] => 0
[label_default] =>
[position_default] => 1
[disabled_default] => 0
)
)
[values] => Array
(
)
)
As you can see the filename is extended by magento and also two items are added to the media_gallery.
My question is how can I reset the product respectively the internal counter so magento is using the original file as it is?
Thanks in advance.
Problem solved: I've saved the product twice with $product->save() and it seems $product->addImageToMediaGallery() is also executed twice.

Resources