Magento API version incompatibility - magento

I am creating catalog products using Magento's core API. It works fine on Magento version 1.4.1.1, but the same code doesn't work on Magento ver 1.5.0.1.
Here's my code:
require 'rubygems'
require 'soap/wsdlDriver'
WSDL_URL = 'http://example.code/api/v2_soap/?wsdl=1'
soap = SOAP::WSDLDriverFactory.new(WSDL_URL).create_rpc_driver
session = soap.login('theuser','theuser123')
data = { "name" => "BARRACUDA", "description" => "fill", "short_description" => "fill", "weight" => "12", "status" => 1, "visibility" => 4, "price" => 350.00 , "tax_class_id" => "2", "qty" => 10, "stock_availability" => "0", "category_ids" => [3] }
a1 = soap.call('catalogProductCreate',session,"simple",1,"black: ONT-920-B",data)
Is there any problem with my code, or any new things added to Magento version 1.5.0.1?
Thanks

The problem is missing one attribute in date field
data = { "name" => "BARRACUDA", "description" => "fill", "short_description" => "fill", "weight" => "12", "status" => 1, "visibility" => 4, "price" => 350.00 , "tax_class_id" => "2", "qty" => 10, "stock_availability" => "0", "category_ids" => [3], "websites" => [1] }
Need to mention the websites id in the array of data in magento 1.5.
This is works for me!

Related

How to add custom shipment CS-Cart

I need to add dynamic shipment option to the checkout page, the shipment's pricing, duration and name come from other API.
I tried these hook, none of these invoked during checkout.
'get_available_shippings',
'get_shipping_methods',
'get_shipping_methods_post',
'get_shipments_info_post'
tried to hook shipping_methods_list.pre.tpl
{assign var="s" value=$all_shippings[0][1]}
{$s["shipping_id"] = '2'}
{$all_shippings[0][2] = $s}
I'm using cs-cart 4.14
The right hook was shippings_get_shippings_list_post, since it's not adding to the database, it seems you need to hook orders:shipping_info on the backend for further customization.
function fn_example_addons_shippings_get_shippings_list_post(&$group, &$lang, &$area, &$shippings_info)
{
$package_info = $group['package_info'];
$fromZipcode = $package_info['origination']['zipcode'];
$toZipcode = $package_info['location']['zipcode'];
$weight = (int)round(ceil($package_info["W"]));
$item_price = (int)round(ceil($package_info["C"]));
$shipments = fn_example_addons_getRates($fromZipcode, $toZipcode, $weight, $item_price);
foreach ($shipments as $key => $shipment) {
$shippings_info[$key] = $shipment;
}
}
for the shipments
$shipments["SHIPMENT_ID"] = array(
"shipping_id" => "SHIPMENT_ID",
"shipping" => "Example Shipment"
"delivery_time" => "2-3 days",
"description" => "",
"rate_calculation" => "M",
"service_params" => array(),
"destination" => "I",
"min_weight" => "0.0",
"max_weight" => "0.0",
"service_id" => "0",
"free_shipping" => "N",
"module" => null,
"service_code" => null,
"is_address_required" => "Y",
"rate_info" => array(
"rate_id" => "0",
"shipping_id" => "0",
"rate_value" => array(
"C" => array(
array(
"range_from_value" => 0,
"range_to_value" => "",
"value" => $price_in_point
)
)
),
"destination_id" => "1",
"base_rate" => "0.0"
),
"disable_payments_ids" => array()
);

Magento 2 rest api doesn't update salable quantity

I use rest api to create a simple product on my magento. All works correctly except quantity column that is correctly update on "Quantity" column but not on the salable quantity.
Rest call: "www.mysite.com/V1/products"
Array data
$data = [
"product" => [
"sku" => $sku,
"name" => $product_title,
"attribute_set_id" => 4,
"price" => $price,
"status" => 1,
"visibility" => 4,
"type_id" => "simple",
"weight" => "1",
"extension_attributes" => [
"category_links" => [
[
"position" => 0,
"category_id" => "53"
]
],
"stock_item" => [
"qty" => $qty,
"is_in_stock" => true
]
],
"custom_attributes" => [
[
"attribute_code" => "special_price",
"value" => $salable_price
],
[
"attribute_code" => "special_from_date",
"value" => "2021-02-07 00:00:00"
],
[
"attribute_code" => "special_to_date",
"value" => "2091-02-07 00:00:00"
],
[
"attribute_code" => "cost",
"value" => $salable_price
],
[
"attribute_code" => "description",
"value" => $description
],
[
"attribute_code" => "short_description",
"value" => $short_description
]
]
]
];
As you see, qty has been correctly update on qty column but not on the salable quantity. What is my mistake?
Please try this:
"stockItem": {
"is_in_stock": 1,
"qty": 10,
"manage_stock": true,
"use_config_manage_stock": 1
}

Display contents of a hash if value exists

I have a hash:
req = {
"count" => 50100,
"results" => [
{"listing_id" => 615929315, "state" => "active", "user_id" => 140604756, "category_id" => 69150367},
{"listing_id" => 615929311, "state" => "active", "user_id" => 152528025, "category_id" => 69150367}
]
}
I want to find and display the entire internal hash if a particular user_id exists. I can find it:
req["results"][0].select{|key, value| value == 152528025}
# => {"user_id" => 152528025}
How do I then display this entire (nested) hash?
{"listing_id" => 615929311, "state" => "active", "user_id" => 152528025, "category_id" => 69150367}
req["results"].select{|x| x["user_id"] == 152528025}

ruby one-liner from two hashes

a = {"rows" => [{"id" => "231-z", "name" => 'jon', "age"=> 27, "state" => 'AL'},
{"id" => "4121-x", "name" => 'ton', "age"=> 37, "state" => 'VA'}
]
}
b = {"rows" => [{"key" => ["xyz","4121-x"], "value" =>{"sum" => 12312, "realage" => 29}},
{"key" => ["xyz","231-z"], "value" =>{"sum" => 1212, "realage" => 33}}
]
}
In hash a, age is incorrect
In hash b, realage is correct. Also in hash b id is the second value in the first array that maps to id of hash a . Those are 4121-x, 231-z correspond to hash a
I want to correct the age in hash a and swap it with the realage of hash b
I can do it in multiple steps, but is it possible to do it in one liner or very short? So finally correct hash a should look like
a = {"rows" => [{"id" => "231-z", "name" => 'jon', "age"=> 33, "state" => 'AL'},
{"id" => "4121-x", "name" => 'ton', "age"=> 29, "state" => 'VA'}
]
}
does this look reasonable?
a['rows'].each_with_index do |ah, i|
(bh = b['rows'].select {|h| h['key'].last == ah['id'] }.first) &&
a['rows'][i] = ah.update('age' => bh['value']['realage'])
end
p a
{
"rows" => [
[0] {
"id" => "231-z",
"name" => "jon",
"age" => 33,
"state" => "AL"
},
[1] {
"id" => "4121-x",
"name" => "ton",
"age" => 29,
"state" => "VA"
}
]
}
Please note it will update a only if corresponding id found in b.
Also, the rows order does not matter, nor matter the rows number, it is only important b to have a row with same id as processed row in a
Here is a Working Demo

Group by and format array of hash values in ruby

Hey I have an array of hash values as follows.
[{"group" => "1", "message" => "hey", "weight" => 1}, {"group" => "1", "message"
=> "hey1", "weight" => 2}, {"group" => "2", "message" => "hey3", "weight" => 4}]
I want to group_by group and format it so that I get the following:
[{"group" => 1, "messages" => {"hey","hey1"}, "weights" => {1,2}}, {"group" => 2,
"messages" => {"hey3"}, "weights" => {4}}]
Is there a nice ruby way to achieve this?
Edit: Now I have:
[
{"group" => "1", "message" => {"hey" => "1"}},
{"group" => "1", "message" => {"hey1" => "2"}}
]
I'd like to have
{"group" => "1", "messages" => {"hey1" => "1", "hey2" => "2"} }
Based on your revised question:
groups = [
{"group" => "1", "message" => {"hey" => "1"}},
{"group" => "1", "message" => {"hey1" => "2"}}
]
merged = groups.inject do |h1,h2|
h1.merge(h2) do |k,v1,v2|
if v1==v2
v1
elsif v1.is_a?(Hash) && v2.is_a?(Hash)
v1.merge(v2)
else
[*v1,*v2]
end
end
end
p merged
#=> {"group"=>"1", "message"=>{"hey"=>"1", "hey1"=>"2"}}
I think the output you want is:
[{"messages"=>["hey", "hey1"], "weights"=>[1, 2], "group"=>"1"}, {"messages"=>["hey3"], "weights"=>[4], "group"=>"2"}]
If this is the case, this code does what you want:
h.group_by { |item| item["group"] }.values.map do |item|
item.inject({"messages" => [], "weights" => []}) do |result, subitem|
result["group"] = subitem["group"]
result["messages"] << subitem["message"]
result["weights"] << subitem["weight"]
result
end
end
You should be able to improve it knowing more about your specific problem, but it should be a good starting point.

Resources