{"id":2245,"date":"2022-09-19T15:35:37","date_gmt":"2022-09-19T13:35:37","guid":{"rendered":"https:\/\/www.wp-moon.com\/?p=2245"},"modified":"2022-09-19T15:35:39","modified_gmt":"2022-09-19T13:35:39","slug":"create-woocommerce-products-programmatically","status":"publish","type":"post","link":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/","title":{"rendered":"How to create WooCommerce products programmatically"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If you want to want to create some products on your WooCommerce shop without your back-office interface, you can do it easily with the code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$my_product = new WC_Product_Simple();\r\n$my_product->set_name( 'My new product' );\r\n$my_product->set_status( 'publish' ); \r\n$my_product->set_catalog_visibility( 'visible' );\r\n$my_product->set_price( 9.90 );\r\n$my_product->set_regular_price( 9.90 );\r\n$my_product->set_sold_individually( true );\r\n$my_product->set_image_id( 1 );\r\n$product->set_category_ids( array( 1 ) );\r\n$my_product->save();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can fill these data :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>set_name<\/em>() is the title of your product<\/li><li><em><em>set_status<\/em>()<\/em> is the status : publish or draft for example<\/li><li><em>set_catalog_visibility<\/em>()<\/li><li><em>set_price<\/em>() is the price displayed for your customers<\/li><li><em><em>set_image_id<\/em>() <\/em>refers to your image ID that will be your featured image<\/li><li><em>set_category_ids<\/em>() is your categories ids<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>new WC_Product_Simple()<\/em> and <em>save()<\/em> must always be there. The complete documentation can be find here : <a href=\"https:\/\/woocommerce.github.io\/code-reference\/classes\/WC-Product-Simple.html\">WC_Product_Simple<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you need to create variable products programmatically, you can also check this documentation: <a href=\"https:\/\/woocommerce.github.io\/code-reference\/classes\/WC-Product-Variable.html\">WC_Product_Variable<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to want to create some products on your WooCommerce shop without your back-office interface, you can do it easily with the code below: You can fill these data : set_name() is the title of your product set_status() is the status : publish or draft for example set_catalog_visibility() set_price() is the price displayed &hellip; <a href=\"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to create WooCommerce products programmatically<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":2247,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-2245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fix-errors"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to create WooCommerce products programmatically - WP Moon<\/title>\n<meta name=\"description\" content=\"If you want to want to create some products on your WooCommerce shop without your back-office interface, you can do it easily with the code below:\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to create WooCommerce products programmatically - WP Moon\" \/>\n<meta property=\"og:description\" content=\"If you want to want to create some products on your WooCommerce shop without your back-office interface, you can do it easily with the code below:\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Moon\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpmoonwebsite\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-19T13:35:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-19T13:35:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/09\/delete-product-woocommerce.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2168\" \/>\n\t<meta property=\"og:image:height\" content=\"582\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Alexandre G\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@alexgaboriau\" \/>\n<meta name=\"twitter:site\" content=\"@WPMoon4\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alexandre G\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to create WooCommerce products programmatically - WP Moon","description":"If you want to want to create some products on your WooCommerce shop without your back-office interface, you can do it easily with the code below:","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/","og_locale":"en_US","og_type":"article","og_title":"How to create WooCommerce products programmatically - WP Moon","og_description":"If you want to want to create some products on your WooCommerce shop without your back-office interface, you can do it easily with the code below:","og_url":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/","og_site_name":"WP Moon","article_publisher":"https:\/\/www.facebook.com\/wpmoonwebsite","article_published_time":"2022-09-19T13:35:37+00:00","article_modified_time":"2022-09-19T13:35:39+00:00","og_image":[{"width":2168,"height":582,"url":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/09\/delete-product-woocommerce.png","type":"image\/png"}],"author":"Alexandre G","twitter_card":"summary_large_image","twitter_creator":"@alexgaboriau","twitter_site":"@WPMoon4","twitter_misc":{"Written by":"Alexandre G","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/#article","isPartOf":{"@id":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/"},"author":{"name":"Alexandre G","@id":"https:\/\/www.wp-moon.com\/en\/#\/schema\/person\/72a64e6910977ec9e4b32fd8da54369d"},"headline":"How to create WooCommerce products programmatically","datePublished":"2022-09-19T13:35:37+00:00","dateModified":"2022-09-19T13:35:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/"},"wordCount":125,"commentCount":0,"publisher":{"@id":"https:\/\/www.wp-moon.com\/en\/#organization"},"image":{"@id":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/09\/delete-product-woocommerce.png","articleSection":["Fix Errors"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/","url":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/","name":"How to create WooCommerce products programmatically - WP Moon","isPartOf":{"@id":"https:\/\/www.wp-moon.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/#primaryimage"},"image":{"@id":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/09\/delete-product-woocommerce.png","datePublished":"2022-09-19T13:35:37+00:00","dateModified":"2022-09-19T13:35:39+00:00","description":"If you want to want to create some products on your WooCommerce shop without your back-office interface, you can do it easily with the code below:","breadcrumb":{"@id":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/#primaryimage","url":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/09\/delete-product-woocommerce.png","contentUrl":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/09\/delete-product-woocommerce.png","width":2168,"height":582,"caption":"Create Product Programmatically"},{"@type":"BreadcrumbList","@id":"https:\/\/www.wp-moon.com\/en\/create-woocommerce-products-programmatically\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.wp-moon.com\/en\/"},{"@type":"ListItem","position":2,"name":"Fix Errors","item":"https:\/\/www.wp-moon.com\/en\/category\/fix-errors\/"},{"@type":"ListItem","position":3,"name":"How to create WooCommerce products programmatically"}]},{"@type":"WebSite","@id":"https:\/\/www.wp-moon.com\/en\/#website","url":"https:\/\/www.wp-moon.com\/en\/","name":"WP Moon","description":"","publisher":{"@id":"https:\/\/www.wp-moon.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wp-moon.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.wp-moon.com\/en\/#organization","name":"WP Moon","url":"https:\/\/www.wp-moon.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wp-moon.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2020\/02\/logo.png","contentUrl":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2020\/02\/logo.png","width":290,"height":95,"caption":"WP Moon"},"image":{"@id":"https:\/\/www.wp-moon.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/wpmoonwebsite","https:\/\/x.com\/WPMoon4"]},{"@type":"Person","@id":"https:\/\/www.wp-moon.com\/en\/#\/schema\/person\/72a64e6910977ec9e4b32fd8da54369d","name":"Alexandre G","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2023\/09\/312419460_10229483707296045_2919774527885824513_n-100x100.jpg","url":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2023\/09\/312419460_10229483707296045_2919774527885824513_n-100x100.jpg","contentUrl":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2023\/09\/312419460_10229483707296045_2919774527885824513_n-100x100.jpg","caption":"Alexandre G"},"description":"Alexandre, Web Developer &amp; Website Editor. I've been using WordPress for over 10 years in my professional environment and on personal projects. I created WP Moon to teach you how to use this CMS, solve your problems and show you all the tricks.","sameAs":["https:\/\/www.croque-pixel.com\/","https:\/\/x.com\/alexgaboriau","https:\/\/www.youtube.com\/@wp_moon\/videos"],"url":"https:\/\/www.wp-moon.com\/en\/author\/wp-moon\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/posts\/2245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/comments?post=2245"}],"version-history":[{"count":0,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/posts\/2245\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/media\/2247"}],"wp:attachment":[{"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/media?parent=2245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/categories?post=2245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/tags?post=2245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}