Last updated on October 18, 2023

WooCommerce inventory sync with ERP

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Sync your inventory seamlessly with ERP in WooCommerce.

The code snippet provided below demonstrates how to sync the inventory of WooCommerce with an ERP (Enterprise Resource Planning) system. This functionality can be useful for e-commerce websites that use WooCommerce as their platform and want to keep their inventory in sync with their ERP system.

/**
 * Sync WooCommerce inventory with ERP.
 */
function wpsnippets_sync_inventory_with_erp() {
    // Connect to the ERP system and fetch the updated inventory data.
    $inventory_data = wpsnippets_get_erp_inventory_data();

    // Loop through each product in the inventory data.
    foreach ( $inventory_data as $product_data ) {
        // Get the product SKU.
        $sku = $product_data['sku'];

        // Get the product ID based on the SKU.
        $product_id = wc_get_product_id_by_sku( $sku );

        // If the product ID is found, update the stock quantity.
        if ( $product_id ) {
            $stock_quantity = $product_data['stock_quantity'];

            // Update the stock quantity for the product.
            update_post_meta( $product_id, '_stock', $stock_quantity );
            update_post_meta( $product_id, '_stock_status', 'instock' );
        }
    }
}

In this code snippet, the wpsnippets_sync_inventory_with_erp function is responsible for syncing the inventory. It first connects to the ERP system and retrieves the updated inventory data using the wpsnippets_get_erp_inventory_data function (which you would need to implement according to your ERP system’s API).

Then, it loops through each product in the inventory data and retrieves the product ID based on the SKU using the wc_get_product_id_by_sku function provided by WooCommerce. If the product ID is found, it updates the stock quantity and status using the update_post_meta function.

You can call this wpsnippets_sync_inventory_with_erp function at regular intervals using a cron job or any other method to keep the inventory in sync between WooCommerce and your ERP system.

Examples

Example 1: Sync WooCommerce inventory with ERP on product update

This use case demonstrates how to sync the inventory of a WooCommerce product with an ERP system when the product is updated.

function wpsnippets_sync_inventory_on_product_update( $product_id ) {
    // Get the product object
    $product = wc_get_product( $product_id );

    // Get the product SKU
    $sku = $product->get_sku();

    // Update the inventory in the ERP system
    wpsnippets_update_erp_inventory( $sku, $product->get_stock_quantity() );
}
add_action( 'woocommerce_update_product', 'wpsnippets_sync_inventory_on_product_update' );

This code example uses the woocommerce_update_product action hook to trigger the inventory sync when a product is updated. It retrieves the product object and its SKU, then calls a custom function wpsnippets_update_erp_inventory() to update the inventory in the ERP system.

Example 2: Sync WooCommerce inventory with ERP on order completion

This use case demonstrates how to sync the inventory of a WooCommerce product with an ERP system when an order is completed.

function wpsnippets_sync_inventory_on_order_completion( $order_id ) {
    // Get the order object
    $order = wc_get_order( $order_id );

    // Loop through the order items
    foreach ( $order->get_items() as $item ) {
        // Get the product SKU
        $sku = $item->get_product()->get_sku();

        // Update the inventory in the ERP system
        wpsnippets_update_erp_inventory( $sku, $item->get_quantity() );
    }
}
add_action( 'woocommerce_order_status_completed', 'wpsnippets_sync_inventory_on_order_completion' );

This code example uses the woocommerce_order_status_completed action hook to trigger the inventory sync when an order is completed. It retrieves the order object and loops through its items. For each item, it retrieves the product SKU and calls the wpsnippets_update_erp_inventory() function to update the inventory in the ERP system.

Example 3: Sync WooCommerce inventory with ERP on scheduled basis

This use case demonstrates how to sync the inventory of all WooCommerce products with an ERP system on a scheduled basis, such as daily or hourly.

function wpsnippets_sync_inventory_on_schedule() {
    // Get all products
    $products = wc_get_products();

    // Loop through the products
    foreach ( $products as $product ) {
        // Get the product SKU
        $sku = $product->get_sku();

        // Update the inventory in the ERP system
        wpsnippets_update_erp_inventory( $sku, $product->get_stock_quantity() );
    }
}
add_action( 'wpsnippets_sync_inventory_schedule', 'wpsnippets_sync_inventory_on_schedule' );

// Schedule the inventory sync
function wpsnippets_schedule_inventory_sync() {
    if ( ! wp_next_scheduled( 'wpsnippets_sync_inventory_schedule' ) ) {
        wp_schedule_event( time(), 'daily', 'wpsnippets_sync_inventory_schedule' );
    }
}
add_action( 'wp', 'wpsnippets_schedule_inventory_sync' );

This code example uses a custom scheduled event wpsnippets_sync_inventory_schedule to trigger the inventory sync on a scheduled basis. It retrieves all products using wc_get_products() and loops through them to update the inventory in the ERP system. The inventory sync is scheduled daily using the wp_schedule_event() function.

Last updated on October 18, 2023. Originally posted on November 23, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *