Magento custom shippment method - magento

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.

Related

Square API - Object Update for Multiple Locations

I'm trying to update objects using the endpoint BatchRetrieveCatalogObjects (POST /v2/catalog/batch-upsert).
When I post the request, I get the error
[type] => bad_request
[message] => Multi-location support is unavailable for this endpoint. Parameter `merchant_id` must correspond to a single-location account, not a business account
How can I update objects if there are multiple locations and if items have location overrides?
Is there another endpoint that I can use?
UpsertCatalogObject (POST /v2/catalog/object) does not work either. Same error.
Below is a sample request payload:
Array
(
[idempotency_key] => 1122334455667788
[batches] => Array
(
[type] => ITEM
[id] => 5BLFIPNYBTDD5ARZYQU4K7SV
[item_data] => Array
(
[name] => Honey Mead
[description] => Mead is an adult beverage brewed exclusively with honey.
[category_id] => OIMGQF7DCO7W6SEXEMKGIJK5
)
[variations] => Array
(
[type] => ITEM_VARIATION
[id] => BEQZ6FXAJACGW55RRZBMXDEI
[item_variation_data] => Array
(
[item_id] => 5BLFIPNYBTDD5ARZYQU4K7SV
[name] => Case (24 bottles)
[pricing_type] => FIXED_PRICING
[ordinal] => 4
[price_money] => Array
(
[amount] => 11900
)
[location_overrides] => Array
(
[0] => Array
(
[location_id] => 81J6E4K21KGC9
[price_money] => Array
(
[amount] => 11900
)
[pricing_type] => FIXED_PRICING
[track_inventory] => 1
[inventory_alert_type] => LOW_QUANTITY
[inventory_alert_threshold] => 15
)
)
)
)
)
)
Additional Notes for Clarification:
I'm trying to edit the price at either the variation or location_overrides level. I get the error for any call to the endpoint. I think the Square API developers don't allow any calls to the endpoint for any multi-unit locations. I also cannot delete locations either on the website or API. They only allow you to hide locations, which I did. But the error still persists.
I tried the v1 endpoint and get strange results. V1 & V2 use different variation_id. So if I pass a v2 variation_id to the v1 endpoint, it creates a new variation rather than updating it. The new variation then displays a new field called catalog_v1_id. Then when I try to delete the variation on the website, it disappears from the website view but still exists as an active item when I call the v2 list variation endpoint. I'm thinking this API has a whole host of bugs and is not ready for prime time.

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.

Yammer: get usernames with messages

I was wondering how I could get the usernames, etc. from messages using the API. Calling the GET messages API gives me all the info I need from the message, but I only get the userID. Because of API request limits, I can't afford to do another API request to know for each messages all the needed userinfo.
Am I missing something, or is this simply not possible?
In fact, check the "references" part of the response and you will find the whole details concerning your user.
I dont see where that is? This is my API response from yammer (for one message)
Array
(
[0] => stdClass Object
(
[id] => 544689850
[sender_id] => 1507082247
[replied_to_id] => 544673530
[created_at] => 2015/06/04 13:03:09 +0000
[network_id] => 355014
[message_type] => update
[sender_type] => user
[url] => https://www.yammer.com/api/v1/messages/544689850
[web_url] => https://www.yammer.com/b-rail.be/messages/544689850
[group_id] => 5741341
[body] => stdClass Object
(
[parsed] => Signed and Approved!
[plain] => Signed and Approved!
[rich] => Signed and Approved!
)
[thread_id] => 544673530
[client_type] => Web
[client_url] => https://www.yammer.com/
[system_message] =>
[direct_message] =>
[chat_client_sequence] =>
[language] => en
[notified_user_ids] => Array
(
)
[privacy] => private
[attachments] => Array
(
)
[liked_by] => stdClass Object
(
[count] => 0
[names] => Array
(
)
)
[content_excerpt] => Signed and Approved!
[group_created_id] => 5741341
)

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