### Example of Detailed Steps for Enabling an Experiment Source: https://developer.woocommerce.com/docs/llms-full.txt Provides a clear, step-by-step guide for enabling a specific experiment, avoiding assumptions about user knowledge. This format is useful when specific plugin installations and navigation are required. ```text - Install the WooCommerce Beta Tester plugin. - Go to `Tools > WCA Test Helper > Experiments`. - Toggle the [x] experiment. ``` -------------------------------- ### Ruby Setup Source: https://developer.woocommerce.com/docs/apis/rest-api Example of how to set up and use the WooCommerce REST API with the official Ruby library. ```APIDOC ## Ruby Setup ### Description This snippet demonstrates how to install, set up, and initialize the WooCommerce REST API client using Ruby. ### Installation ```bash gem install woocommerce_api ``` ### Setup ```ruby require "woocommerce_api" woocommerce = WooCommerce::API.new( "https://example.com", # Your store URL "consumer_key", # Your consumer key "consumer_secret", # Your consumer secret { wp_api: true, # Enable the WP REST API integration version: "wc/v3" # WooCommerce WP REST API version } ) ``` ``` -------------------------------- ### PHP Setup Source: https://developer.woocommerce.com/docs/apis/rest-api Example of how to set up and use the WooCommerce REST API with the official PHP library. ```APIDOC ## PHP Setup ### Description This snippet demonstrates how to install, set up, and initialize the WooCommerce REST API client using PHP. ### Installation ```bash composer require automattic/woocommerce ``` ### Setup ```php true, // Enable the WP REST API integration 'version' => 'wc/v3' // WooCommerce WP REST API version ] ); ?> ``` ``` -------------------------------- ### Python Setup Source: https://developer.woocommerce.com/docs/apis/rest-api Example of how to set up and use the WooCommerce REST API with the official Python library. ```APIDOC ## Python Setup ### Description This snippet shows how to install, set up, and initialize the WooCommerce REST API client using Python. ### Installation ```bash pip install woocommerce ``` ### Setup ```python from woocommerce import API wcapi = API( url="http://example.com", # Your store URL consumer_key="consumer_key", # Your consumer key consumer_secret="consumer_secret", # Your consumer secret wp_api=True, # Enable the WP REST API integration version="wc/v3" # WooCommerce WP REST API version ) ``` ``` -------------------------------- ### Start Local WordPress Environment Source: https://developer.woocommerce.com/docs/features/analytics/extending-woocommerce-admin-reports Commands to install and start a local WordPress development environment using wp-env. ```bash npm -g i @wordpress/env wp-env start ``` -------------------------------- ### JavaScript Setup Source: https://developer.woocommerce.com/docs/apis/rest-api Example of how to set up and use the WooCommerce REST API with the official JavaScript library. ```APIDOC ## JavaScript Setup ### Description This snippet shows how to install, set up, and initialize the WooCommerce REST API client using JavaScript. ### Installation ```bash npm install --save @woocommerce/woocommerce-rest-api ``` ### Setup ```javascript const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default; // import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api"; // Supports ESM const WooCommerce = new WooCommerceRestApi({ url: 'http://example.com', // Your store URL consumerKey: 'consumer_key', // Your consumer key consumerSecret: 'consumer_secret', // Your consumer secret version: 'wc/v3' // WooCommerce WP REST API version }); ``` ``` -------------------------------- ### Ruby Library Setup Source: https://developer.woocommerce.com/docs/category/rest-api Example of how to set up and use the official Ruby library for the WooCommerce REST API. ```APIDOC ## Ruby Library Setup ### Description This snippet provides instructions for installing and setting up the official Ruby library for the WooCommerce REST API. ### Installation ```bash gem install woocommerce_api ``` ### Setup ```ruby require "woocommerce_api" woocommerce = WooCommerce::API.new( "https://example.com", # Your store URL "consumer_key", # Your consumer key "consumer_secret", # Your consumer secret { wp_api: true, # Enable the WP REST API integration version: "wc/v3" # WooCommerce WP REST API version } ) ``` ``` -------------------------------- ### Python Library Setup Source: https://developer.woocommerce.com/docs/category/rest-api Example of how to set up and use the official Python library for the WooCommerce REST API. ```APIDOC ## Python Library Setup ### Description This snippet shows the installation and setup process for the official Python library to interact with the WooCommerce REST API. ### Installation ```bash pip install woocommerce ``` ### Setup ```python from woocommerce import API wcapi = API( url="http://example.com", # Your store URL consumer_key="consumer_key", # Your consumer key consumer_secret="consumer_secret", # Your consumer secret wp_api=True, # Enable the WP REST API integration version="wc/v3" # WooCommerce WP REST API version ) ``` ``` -------------------------------- ### JavaScript Library Setup Source: https://developer.woocommerce.com/docs/llms-full.txt Instructions and code example for setting up the official JavaScript library to interact with the WooCommerce REST API. ```APIDOC ## JavaScript Library Setup ### Description This snippet shows how to install and set up the official JavaScript library for the WooCommerce REST API. ### Installation ```bash npm install --save @woocommerce/woocommerce-rest-api ``` ### Setup ```javascript // Supports ESM // import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api"; const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default; const WooCommerce = new WooCommerceRestApi({ url: 'http://example.com', // Your store URL consumerKey: 'consumer_key', // Your consumer key consumerSecret: 'consumer_secret', // Your consumer secret version: 'wc/v3' // WooCommerce WP REST API version }); ``` ``` -------------------------------- ### List General Settings (Python) Source: https://developer.woocommerce.com/docs/apis/rest-api/v3/setting-options This Python example shows how to get general settings using the wcapi library. It prints the JSON response. ```python print(wcapi.get("settings/general").json()) ``` -------------------------------- ### Start Development Server Source: https://developer.woocommerce.com/docs/llms-full.txt Navigate to your newly created extension directory and run this command to start the development server. ```sh cd my-extension-name npm run start ``` -------------------------------- ### Main Plugin File Initialization Source: https://developer.woocommerce.com/docs/llms-full.txt Initializes the `Setup` class if in the admin area and instantiates the `Product` class for front-end functionality. This ensures both admin and front-end features are loaded correctly. ```php public function __construct() { if ( is_admin() ) { new Setup(); } new WooProductField\Product(); } ``` -------------------------------- ### Initialize front-end product class Source: https://developer.woocommerce.com/docs/how-to-add-a-custom-field-to-simple-and-variable-products Sets up the basic structure for a front-end product class. ```php hooks(); } private function hooks() { } } ``` -------------------------------- ### Get Available Email Templates - Python Source: https://developer.woocommerce.com/docs/apis/rest-api/v3/order-actions This Python example demonstrates how to get available email templates for an order using the WooCommerce API. It sends a GET request and prints the JSON response. ```python print(wcapi.get("orders/723/actions/email_templates").json()) ``` -------------------------------- ### Get Products with Offset Source: https://developer.woocommerce.com/docs/extensions/core-concepts/wc-get-products Retrieves a specific range of products by using `limit` and `offset`. This example gets the second to fifth most-recent products. ```php // Get second to fifth most-recent products. $args = array( 'limit' => 4, 'offset' => 1, ); $products = wc_get_products( $args ); ``` -------------------------------- ### GET Requests for Product Collection Data Source: https://developer.woocommerce.com/docs/llms-full.txt Examples of GET requests to the Product Collection Data endpoint with different parameters to calculate specific data. ```http GET /products/collection-data GET /products/collection-data?calculate_price_range=true GET /products/collection-data?calculate_attribute_counts[0][query_type]=or&calculate_attribute_counts[0][taxonomy]=pa_color GET /products/collection-data?calculate_rating_counts=true GET /products/collection-data?calculate_taxonomy_counts=product_cat ``` -------------------------------- ### Setup WooCommerce REST API Client in JavaScript Source: https://developer.woocommerce.com/docs/apis/rest-api Installs and sets up the JavaScript client for the WooCommerce REST API. Ensure you have Node.js and npm installed. ```javascript // Install: // npm install --save @woocommerce/woocommerce-rest-api // Setup: const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default; // import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api"; // Supports ESM const WooCommerce = new WooCommerceRestApi({ url: 'http://example.com', // Your store URL consumerKey: 'consumer_key', // Your consumer key consumerSecret: 'consumer_secret', // Your consumer secret version: 'wc/v3' // WooCommerce WP REST API version }); ``` -------------------------------- ### Create a Simple Product Source: https://developer.woocommerce.com/docs/llms-full.txt Use this endpoint to create a new product. The example demonstrates creating a simple product with details like name, price, description, categories, and images. ```http POST /wp-json/wc/v1/products ``` -------------------------------- ### Pagination Example Source: https://developer.woocommerce.com/docs/apis/store-api Demonstrates how to list products with custom pagination settings. ```APIDOC ## Pagination If collections contain many results, they may be paginated. When listing resources you can pass the following parameters: | Parameter | Description | | :--------- | :------------------------------------------------------------------------------------- | | `page` | Current page of the collection. Defaults to `1`. | | `per_page` | Maximum number of items to be returned in result set. Defaults to `10`. Maximum `100`. | In the example below, we list 20 products per page and return page 2. ```sh curl "https://example-store.com/wp-json/wc/store/v1/products?page=2&per_page=20" ``` Additional pagination headers are also sent back with extra information. | Header | Description | | :---------------- | :------------------------------------------------------------------------ | | `X-WP-Total` | The total number of items in the collection. | | `X-WP-TotalPages` | The total number of pages in the collection. | | `Link` | Contains links to other pages; `next`, `prev`, and `up` where applicable. | ``` -------------------------------- ### Install Dependencies and Build Extension Source: https://developer.woocommerce.com/docs/how-to-add-a-custom-field-to-simple-and-variable-products Navigate to your new extension directory and run these commands to install project dependencies and build the JavaScript assets. ```bash cd woo-product-fields npm install # Install dependencies npm run build # Build the javascript ``` -------------------------------- ### Initialize Extension Components (PHP) Source: https://developer.woocommerce.com/docs/extensions/getting-started-extensions/how-to-design-a-simple-extension The `init()` method is responsible for setting up the extension's components after dependencies have been loaded. This example shows how to instantiate classes for cache management, REST API integration, and email management. It also demonstrates registering a WordPress action hook. This method serves as a central point for all initial setup and registration tasks required by the extension. ```php private function init() { // Set up cache management. new My_Extension_Cache(); // Initialize REST API. new My_Extension_REST_API(); // Set up email management. new My_Extension_Email_Manager(); // Register with some-action hook add_action( 'some-action', 'my-extension-function' ); } ``` -------------------------------- ### Get API Index (cURL) Source: https://developer.woocommerce.com/docs/apis/rest-api/v3/api-reference Example of how to fetch the API index using cURL. This is useful for testing API connectivity. ```shell curl https://example.com/wp-json/wc/v3 ``` -------------------------------- ### List all products using cURL Source: https://developer.woocommerce.com/docs/llms-full.txt This example shows how to list all products using cURL. Replace 'consumer_key' and 'consumer_secret' with your actual API credentials. ```shell curl https://example.com/wp-json/wc/v2/products \ -u consumer_key:consumer_secret ``` -------------------------------- ### Retrieve a Customer using Ruby Source: https://developer.woocommerce.com/docs/apis/rest-api/v3/customers Ruby code to get customer details. This example uses the `woocommerce` object, which should be pre-configured. ```ruby woocommerce.get("customers/25").parsed_response ``` -------------------------------- ### Build documentation locally Source: https://developer.woocommerce.com/docs/contribution/contributing-docs Run this command to verify changes, update sitemaps, and check for linting errors or broken links. ```bash npm run build ``` -------------------------------- ### Batch Update Settings using Ruby Source: https://developer.woocommerce.com/docs/llms-full.txt A Ruby example demonstrating batch operations for product settings, including creating new variations, updating existing ones, and deleting products by ID. ```ruby data = { create: [ { regular_price: "10.00", attributes: [ { id: 6, option: "Blue" } ] }, { regular_price: "10.00", attributes: [ { id: 6, option: "White" } ] } ], update: [ { id: 733, regular_price: "10.00" } ], delete: [ 732 ] } woocommerce.post("products/22/settings/general/batch", data).parsed_response ``` -------------------------------- ### Retrieve a Refund using Python Source: https://developer.woocommerce.com/docs/llms-full.txt This Python example utilizes the WooCommerce API client to get details of a specific refund for an order. ```python print(wcapi.get("orders/723/refunds/726").json()) ``` -------------------------------- ### Get API Index (PHP) Source: https://developer.woocommerce.com/docs/apis/rest-api/v3/api-reference Example of fetching the API index using the WooCommerce PHP SDK. This requires the SDK to be set up. ```php get('')); ?> ``` -------------------------------- ### JSONP Support Example Source: https://developer.woocommerce.com/docs/apis/rest-api Use the `?_jsonp` parameter for GET requests to enable JSONP responses, which are wrapped in a specified callback function. ```http GET /wp-json/wc/v3?_jsonp=callback ``` -------------------------------- ### Check if Express Payment Started (JavaScript) Source: https://developer.woocommerce.com/docs/block-development/reference/data-store/payment Example of using the `isExpressPaymentStarted` selector to determine if an express payment method button has been clicked. ```javascript const store = select( paymentStore ); const isExpressPaymentStarted = store.isExpressPaymentStarted(); ``` -------------------------------- ### Create a Simple Product using cURL Source: https://developer.woocommerce.com/docs/llms-full.txt Example of creating a simple product via cURL. Ensure you replace 'consumer_key' and 'consumer_secret' with your actual credentials. ```shell curl -X POST https://example.com/wp-json/wc/v1/products \ -u consumer_key:consumer_secret \ -H "Content-Type: application/json" \ -d '{ "name": "Premium Quality", "type": "simple", "regular_price": "21.99", "description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.", "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.", "categories": [ { "id": 9 }, { "id": 14 } ], "images": [ { "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg", "position": 0 }, { "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg", "position": 1 } ]' ``` -------------------------------- ### List all countries (HTTP) Source: https://developer.woocommerce.com/docs/apis/rest-api/v3/data Use this HTTP GET request to retrieve a list of all countries. Ensure you have the correct base URL for your WooCommerce installation. ```http GET /wp-json/wc/v3/data/countries ``` -------------------------------- ### WooCommerce Create Simple Product Example Source: https://developer.woocommerce.com/docs/best-practices/data-management/crud-objects Demonstrates how to create a new simple product in WooCommerce by instantiating WC_Product_Simple, setting its properties like name, slug, description, and price, and then saving it to the database. ```php $product = new WC_Product_Simple(); $product->set_name( 'My Product' ); $product->set_slug( 'myproduct' ); $product->set_description( 'A new simple product' ); $product->set_regular_price( '9.50' ); $product->save(); $product_id = $product->get_id(); ``` -------------------------------- ### List All Settings Groups (Python) Source: https://developer.woocommerce.com/docs/llms-full.txt This Python example uses the WooCommerce API client to get all settings groups. The JSON response is then printed. ```python print(wcapi.get("settings").json()) ``` -------------------------------- ### Build and Lint Documentation Source: https://developer.woocommerce.com/docs/llms-full.txt Run this command to build the documentation, verify sitemaps and llms-txt files, and check for markdown linting errors and broken links. ```bash npm run build ``` -------------------------------- ### Retrieve a Coupon using Python Source: https://developer.woocommerce.com/docs/llms-full.txt Python example to get coupon information for ID 113. The JSON response is printed to the standard output. ```python print(wcapi.get("coupons/113").json()) ``` -------------------------------- ### Configure Gateway Constructor and Settings Source: https://developer.woocommerce.com/docs/features/payments/payment-gateway-api Demonstrates how to initialize form fields, load settings, and register the admin options update hook within the gateway constructor. ```php $this->init_form_fields(); $this->init_settings(); $this->title = $this->get_option( 'title' ); add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); ``` -------------------------------- ### Retrieve a Product Variation using Python Source: https://developer.woocommerce.com/docs/apis/rest-api/v3/product-variations Python example for retrieving a product variation. It uses the `get` method and prints the JSON response. ```python print(wcapi.get("products/22/variations/732").json()) ``` -------------------------------- ### Retrieve Products Totals with Ruby Source: https://developer.woocommerce.com/docs/apis/rest-api/v3/reports Example of how to get the products totals report using the WooCommerce Ruby SDK. Retrieves the parsed response. ```ruby woocommerce.get("reports/products/totals").parsed_response ``` -------------------------------- ### Retrieve General Settings via Ruby Source: https://developer.woocommerce.com/docs/llms-full.txt Ruby example for fetching general settings. The parsed response is returned. ```ruby woocommerce.get("settings/general").parsed_response ``` -------------------------------- ### Get Products by Total Sales Source: https://developer.woocommerce.com/docs/extensions/core-concepts/wc-get-products Retrieves products based on the total number of times they have been purchased. This example finds products that have never been sold. ```php // Get products that have never been purchased. $products = wc_get_products( array( 'total_sales' => 0 ) ); ``` -------------------------------- ### Navigate to Extension Directory Source: https://developer.woocommerce.com/docs/llms-full.txt After generating the extension, change into its directory to proceed with installation and building. ```sh cd my-extension-name ``` -------------------------------- ### Initiate a Subscription Purchase Source: https://developer.woocommerce.com/docs/woo-marketplace/billing-api-saas This example demonstrates how to initiate a plan purchase for a subscription using the Billing API. It includes the necessary authentication header and a sample request body. ```APIDOC ## POST /subscriptions ### Description Initiates a plan purchase for a subscription. ### Method POST ### Endpoint `https://sandbox.woocommerce.com/wp-json/wccom/billing/1.0/subscriptions` ### Request Body - **name** (string) - Required - The name of the plan. - **price** (number) - Required - The price of the plan. - **billing_period** (string) - Required - The billing period (e.g., 'year', 'month'). - **billing_interval** (integer) - Required - The billing interval. - **return_url** (string) - Required - The URL to redirect the customer to after purchase. ### Request Example ```json { "name": "test plan", "price": 199.99, "billing_period": "year", "billing_interval": 1, "return_url": "https://example.com" } ``` ### Authentication Basic authentication using API key and secret. `Authorization: Basic base64encode(api_key:api_secret)` ``` -------------------------------- ### Create Product with Categories via CLI Source: https://developer.woocommerce.com/docs/wc-cli/cli-faq Shows how to pass complex data structures like product categories as a JSON string. The --user flag is required to authenticate the request. ```bash wp wc product create --name='Product Name' --categories='[ { "id" : 21 } ]' --user=admin ``` -------------------------------- ### List all countries (JavaScript) Source: https://developer.woocommerce.com/docs/apis/rest-api/v3/data Use the WooCommerce JavaScript SDK to get a list of countries. This example shows how to handle successful responses and errors. ```javascript WooCommerce.get( 'data/countries' ) .then( ( response ) => { console.log( response.data ); } ) .catch( ( error ) => { console.log( error.response.data ); } ); ``` -------------------------------- ### List All Webhooks (HTTP Request) Source: https://developer.woocommerce.com/docs/llms-full.txt This is the base HTTP GET request to list all webhooks. No specific setup is required beyond having API credentials. ```http GET /wp-json/wc/v3/webhooks ``` -------------------------------- ### List Product Variations using cURL Source: https://developer.woocommerce.com/docs/llms-full.txt This cURL command demonstrates how to fetch product variations. Ensure you replace `example.com` with your store's URL and provide valid `consumer_key` and `consumer_secret`. ```shell curl https://example.com/wp-json/wc/v2/products/22/variations \ -u consumer_key:consumer_secret ``` -------------------------------- ### List all product reviews using cURL Source: https://developer.woocommerce.com/docs/apis/rest-api/v3/product-reviews Example of fetching all product reviews using the cURL command. Ensure you replace placeholder credentials. ```shell curl https://example.com/wp-json/wc/v3/products/reviews \ -u consumer_key:consumer_secret ``` -------------------------------- ### Retrieve Country Data (Ruby) Source: https://developer.woocommerce.com/docs/llms-full.txt Get country data using the WooCommerce Ruby SDK. This example assumes the 'woocommerce' object is properly initialized. ```ruby woocommerce.get("data/countries/br").parsed_response ``` -------------------------------- ### Retrieve Customer Downloads (cURL) Source: https://developer.woocommerce.com/docs/llms-full.txt Use this cURL command to fetch download permissions for a specific customer. Ensure you replace `example.com` with your store's URL and `26` with the customer's ID. ```shell curl https://example.com/wp-json/wc/v3/customers/26/downloads \ -u consumer_key:consumer_secret ``` -------------------------------- ### List Webhook Deliveries (JavaScript) Source: https://developer.woocommerce.com/docs/llms-full.txt Use the WooCommerce JavaScript SDK to get webhook deliveries. This example shows how to handle both successful responses and errors. ```javascript WooCommerce.get("webhooks/142/deliveries") .then((response) => { console.log(response.data); }) .catch((error) => { console.log(error.response.data); }); ```