Magento 2 rest order API show duplicate sku in order item if product is configurable - magento2

I am getting Magento 2 order details by rest order API but the result is showing the same SKU in configured product order item that is creating an issue in SAP integration.
Anyone let me know how can we overwrite Magento 2 rest API code?

You should not need to “overwrite magento 2 rest API code” because what you described is perfectly normal and expected.
In Magento orders, each configurable product creates two sales order line items due to the way that configurable product data is structured in the catalog. One line corresponds to the “parent” catalog product, and one corresponds to the specific variant “child” (simple) product. Both records contain the same SKU value but their product IDs are different.
In order to get all information about the ordered item both of those records could be important, however if you can get by with just the data from the simple product then you could filter your REST request like below:
GET <host>/rest/V1/orders/items?
searchCriteria[filter_groups][0][filters][0][field]=order_id&
searchCriteria[filter_groups][0][filters][0][value]=1&
searchCriteria[filter_groups][1][filters][0][field]=product_type&
searchCriteria[filter_groups][1][filters][0][value]=simple

Related

How to avoid customer's order history being changed in MongoDB?

I have two collections
Customers
Products
I have a field called "orders" in each of my customer document and what this "orders" field does is that it stores a reference to the product Id which was ordered by a customer, now my question is since I'm referencing product Id and if I update the "title" of that product then it will also update in the customer's order history since I can't embed each order information since a customer may order thousands of products and it can hit 16mb mark in no time so what's the fix for this. Thanks.
Create an Orders Collection
Store ID of the user who made the order
Store ID of the product bought
I understand you are looking up the value of the product from the customer entity. You will always get the latest price if you are not storing the order/price historical transactions. Because your data model is designed this way to retrieve the latest price information.
My suggestion.
Orders place with product and price always need to be stored in history entity or like order lines and not allow any process to change it so that when you look up products that customers brought you can always get the historical price and price change of the product should not affect the previous order. Two options.
Store the order history in the current collection customers (or top say 50 order lines if you don't need all of history(write additional logic to handle this)
if "option1" is not feasible due to large no. of orders think of creating an order lines transaction table and refer order line for the product brought via DBref or lookup command.
Note: it would have helped if you have given no. of transactions in each collection currently and its expected rate of growth of documents in the collection QoQ.
You have orders and products. Orders are referencing products. Your problem is that the products get updated and now your orders reference the new product. The easiest way to combat this issue is to store full data in each order. Store all the key product-related information.
The advantage is that this kind of solution is extremely easy to visualize and implement. The disadvantage is that you have a lot of repetitive data since most of your products probably don't get updated.
If you store a product update history based on timestamps, then you could solve your problem. Products are identified now by 3 fields. The product ID, active start date and active end date. Or you could configure products in this way: product ID = product ID + "Version X" and store this version against each order.
If you use dates, then you will query for the product and find the product version that was active during the time period that the order occurred. If you use versions against the product, then you will simply query the database for the particular version of the product itself. I haven't used mongoDb so I'm not sure how you would achieve this in mongoDb exactly. Naively however, you can modify the product ID to include the version as well using # as a delimiter possibly.
The advantage of this solution is that you don't store too much of extra data. Considering that products won't be updated too often, I feel like this is the ideal solution to your problem

Get specific product with id via Rest API Magento 2

I want to get the information from an specific product.
I am using this reference in this moment, and get all the products in given searchCriteria:
http://www.mysite.co/rest/V1/products-render-info?storeId=1&currencyCode=cop
Is there a way I can send the product id in the url and get only all its information?
You can add the filter
http://www.mysite.co/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][value]=1&searchCriteria[filterGroups][0][filters][0][condition_type]=eq
use field entity_id instead of id, value is product id here 1, condition_type is equal to here eq.
You can fetch product info by SKU, not ID.
API endpoint (get method) will pull product info. vendor/magento/module-catalog/etc/webapi.xml
/V1/products/:sku
So, your rest API calling URL should be like this
http://www.mysite.co/rest/V1/products/productsku
Note: If you want to fetch product info by id, you probably need to create a simple rest API. you can check this out.
https://magento.stackexchange.com/questions/282480/i-am-new-here-i-want-to-know-how-can-i-create-my-own-simple-api/282730

How correctly make REST API request to get complete orders between a date range in Magento 2

I want to get complete orders between a date range using Magento 2 REST Api. So request looks like:
/rest/V1/orders?searchCriteria[filterGroups][0][filters][0][field]=status&searchCriteria[filterGroups][0][filters][0][value]=Complete&searchCriteria[filterGroups][0][filters][0][conditionType]=eq
Now I want get it in specific period. I found that Magento api has "from" and "to" fields but I always confused in searchCriteria filter index. Can anybody complete my request? Thanks
You will get complete orders between a date range by using the conditions from and to.
This API will get you the orders between the two dates:
http://<magento_host>/rest/V1/orders?
searchCriteria[filter_groups][0][filters][0][field]=created_at&
searchCriteria[filter_groups][0][filters][0][condition_type]=from&
searchCriteria[filter_groups][0][filters][0][value]=from_date&
searchCriteria[filter_groups][1][filters][0][field]=created_at&
searchCriteria[filter_groups][1][filters][0][condition_type]=to&
searchCriteria[filter_groups][1][filters][0][value]=to_date
Example:
http://<magento_host>/rest/V1/orders?
searchCriteria[filter_groups][0][filters][0][field]=created_at&
searchCriteria[filter_groups][0][filters][0][condition_type]=from&
searchCriteria[filter_groups][0][filters][0][value]=2016-07-01 00:00:00&
searchCriteria[filter_groups][1][filters][0][field]=created_at&
searchCriteria[filter_groups][1][filters][0][condition_type]=to&
searchCriteria[filter_groups][1][filters][0][value]=2018-07-01 00:00:00
The above API will get you the orders between a by using created_at timestamp.
For an example also see the Magento Docs.

CRM 2011 Multiple Plugins firing at same time, retrieving invalid data

I am working on Dynamics CRM 2011,I have created a Order Product Create Plugin(Post Operation) and also an Order Product Delete Plugin (Pre Validation).
When an Order Product is created, my plugin retrieves a parent record and updates a quantity field according to the Order Product Quantity.
When an Order product is deleted, my delete plugin reverses this and adds back a quantity to a parent record.
My problem is that I have a custom HTML Resource that calls an OData Post that creates Order Products in Batches (simulating a Bulk Create), my Script calls this Creation in a loop. For instance, I may call the OData Create 5 times in a row to quickly create 5 custom Order Products, Or I may call it 10 times, depending on the users desire. It looks like my plugin is firing at the same time as the value that is retrieved from the parent record sometimes is the same instead of an updated value. My intention is that each plugin fire and update the parent record before the next plugin fires/retrieves the quantity value.
If I create 5 order products with a quantity of 1 each, I expect my parent record to decrement by 5. In reality it only decrements by 1 or 2 in a 5 Order Product Create situation. It looks like the retrieve org service calls in each plugin must be firing at the same time to grab the old value.
On the other hand, my delete plugin works perfectly in a Bulk Delete situation. I can delete 5 Order Products in a Bulk Delete and the Parent Record is updated correctly. For instance 5 Order products with quantity of 1 each, results in parent record updating by 5.
Why would a bulk delete work differently then me calling a Odata Post a few times. Do you think that moving this from a Plugin to a Workflow process would be a better solution?
Thank you
Ian
You should use Plugin Execution Order to execute plugins in order you want.
Execution order in plugin specifies the order, also known as rank, that plug-ins are executed within a pipeline stage. Plug-ins registered with an order value of 1 are executed first, followed by plug-ins registered with an order of 2, and so on. However, if there is more than one plug-in in a stage with the same order value, then the plug-in with the earliest compilation date is called first
See Image Below
You may be experiencing a race condition as a result of the plugin being triggered simultaneously after two or more Order Products are created at the same time for the same Product.
e.g. Product Lemon has 10 stocked items.
Order Product A orders 2 Lemons.
Order Product B orders 3 Lemons.
If Product Order A and B triggers your plugin at the same time, they will both grab the current stock count for Product Lemon which is 10.
Both plugin will deduct from 10 (i.e. Product Order A will be 10-2, while Product B will be 10-3). Depending on who gets to update the Product Lemon record last will be the new stock count for Product Lemon.
Solution:
Use a MUTEX to prevent a race condition in the calculation.
NB 1: MUTEX is not available in a CRM Online environment.
NB 2: (lock) is supported in CRM Online, but not for Cross-Process locking.

can i add multiple value in field ? (please see an image)

I need to do the stock management website with 2 Content type Supplier and Product. First I add contents in Supplier content type. Then I add node reference field in Product content type and call it supplier and make it multiple value. it's looks good, i can select suppliers and save it. but in my concept i need to check which supplier sale this product and how price. so i need to add suppliers and price in the same form like my image.
the propose of this form, user can check how price of each supplier for this product and they can choose the lowest price for purchase in the next process.
Guys, did you have the idea that i can do like this ?
Based on your image attached, I'd assume you have Druapl 7.
You can do this by installing Field Collection module. To get the table-input, install Field Collection Table.
Core Fields can have multiple instances of the same field. But you can't group them and make the whole group a multi-instance group.
Drupal 6 required CCK module to have fields, and there was a MultiGroup module (CCK 3branch which never had a stable release) that does the similar for Drupal 6.
Although OP will not need this, dear Googler if you are looking for a simple table with text fields, try TableField module