Skip to content

Instantly share code, notes, and snippets.

@jay7793
Last active April 3, 2021 00:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jay7793/f7d104bcfc3bb9c95c1b59a6a2dc8ee0 to your computer and use it in GitHub Desktop.
Save jay7793/f7d104bcfc3bb9c95c1b59a6a2dc8ee0 to your computer and use it in GitHub Desktop.
Sample WooCommerce API Product Update (พี่เม่น)
<?php
ติดตั้ง Woocommerce php sdk โดยใช้คำสั่งด้านล่างนี้ (ใช้ Command ในการตัดตั้ง)
// composer require automattic/woocommerce
// Setup:
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
function wc_init() {
$woocommerce = new Client(
'http://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
]
);
}
add_action( 'init', 'wc_init', 10);
// Simple Product Stock Update
function wc_update_product() {
if (empty($woocommerce)) return;
$product_id = 794;
// เลือก Field ที่ต้องการ Update // http://woocommerce.github.io/woocommerce-rest-api-docs/?php#update-a-product
$data = [
'price' => '24.54',
'manage_stock' => true,
'stock_quantity' => 10, // จำนวน Stock สินค้าที่ต้องการอัพเดท
'stock_status' => "instock",
];
print_r($woocommerce->put('products/'.$product_id.'', $data));
wp_die();
}
// Variation Product Stock Update
function wc_update_product_variation() {
if (empty($woocommerce)) return;
$product_id = 794;
$variation_id = 22;
// เลือก Field ที่ต้องการ Update // http://woocommerce.github.io/woocommerce-rest-api-docs/?php#update-a-product-variation
$data = [
'price' => '24.54',
'manage_stock' => true,
'stock_quantity' => 10, // จำนวน Stock สินค้าที่ต้องการอัพเดท
'stock_status' => "instock",
];
print_r($woocommerce->put('products/'.$product_id.'variations/'.$variation_id.'', $data));
wp_die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment