Magento - How to set media gallery images programmatically? - image

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.

Related

Google Drive API get files in specific folder in Laravel

i am using the following code to get files from folder id. But I am not getting the files id instead empty array returning.
putenv('GOOGLE_APPLICATION_CREDENTIALS=C:\Users\new\Downloads\zonefunnel1.json');
$client = new Google_Client();
$client->addScope(Google_Service_Drive::DRIVE);
$client->useApplicationDefaultCredentials();
$service = new Google_Service_Drive($client);
$res=$service->files->listFiles();
//print_r($res);
$folderId="0B_bZZilaXPR4WkNvdFpMZmdneEU";
$optParams = array(
'pageSize' => 10,
'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)",
'q' => "'".$folderId."' in parents"
);
$results = $service->files->listFiles($optParams);
print_r($results);die();
I am getting the following,
Google_Service_Drive_FileList Object ( [collection_key:protected] => files [filesType:protected] => Google_Service_Drive_DriveFile [filesDataType:protected] => array [incompleteSearch] => [kind] => [nextPageToken] => [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) [files] => Array ( ) )
Please help me I have spent one week time for this.But not able to sort it out.
Regards,
Rekha
$files_list = $service->files->listFiles(array())->getFiles($optParams);

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

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] =>
)

Magento API 'Exception' with message 'Item (Mage_Sales_Model_Order) with the same id "X" already exist'

I'm trying to get a list of orders using the Magento REST API.
The REST request we use is pretty basic: http://www.example.com/api/rest/orders
The response shows the next error:
{
"messages": {
"error": [
{
"code": 0,
"message": "Item (Mage_Sales_Model_Order) with the same id \"54\" already exist"
}
]
}
}
Checking my exception log to see what's going on and got the next backtrace of the error:
2015-09-10T21:54:59+00:00 ERR (3):
exception 'Exception' with message 'Item (Mage_Sales_Model_Order) with the same id "54" already exist' in /path/to/site/lib/Varien/Data/Collection.php:373
Stack trace:
#0 /path/to/site/lib/Varien/Data/Collection/Db.php(576): Varien_Data_Collection->addItem(Object(Mage_Sales_Model_Order))
#1 /path/to/site/lib/Varien/Data/Collection.php(301): Varien_Data_Collection_Db->load()
#2 /path/to/site/app/code/core/Mage/Sales/Model/Api2/Order.php(302): Varien_Data_Collection->getItems()
#3 /path/to/site/app/code/core/Mage/Api2/Model/Resource.php(245): Mage_Sales_Model_Api2_Order->_retrieveCollection()
#4 /path/to/site/app/code/core/Mage/Api2/Model/Dispatcher.php(74): Mage_Api2_Model_Resource->dispatch()
#5 /path/to/site/app/code/core/Mage/Api2/Model/Server.php(239): Mage_Api2_Model_Dispatcher->dispatch(Object(Mage_Api2_Model_Request), Object(Mage_Api2_Model_Response))
#6 /path/to/site/app/code/core/Mage/Api2/Model/Server.php(107): Mage_Api2_Model_Server->_dispatch(Object(Mage_Api2_Model_Request), Object(Mage_Api2_Model_Response), Object(Mage_Api2_Model_Auth_User_Admin))
#7 /path/to/site/api.php(67): Mage_Api2_Model_Server->run()
#8 {main}
I modified the file app/code/core/Mage/Sales/Model/Api2/Order.php (function _retrieveCollection) and added a line to print some info on the logs:
Mage::log($collection->getSelect(),null,'mylog.log');
That's part of the output:
[_parts:protected] => Array
(
[straightjoin] =>
[distinct] =>
[columns] => Array
(
[0] => Array
(
[0] => main_table
[1] => *
[2] =>
)
[1] => Array
(
[0] => payment_method
[1] => method
[2] => payment_method
)
[2] => Array
(
[0] => gift_message
[1] => sender
[2] => gift_message_from
)
[3] => Array
(
[0] => gift_message
[1] => recipient
[2] => gift_message_to
)
[4] => Array
(
[0] => gift_message
[1] => message
[2] => gift_message_body
)
[5] => Array
(
[0] => order_tax
[1] => title
[2] => tax_name
)
[6] => Array
(
[0] => order_tax
[1] => percent
[2] => tax_rate
)
)
[union] => Array
(
)
[from] => Array
(
[main_table] => Array
(
[joinType] => from
[schema] =>
[tableName] => sales_flat_order
[joinCondition] =>
)
[payment_method] => Array
(
[joinType] => left join
[schema] =>
[tableName] => sales_flat_order_payment
[joinCondition] => main_table.entity_id = payment_method.parent_id
)
[gift_message] => Array
(
[joinType] => left join
[schema] =>
[tableName] => gift_message
[joinCondition] => main_table.gift_message_id = gift_message.gift_message_id
)
[order_tax] => Array
(
[joinType] => left join
[schema] =>
[tableName] => sales_order_tax
[joinCondition] => main_table.entity_id = order_tax.order_id
)
)
[where] => Array
(
)
[group] => Array
(
)
[having] => Array
(
)
[order] => Array
(
)
[limitcount] =>
[limitoffset] =>
[forupdate] =>
)
[_tableCols:protected] => Array
(
)
)
If I understood correctly that means that the SQL statement was something like:
SELECT
main_table.*,
payment_method.method AS method,
gift_message.sender AS gift_message_from,
gift_message.recipient AS gift_message_to,
gift_message.message AS gift_message_body,
order_tax.title AS tax_name,
order_tax.percent AS tax_rate
FROM
sales_flat_order AS main_table LEFT JOIN
sales_flat_order_payment AS payment_method ON main_table.entity_id = payment_method.parent_id LEFT JOIN
gift_message ON main_table.gift_message_id = gift_message.gift_message_id LEFT JOIN
sales_order_tax AS order_tax ON main_table.entity_id = order_tax.order_id
After manually running the previous query, it came up with more than one row with the same entity_id (sales_flat_order). These 'duplicated' entity_id rows seem to be the problem later on when using Varien_Data_Collection->addItem
The part of the query that is making the two rows with same entity_id to be on the resultset is the LEFT JOIN sales_order_tax. That table contains can contain N rows per each order placed, since every row contains a different tax rule applied.
For example in Canada we collect two different Tax Rules combined for
some areas. In British Columbia we collect GST 5% (country specific)
plus PST 7% (province specific).
Am I missing something obvious here, or did I run into a bug?
Any help is much appreciated, thanks for reading!
P.S. My issue is very close to the one described in here: Magento API V2 Sales Orders List Not Working
After a while playing around I think I found a solution, so I'm posting it here for future references.
Solution
We need to modify the query of the collection, to group by sales_flat_order.entity_id before it starts iterating the items in Mage_Sales_Model_Api2_Order::_retrieveCollection.
Since modifying core files it's not a good practice we can make a copy of the core class to the local code pool and modify it as desired. Magento will use the class under the local folder to override the core class.
Copy app/code/core/Mage/Sales/Model/Api2/Order.php to app/code/local/Mage/Sales/Model/Api2/Order.php
Modify the file under the local code pool folder (app/code/local/Mage/Sales/Model/Api2)
Look for the function _retrieveCollection (around line 288 in Magento 1.9.2)
After the line $this->_addTaxInfo($collection); you should add:
$collection->getSelect()->group('main_table.entity_id');
Cons
If we are updating Magento we want to compare our current version of the Mage_Sales_Model_Api2_Order class with the new version, and if we find differences we shall repeat the process of making a copy of the core file under the local code pool folder and perform the edition again.

Magento custom shippment method

I wrote shippment module with checking a lot of resources/tutorials etc. I am using magento 1.9. Source of module is:
https://github.com/aleextra/magentoshipping
I lost full day to find why it doesn't works fine. Magento see my module, when I am adding at Model/Carrier.php at line 52 code:
die($result);
it shows objec dump:
Mage_Shipping_Model_Rate_Result Object
(
[_rates:protected] => Array
(
[0] => Mage_Shipping_Model_Rate_Result_Method Object
(
[_data:protected] => Array
(
[carrier] => mikshipping
[carrier_title] => Mik Carrier
[method] => fixed
[method_title] => Fixed price 10
[price] => 10
[cost] => 10.00
)
[_hasDataChanges:protected] => 1
[_origData:protected] =>
[_idFieldName:protected] =>
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
)
[_error:protected] =>
)
What I am doing wrong?
As a quick guess I say that your carriers name in the config.xml (mshipping) does not match the code used in Carrier.php (mikshipping). I have them always being the same so Iḿ not absolutely sure if this might cause the issue, but it is easy to check.

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

Resources