{"id":4526,"date":"2018-03-03T15:13:21","date_gmt":"2018-03-03T14:13:21","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=4526"},"modified":"2020-02-09T11:27:55","modified_gmt":"2020-02-09T10:27:55","slug":"creating-a-woocommerce-product-variation-rest-api","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/creating-a-woocommerce-product-variation-rest-api-4526\/","title":{"rendered":"Creating a WooCommerce product variation with the REST API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Like adding a product via the REST API is creating a product variation done relatively quickly \u2013 I use the PHP library for that, like in my last week\u2019s post.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Requirements for adding a variation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To make adding a variation work, we need to add the wanted attributes to the product first. This can look something like that while adding a product (<a href=\"https:\/\/florianbrinkmann.com\/en\/4520\/creating-woocommerce-product-rest-api\/\">my post from last week was about that<\/a>):<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$prod_data = &#91;\n\t<span class=\"hljs-string\">'name'<\/span>        =&gt; <span class=\"hljs-string\">'A great product'<\/span>,\n\t<span class=\"hljs-string\">'type'<\/span>        =&gt; <span class=\"hljs-string\">'variable'<\/span>,\n\t<span class=\"hljs-string\">'description'<\/span> =&gt; <span class=\"hljs-string\">'A very meaningful product description'<\/span>,\n\t<span class=\"hljs-string\">'images'<\/span>      =&gt; &#91;\n\t\t&#91;\n\t\t\t<span class=\"hljs-string\">'src'<\/span>      =&gt; <span class=\"hljs-string\">'https:\/\/shop.local\/path\/to\/image.jpg'<\/span>,\n\t\t\t<span class=\"hljs-string\">'position'<\/span> =&gt; <span class=\"hljs-number\">0<\/span>,\n\t\t],\n\t],\n\t<span class=\"hljs-string\">'categories'<\/span>  =&gt; &#91;\n\t\t&#91;\n\t\t\t<span class=\"hljs-string\">'id'<\/span> =&gt; <span class=\"hljs-number\">1<\/span>,\n\t\t],\n\t],\n\t<span class=\"hljs-string\">'attributes'<\/span>  =&gt; &#91;\n\t\t&#91;\n\t\t\t<span class=\"hljs-string\">'id'<\/span>        =&gt; <span class=\"hljs-number\">5<\/span>,\n\t\t\t<span class=\"hljs-string\">'variation'<\/span> =&gt; <span class=\"hljs-keyword\">true<\/span>,\n\t\t\t<span class=\"hljs-string\">'visible'<\/span>   =&gt; <span class=\"hljs-keyword\">true<\/span>,\n\t\t\t<span class=\"hljs-string\">'options'<\/span>   =&gt; &#91; <span class=\"hljs-string\">'S'<\/span>, <span class=\"hljs-string\">'M'<\/span>, <span class=\"hljs-string\">'L'<\/span> ],\n\t\t],\n\t],\n];\n\n$product = <span class=\"hljs-keyword\">$this<\/span>-&gt;woocommerce-&gt;post( <span class=\"hljs-string\">'products'<\/span>, $prod_data );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">We set an array of attributes for the <code class=\"lang-php\">attributes<\/code> key (just the size in the example) and can specify if this attributes should be available for variations via <code class=\"lang-php\">variation<\/code>. With <code class=\"lang-php\">options<\/code>, we define the available options for the variations \u2013 those attribute terms must exist before adding the product, like the attribute.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Besides that, we set the <code class=\"lang-php\">type<\/code> of the product to <code class=\"lang-php\">variable<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adding a product variation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After we meet the requirements, we can create a variation with the following code:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$variation_data = &#91;\n\t<span class=\"hljs-string\">'regular_price'<\/span> =&gt; <span class=\"hljs-string\">'15.00'<\/span>,\n\t<span class=\"hljs-string\">'image'<\/span>         =&gt; &#91;\n\t\t<span class=\"hljs-string\">'src'<\/span> =&gt; <span class=\"hljs-string\">'https:\/\/shop.local\/path\/to\/image_size_l.jpg'<\/span>,\n\t],\n\t<span class=\"hljs-string\">'attributes'<\/span>    =&gt; &#91;\n\t\t&#91;\n\t\t\t<span class=\"hljs-string\">'id'<\/span>     =&gt; <span class=\"hljs-number\">5<\/span>,\n\t\t\t<span class=\"hljs-string\">'option'<\/span> =&gt; <span class=\"hljs-string\">'L'<\/span>,\n\t\t],\n\t],\n];\n\n<span class=\"hljs-keyword\">$this<\/span>-&gt;woocommerce-&gt;post( <span class=\"hljs-string\">\"products\/$product-&gt;id\/variations\"<\/span>, $variation_data );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">We define the price, set an image and the size. For specifying the size, we pass the id of the attribute and the label of the option as an array inside the array of <code class=\"lang-php\">attributes<\/code>. We use the REST API endpoint <code class=\"lang-php\">products\/$product-&gt;id\/variations<\/code> \u2013 we can get the product\u2019s ID via <code class=\"lang-php\">$product-&gt;id<\/code> here, because we saved the call of the <code class=\"lang-php\">products<\/code> endpoint for creating the product in the <code class=\"lang-php\">$product<\/code> variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And that is it \u2013 we should have created a variable product with a variation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like adding a product via the REST API is creating a product variation done relatively quickly \u2013 I use the PHP library for that, like in my last week\u2019s post.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[115],"tags":[],"class_list":["post-4526","post","type-post","status-publish","format-standard","hentry","category-wordpress-snippets"],"wp-worthy-pixel":{"ignored":false,"public":"46ac065aeac94cdbbe3ce85ce12ffa17","server":"vg07.met.vgwort.de","url":"https:\/\/vg07.met.vgwort.de\/na\/46ac065aeac94cdbbe3ce85ce12ffa17"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=4526"}],"version-history":[{"count":4,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4526\/revisions"}],"predecessor-version":[{"id":5972,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4526\/revisions\/5972"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=4526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=4526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=4526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}