{"id":4520,"date":"2018-02-24T19:56:10","date_gmt":"2018-02-24T18:56:10","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=4520"},"modified":"2020-02-09T11:28:17","modified_gmt":"2020-02-09T10:28:17","slug":"creating-woocommerce-product-rest-api","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/creating-woocommerce-product-rest-api-4520\/","title":{"rendered":"Creating a WooCommerce product with PHP via the REST API"},"content":{"rendered":"\n<p>WooCommerce comes with a REST API that, for example, allows us to create products. Here I show you how to do this.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Installing and initializing the PHP library for the WooCommerce REST API<\/h2>\n\n\n\n<p>There are <a href=\"https:\/\/woocommerce.github.io\/woocommerce-rest-api-docs\/#libraries-and-tools\">official libraries for the WooCommerce REST API<\/a>, which make it possible to use the API \u2013 besides via cURL \u2013 with Node.js, PHP, Python, and Ruby. Here I show the usage of the <a href=\"https:\/\/packagist.org\/packages\/automattic\/woocommerce\">PHP library from Automattic<\/a>. For that, we first need to install it, what is quickly done with <a href=\"https:\/\/getcomposer.org\/\">Composer<\/a> (I assume that Composer is already installed for the project):<\/p>\n\n\n<pre class=\"wp-block-code lang-bash\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">composer require automattic\/woocommerce<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now we load it so that we can use the lib:<\/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\"><span class=\"hljs-comment\">\/\/ Load Composer autoloader.<\/span>\n<span class=\"hljs-comment\">\/\/ @link https:\/\/github.com\/brightnucleus\/jasper-client\/blob\/master\/tests\/bootstrap.php#L55-L59<\/span>\n$autoloader = dirname( <span class=\"hljs-keyword\">__FILE__<\/span> ) . <span class=\"hljs-string\">'\/vendor\/autoload.php'<\/span>;\n<span class=\"hljs-keyword\">if<\/span> ( is_readable( $autoloader ) ) {\n\t<span class=\"hljs-keyword\">require_once<\/span> $autoloader;\n}\n\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Automattic<\/span>\\<span class=\"hljs-title\">WooCommerce<\/span>\\<span class=\"hljs-title\">Client<\/span>;\n\n$woocommerce = <span class=\"hljs-keyword\">new<\/span> Client(\n\t<span class=\"hljs-string\">'https:\/\/shop.local'<\/span>,\n\t<span class=\"hljs-string\">'the_consumer_key'<\/span>,\n\t<span class=\"hljs-string\">'the_consumer_secret'<\/span>,\n\t&#91;\n\t\t<span class=\"hljs-string\">'wp_api'<\/span>  =&gt; <span class=\"hljs-keyword\">true<\/span>,\n\t\t<span class=\"hljs-string\">'version'<\/span> =&gt; <span class=\"hljs-string\">'wc\/v2'<\/span>,\n\t]\n);<\/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>To do that, we include the Composer autoloader if it exists (if the plugin is part of a larger project that is managed via Composer, our subproject would not get an own autoloader file, but the classes would be included in the autoloader of the parent project) and use the <code class=\"lang-php\">use<\/code> statement so we can access the <code class=\"lang-php\">Client<\/code> class more quickly. Now we create a <code class=\"lang-php\">Client<\/code> object and save it in the <code class=\"lang-php\">$woocommerce<\/code> variable. These are the parameters we pass to the <code class=\"lang-php\">new Client()<\/code> call:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>the shop URL.<\/li><li>the consumer key.<\/li><li>the consumer secret.<\/li><li>an array of additional options, in our case:\n<ul>\n<li>the permission to make requests to the new WP-REST API integration, which is available since WooCommerce 2.6.<\/li>\n<li>the version of the API.<\/li>\n<\/ul>\n<\/li><\/ul>\n\n\n\n<p>You can generate consumer secret and consumer key in the WordPress backend under <em>WooCommerce<\/em> \u203a <em>Settings<\/em> \u203a <em>API<\/em> \u203a <em>Keys\/Apps<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a product with the REST API<\/h2>\n\n\n\n<p>Now let us create a product:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-3\" 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\">'simple'<\/span>,\n\t<span class=\"hljs-string\">'regular_price'<\/span> =&gt; <span class=\"hljs-string\">'15.00'<\/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];\n\n$woocommerce-&gt;post( <span class=\"hljs-string\">'products'<\/span>, $prod_data );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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>This code adds a product with the title \u00bbA great product\u00ab, a meaningful description, and a picture, for the price of 15,00 dollar (or whatever is set as the currency of the shop) in the category with the ID <code class=\"lang-php\">1<\/code>.<\/p>\n\n\n\n<p><a href=\"https:\/\/woocommerce.github.io\/woocommerce-rest-api-docs\/?php#products\">You can find all available options for products in the docs<\/a>.<\/p>\n\n\n\n<p>Of course, my example with hard-coded data is a little bit pointless. But in this way, for example, you could loop the products from an export file or string and add them to WooCommerce relatively easy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WooCommerce comes with a REST API that, for example, allows us to create products. Here I show you how to do this.<\/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-4520","post","type-post","status-publish","format-standard","hentry","category-wordpress-snippets"],"wp-worthy-pixel":{"ignored":false,"public":"4e9eb99fe93d458a840fef8932088c87","server":"vg04.met.vgwort.de","url":"https:\/\/vg04.met.vgwort.de\/na\/4e9eb99fe93d458a840fef8932088c87"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4520","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=4520"}],"version-history":[{"count":4,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4520\/revisions"}],"predecessor-version":[{"id":5973,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4520\/revisions\/5973"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=4520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=4520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=4520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}