Create a gift card product
Learn how to create a gift card product using the REST API and the 51黑料不打烊 Commerce Admin.
Who is this video for?
- Website managers
- eCommerce merchandisers
- New 51黑料不打烊 Commerce developers who want to learn how to create products in 51黑料不打烊 Commerce using the REST API.
Video content
Transcript
creating a gift card with 51黑料不打烊 Commerce, we鈥檙e gonna be using the APIs. And then we鈥檙e also gonna show you what this looks like using the Commerce admin. But in the end, the example for the API, we鈥檙e gonna create a product simple, very similar to this, where we鈥檙e gonna be able to pick an amount or define your own amount within a range of 10 to $100. You鈥檒l be able to set the sender鈥檚 name and the recipient鈥檚 name and then a message, and then add it to cart. So let鈥檚 get started. The first place we鈥檙e gonna do is we鈥檙e gonna stop and find some values that we gonna need for our APIs. And the first one we鈥檙e gonna need is the attribute set that you may have a special one created for gift cards. If not, then the default attribute set will probably work. My 51黑料不打烊 Commerce instance has several attribute sets, but for this one, I鈥檓 just gonna use the default attribute set. And the ID number for that is number four. So you鈥檒l have to check out your own installation and figure out which attributes that make sense. But once you do click on the link and then up in the URL bar, just see the ID number, and then you鈥檙e gonna use that for your API. The second thing that we need is the product attribute for gift card amount. And the reason for this is the API is expecting the ID number for this, not the code. And so same problem, the same premise is you will go to your admin, you will open it up and you鈥檒l look at the ID number that鈥檚 been associated for gift card amounts. And in my use case, it鈥檚 ID number 134. So those are the two numbers four and 134 that I need to make sure that I remember when I do my APIs. Let鈥檚 go ahead and go over to our postman. And we鈥檙e gonna show you a simple payload with a few customizations for a few of the values. And in the documentation, I do provide a more complete payload that has a more granular look on all of the potential values that you can set, but for simplicity and to make sure that the minimum to mimic the product that I showed you earlier, this is the sample payload that we鈥檙e gonna use. For authentication, I did choose to use the bearer token, which is not recommended by 51黑料不打烊 commerce and for better security, you should use OAuth or at worst case, the admin users credentials. But for the simple use case that I鈥檓 showing you today, I chose to use the bearer token, which has been deprecated, but I enabled it just for this purpose. So for the payload and the URL, it鈥檚 gonna be the same URL that you use when you create any other product type. So it鈥檚 gonna be REST and then the store code and then V1 and then products. And you鈥檙e gonna use a post method and we鈥檒l start at the top. A couple of the bare minimum requirements are a SKU and a name. And then the type in this case is a gift card. Here is my attribute set ID that I talked about earlier. And this is gonna be number four. And then we鈥檒l just scroll down a little bit inside of the extension attributes. There鈥檚 a couple of values that I set just because I wanted the product to be available a little bit quicker and not have to manually set it. So I did give it a quantity and I did force it to be in stock of being true. And then under the gift card amounts node, this is where you would use that attribute ID for the product attribute. And so in my case for gift card amounts, that attribute ID is 134, which is why I defined it here. And then I set a value of 10 and then I set an alternate value using the same ID, a value of 20. So those are my two default values. And through configuration for this, it is another option we did set is allow the customer to be able to choose between 10 and $100. And we鈥檒l go over that in a minute. For the gift message, once again, this is just an option that I decided to utilize just so you can see what it looks like. And then just a couple of other nodes in this section. But the thing to remember more than anything is that some of these values aren鈥檛 in extension attributes, they鈥檙e actually in custom attributes. This kind of threw me for a loop when I got started doing this investigation for this tutorial. And I wasn鈥檛 fully aware that some of these attributes actually lived in the custom attributes versus in a node by themselves or an extension attributes. So under custom attributes, there鈥檚 a lot of values that we can set here. The one in particular that I wanna use is this attribute code, which is going to be gift card amounts. And then the attribute ID that鈥檚 associated with it is 134. The value, this is where we鈥檙e setting some predefined values that they can choose from. So in this case, I chose a $10 option and then a $20 option. And then the open amount and minimum and maximum, these are for the option if someone chooses to set their own. So it starts at $10 and they can do any number between 10 and $100. So this is my fairly simple payload. I could have removed a few of these examples, but I wanted to see a fairly flexible and powerful version of a gift card. And this is a pretty good option. So if we鈥檙e ready and everything does look good, I鈥檓 gonna go ahead and hit send. And there we go. Let鈥檚 check the commerce site. Depending on how your site is set up, you might have to do a re-index. So I鈥檓 going to do that real quick because I do not have crons running on my local environment. And great. So the SKU that I was trying to create was gift card 21. And the name that I was trying to create was called gift card 21. And if I go back to my front end, there it is. So if I click on it, this has all the configurations that I was hoping. You get to choose an amount. You can also define your amount anywhere between 10 and $100. So we鈥檒l do something like 16. We鈥檒l give it a name. Add it to my cart. Everything looks good. Now we鈥檙e gonna go ahead and do the same thing, but we鈥檙e gonna do this via the admin. The benefit to the admin is you don鈥檛 have to look up some of these values ahead of time, like the attribute set ID or the gift card amount ID for the product attribute. But other than that, it鈥檚 fairly simple, fairly straightforward. And you can do all the options that we have with the APIs, except for just using the graphical UI. So to do that, you鈥檙e gonna go to catalog products and under add product, you鈥檙e gonna do gift card. And this is where you just have to use the UI to choose your attribute set. You don鈥檛 have to remember the ID, which is great. So we鈥檙e gonna do gift card 24. And you鈥檒l notice that it automatically filled up my SKU. That鈥檚 fine. This is where you can define whether it鈥檚 physical or combined or virtual. We鈥檒l just do a virtual one for now. We鈥檒l add an amount of $16. And we will do some inventory. We鈥檒l just say, nah, don鈥檛 bother. We don鈥檛 need you to manage inventory cause it鈥檚 virtual. We鈥檒l go down to gift card information. There are some options that you can set here or you can just leave it as is, that鈥檚 fine. So if you鈥檙e happy and there鈥檚 nothing else that you wanna add, you can go ahead and hit save and close. And once again, because I don鈥檛 have crowns running, I鈥檓 gonna go ahead and kick off a re-index. All right, and so on my front end, on my homepage, I should have this new gift card 24. So let鈥檚 go see if it鈥檚 there. There it is, great. So if I click on it, you鈥檒l see I have a few more options because my values were slightly different than my API, but basically the same premise. So we鈥檒l just do Ross, Ross at adobe.com. Jerry, Jerry at adobe.com. All right, and just add to cart. Everything looks great. So that鈥檚 how you would create a gift card in 51黑料不打烊 Commerce using the APIs as well as the Commerce admin. Don鈥檛 forget to continue your experience and your learning through experience league, trying to find new ways to expand your knowledge on 51黑料不打烊 Commerce.
Create a gift card with a simple payload
The following request example shows the payload to create a gift card like the one shown in the video. This smaller payload overrides the default settings for a subset of the available attributes. The remaining attributes not included in the payload remain set to the default values.
curl --location '{{your.url.here}}/rest/default/V1/products' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer {{Your Bearer Token}}' \
--header 'Cookie: PHPSESSID=83b55460e3ab1bf903fab59dfedc81c6; private_content_version=41d94540f5bd8b691017a850bc3b82b3' \
--data '{
"product": {
"sku": "giftcard21",
"name": "giftcard21",
"attribute_set_id": {{Your Attribute Set ID}},
"price": 0,
"status": 1,
"visibility": 4,
"type_id": "giftcard",
"weight": 1,
"extension_attributes": {
"website_ids": [
1
],
"stock_item": {
"qty": 1000,
"is_in_stock": true
},
"giftcard_amounts": [
{
"attribute_id": {{Your attribute ID for giftcard_amounts},
"website_id": 0,
"value": 10,
"website_value": null
},
{
"attribute_id": {{Your attribute ID for giftcard_amounts},
"website_id": 0,
"value": 20,
"website_value": null
}
]
},
"custom_attributes": [
{
"attribute_code": "gift_message_available",
"value": "2"
},
{
"attribute_code": "is_redeemable",
"value": "1"
},
{
"attribute_code": "required_options",
"value": "1"
},
{
"attribute_code": "use_config_is_redeemable",
"value": "1"
},
{
"attribute_code": "has_options",
"value": "1"
},
{
"attribute_code": "allow_message",
"value": "1"
},
{
"attribute_code": "giftcard_type",
"value": "1"
},
{
"attribute_code": "giftcard_amounts",
"value": [
{
"website_id": "0",
"value": "10.0000",
"attribute_id": "{{Your attribute ID for giftcard_amounts}",
"website_value": 10
},
{
"website_id": "0",
"value": "20.0000",
"attribute_id": "{{Your attribute ID for giftcard_amounts}",
"website_value": 20
}
]
},
{
"attribute_code": "allow_open_amount",
"value": "1"
},
{
"attribute_code": "open_amount_min",
"value": "10.000000"
},
{
"attribute_code": "open_amount_max",
"value": "100.000000"
},
{
"attribute_code": "is_returnable",
"value": "2"
}
]
}
}'
Create a gift card with a full payload
The following example shows the POST request to create a gift card with a full payload. The payload includes all attributes that can configured when you create a gift card. If you use this code sample, customize the configuration by updating the default values for each attribute as needed before submitting the request.
curl --location '{{your.url.here}}/rest/default/V1/products' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer {{Your Bearer Token}}' \
--header 'Cookie: PHPSESSID=83b55460e3ab1bf903fab59dfedc81c6; private_content_version=41d94540f5bd8b691017a850bc3b82b3' \
--data '{
"product": {
"sku": "giftcard22",
"name": "giftcard22",
"attribute_set_id": {{Your Attribute Set ID}},
"price": 0,
"status": 1,
"visibility": 4,
"type_id": "giftcard",
"created_at": "2024-01-24 20:54:54",
"updated_at": "2024-01-24 20:54:54",
"weight": 1,
"extension_attributes": {
"website_ids": [
1
],
"stock_item": {
"qty": 1000,
"is_in_stock": true,
"is_qty_decimal": false,
"show_default_notification_message": false,
"use_config_min_qty": true,
"min_qty": 0,
"use_config_min_sale_qty": 1,
"min_sale_qty": 1,
"use_config_max_sale_qty": true,
"max_sale_qty": 10000,
"use_config_backorders": true,
"backorders": 0,
"use_config_notify_stock_qty": true,
"notify_stock_qty": 1,
"use_config_qty_increments": true,
"qty_increments": 0,
"use_config_enable_qty_inc": true,
"enable_qty_increments": false,
"use_config_manage_stock": true,
"manage_stock": true,
"low_stock_date": null,
"is_decimal_divided": false,
"stock_status_changed_auto": 0
},
"giftcard_amounts": [
{
"attribute_id": {{Your attribute ID for giftcard_amounts},
"website_id": 0,
"value": 10,
"website_value": null
},
{
"attribute_id": {{Your attribute ID for giftcard_amounts},
"website_id": 0,
"value": 20,
"website_value": null
}
]
},
"product_links": [],
"options": [],
"media_gallery_entries": [],
"tier_prices": [],
"custom_attributes": [
{
"attribute_code": "options_container",
"value": "container2"
},
{
"attribute_code": "url_key",
"value": "giftcard22"
},
{
"attribute_code": "gift_message_available",
"value": "2"
},
{
"attribute_code": "is_redeemable",
"value": "1"
},
{
"attribute_code": "required_options",
"value": "1"
},
{
"attribute_code": "use_config_is_redeemable",
"value": "1"
},
{
"attribute_code": "has_options",
"value": "1"
},
{
"attribute_code": "lifetime",
"value": "0"
},
{
"attribute_code": "use_config_lifetime",
"value": "1"
},
{
"attribute_code": "email_template",
"value": "giftcard_email_template"
},
{
"attribute_code": "use_config_email_template",
"value": "1"
},
{
"attribute_code": "allow_message",
"value": "1"
},
{
"attribute_code": "meta_title",
"value": "giftcard22"
},
{
"attribute_code": "use_config_allow_message",
"value": "1"
},
{
"attribute_code": "gift_wrapping_available",
"value": "2"
},
{
"attribute_code": "meta_keyword",
"value": "giftcard22"
},
{
"attribute_code": "giftcard_type",
"value": "1"
},
{
"attribute_code": "giftcard_amounts",
"value": [
{
"website_id": "0",
"value": "10.0000",
"attribute_id": "{{Your attribute ID for giftcard_amounts}",
"website_value": 10
},
{
"website_id": "0",
"value": "20.0000",
"attribute_id": "{{Your attribute ID for giftcard_amounts}",
"website_value": 20
}
]
},
{
"attribute_code": "allow_open_amount",
"value": "1"
},
{
"attribute_code": "open_amount_min",
"value": "10.000000"
},
{
"attribute_code": "open_amount_max",
"value": "100.000000"
},
{
"attribute_code": "meta_description",
"value": "giftcard22"
},
{
"attribute_code": "is_returnable",
"value": "2"
}
]
}
}'
3a5f7e19-f383-4af8-8983-d01154c1402f