Picqer set stock

<?php

function s7_picqer_update_stock($product) {
  // Basic data
  $url = 'https://het-mandenhuys.picqer.com/api/v1/'; // see: https://picqer.com/en/api/products
  $warehouse_id = 5624; // warehouse id from picqer data
  $username = 'pNFPyzNOQADVre1WszcKOqEUc8bcMjzg1blw5DufP8LdLlXd';
  $password = '';

  // Post data
  $body = [
    'amount' => $product->get_stock_quantity(),
    'reason' => 'Testreason from website Suite Seven',
  ];
  $body = wp_json_encode( $body );
  $url = $url . 'products/13103536/stock/' . $warehouse_id; //@TODO: Get picqer product id based on picqer productcode (WC _sku)

  // Make the request
  $response = wp_remote_post( $url, array(
    'method'      => 'POST',
    'timeout'     => 45,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking'    => true,
    'headers'     => [
                    'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
                  ],
    'body'        => $body,
    'cookies'     => array()
  ));
  
  if ( is_wp_error( $response ) ) {
    $error_message = $response->get_error_message();
    error_log("Picqer api error: $error_message");
  } else {
    // perhaps create a order note here: wc_trigger_stock_change_notifications()
  }
}

// see hooks in: plugins/woocommerce/includes/wc-stock-functions.php
// add_action('woocommerce_variation_set_stock', __NAMESPACE__ . '\\s7_picqer_update_stock', 10);
// add_action('woocommerce_product_set_stock', __NAMESPACE__ . '\\s7_picqer_update_stock', 10);
// Commented this because it's a WIP
Last Updated:
Contributors: Niek Vlam, Suite Seven