{"id":1876,"date":"2023-05-31T10:00:00","date_gmt":"2023-05-31T15:00:00","guid":{"rendered":"http:\/\/racmanuel.dev\/?p=1876"},"modified":"2023-05-27T19:15:19","modified_gmt":"2023-05-28T00:15:19","slug":"como-actualizar-el-stock-de-woocommerce-con-una-api-externa","status":"publish","type":"post","link":"https:\/\/racmanuel.dev\/en\/como-actualizar-el-stock-de-woocommerce-con-una-api-externa\/","title":{"rendered":"How to update WooCommerce stock using an external API?"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Updating WooCommerce stock via an external API is a common task for online store owners who use a variety of systems to manage their inventory. In this tutorial, I will show you how to update WooCommerce stock using an external API with PHP.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1880\" height=\"1255\" src=\"http:\/\/racmanuel.dev\/wp-content\/uploads\/pexels-photo-1181354-1.jpeg\" alt=\"software engineer standing beside server racks\" class=\"wp-image-2220\" title=\"\" srcset=\"https:\/\/racmanuel.dev\/wp-content\/uploads\/pexels-photo-1181354-1.jpeg 1880w, https:\/\/racmanuel.dev\/wp-content\/uploads\/pexels-photo-1181354-1-600x401.jpeg 600w, https:\/\/racmanuel.dev\/wp-content\/uploads\/pexels-photo-1181354-1-300x200.jpeg 300w, https:\/\/racmanuel.dev\/wp-content\/uploads\/pexels-photo-1181354-1-1024x684.jpeg 1024w, https:\/\/racmanuel.dev\/wp-content\/uploads\/pexels-photo-1181354-1-768x513.jpeg 768w, https:\/\/racmanuel.dev\/wp-content\/uploads\/pexels-photo-1181354-1-1536x1025.jpeg 1536w\" sizes=\"(max-width: 1880px) 100vw, 1880px\" \/><figcaption class=\"wp-element-caption\">Photo by Christina Morillo on <a href=\"https:\/\/www.pexels.com\/photo\/software-engineer-standing-beside-server-racks-1181354\/\" rel=\"nofollow noopener\" target=\"_blank\">Pexels.com<\/a><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"paso-1-obtener-las-credenciales-de-la-api-externa\">Step 1: Obtain the external API credentials<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The first thing you need to do is obtain the credentials for the external API you wish to use to update WooCommerce stock. Each API will have its own set of credentials, so be sure to follow the API provider's instructions to obtain them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"paso-2-crear-el-archivo-php-para-actualizar-el-stock\">Step 2: Create the PHP file to update the stock<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have the external API credentials, it is time to create the PHP file that will update the WooCommerce stock. You can create a new PHP file in the root directory of your WooCommerce website and name it \u201cupdate-stock.php\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the \u201cupdate-stock.php\u201d file, you will need to add the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nrequire_once('\/path\/to\/wp-load.php'); \/\/ Reemplaza '\/path\/to\/wp-load.php' con la ruta correcta a tu archivo wp-load.php\nrequire_once('\/path\/to\/woocommerce\/includes\/class-wc-product.php'); \/\/ Reemplaza '\/path\/to\/woocommerce\/includes\/class-wc-product.php' con la ruta correcta a tu archivo class-wc-product.php\n\n$product_id = 123; \/\/ Reemplaza '123' con el ID del producto que deseas actualizar\n$new_stock_quantity = 50; \/\/ Reemplaza '50' con la nueva cantidad de stock\n\n$product = new WC_Product($product_id);\n$product-&gt;set_stock_quantity($new_stock_quantity);\n$product-&gt;save();\n\n?&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this code, we are including the necessary WooCommerce files to update the stock, and then defining the product ID we wish to update and the new stock quantity. Next, we create a WooCommerce product object and update the product's stock using the \u201cset_stock_quantity()\u201d method, and save the changes using the \u201csave()\u201d method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"paso-3-configurar-la-api-externa-para-actualizar-el-stock\">Step 3: Configure the external API to update the stock<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have created the \u201cupdate-stock.php\u201d file, it is time to configure the external API to update the WooCommerce stock. How this is done will depend on the API provider, so be sure to follow the instructions provided by them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In general, you will need to send a POST request to \u201cupdate-stock.php\u201d and provide the external API credentials along with the details of the product you wish to update. This may require the use of a PHP library for sending HTTP requests, such as cURL or Guzzle.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Updating WooCommerce stock with an external API is a useful task that can save a lot of time and effort when managing your inventory. By following the steps described in this tutorial, you will be able to create a PHP file to update WooCommerce stock and configure your external API to send requests to that file. This is one of the many ways to update your WooCommerce stock; in the next tutorial, I will show you how to do it using the WooCommerce API. If you encounter issues, be sure to review the documentation for both the external API and WooCommerce to resolve them. Good luck!<\/p>\n\n\n<style><\/style><style><\/style>\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<h4 class=\"wp-block-heading has-large-font-size\" id=\"necesitas-ayuda-con-un-proyecto-web\">Do you need help with a web project?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Do you need help with a web project? Don&#039;t hesitate to contact me. I develop complete and customized solutions with WordPress and PHP, using modern tools and processes, HTML, CSS, SCSS, PHP, JavaScript, Bootstrap, and more. Ready? Send me a message and let&#039;s talk about your web project!<\/p>\n\n\n<div class=\"wp-block-groundhogg-forms\">\n\t<div class=\"gh-form-wrapper\"><form method=\"post\" class=\"gh-form gh-form-v2\" target=\"_parent\" enctype=\"multipart\/form-data\" name=\"Formulario de Contacto\" id=\"gh-form-24\" data-id=\"24\" action=\"\"><div class=\"gh-form-fields\"><div class=\"gh-form-column col-1-of-2\"><label for=\"first_name\">Name <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><input type=\"text\" name=\"first_name\" id=\"first_name\" class=\"gh-input gh-first-name\" value=\"\" required><\/div><\/div><div class=\"gh-form-column col-1-of-2\"><label for=\"last_name\">Last name(s) <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><input type=\"text\" name=\"last_name\" id=\"last_name\" class=\"gh-input gh-last-name\" value=\"\" required><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"email\">Email <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><input type=\"email\" name=\"email\" id=\"email\" class=\"gh-input gh-email\" value=\"\" required><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"primary_phone\">Phone or WhatsApp <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><input type=\"tel\" name=\"primary_phone\" id=\"primary_phone\" class=\"gh-input\" value=\"\" required><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"nombre_de_tu_negocio_o_empresa\">Name of your Business or Company<\/label><div class=\"gh-form-input-field\"><input type=\"text\" name=\"nombre_de_tu_negocio_o_empresa\" id=\"nombre_de_tu_negocio_o_empresa\" class=\"gh-input\" value=\"\"\/><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"link_de_tu_pagina_web\">Link to your website<\/label><div class=\"gh-form-input-field\"><input type=\"url\" name=\"link_de_tu_pagina_web\" id=\"link_de_tu_pagina_web\" class=\"gh-input\" value=\"\" placeholder=\"If your business or company has a website, enter the link here.\"\/><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"question_type\">What kind of question do you have? <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><select name=\"question_type\" id=\"question_type\" class=\"gh-input\" required><option value=\"Cotizaci\u00f3n\">Price<\/option><option value=\"General\">General<\/option><option value=\"Soporte\">Medium<\/option><\/select><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"message\">Ask <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><textarea name=\"message\" id=\"message\" class=\"gh-input\" rows=\"7\" placeholder=\"Your question or doubt...\" type=\"text\" required><\/textarea><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><div class=\"consent gh-gdpr\" id=\"gdpr_consent\"><div ><label class=\"gh-checkbox-label\"><input type=\"checkbox\" name=\"data_processing_consent\" id=\"data-processing-consent\" class=\"\" value=\"yes\" required><span class=\"checkbox-label\">I agree to racmanuel.dev&#8217;s storage and processing of my personal data. <span class=\"required\">*<\/span><\/span><\/label><\/div><div ><label class=\"gh-checkbox-label\"><input type=\"checkbox\" name=\"marketing_consent\" id=\"marketing-consent\" class=\"\" value=\"yes\"\/><span class=\"checkbox-label\">I agree to receive marketing offers and updates from racmanuel.dev.<\/span><\/label><\/div><\/div><\/div><div class=\"gh-form-column col-1-of-3\"><button type=\"submit\" class=\"gh-submit gh-button primary\" value=\"\">Get in touch!<\/button><\/div><\/div><input type=\"hidden\" name=\"trp-form-language\" value=\"en\"\/><\/form><\/div><\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Updating WooCommerce stock with an external API is a useful task when you want to connect systems to manage your inventory. In this tutorial, I will show you how to update WooCommerce stock.<\/p>","protected":false},"author":1,"featured_media":2220,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","_glsr_average":0,"_glsr_ranking":0,"_glsr_reviews":0,"footnotes":""},"categories":[48],"tags":[40,42],"niveles":[39],"class_list":["post-1876","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutoriales","tag-woocommerce","tag-wordpress","niveles-avanzado"],"blocksy_meta":[],"acf":[],"_links":{"self":[{"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/posts\/1876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/comments?post=1876"}],"version-history":[{"count":4,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/posts\/1876\/revisions"}],"predecessor-version":[{"id":2222,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/posts\/1876\/revisions\/2222"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/media\/2220"}],"wp:attachment":[{"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/media?parent=1876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/categories?post=1876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/tags?post=1876"},{"taxonomy":"niveles","embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/niveles?post=1876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}