I’ve been asked to create a self made plugin that consists on whenever a product is created on woocommerce it gets this product details and send them to the billing program,
From Automatic product insert via soap on WooCommerce product creation answer code to my previous question, I am using the following code :
add_action( 'woocommerce_new_product', 'woocommerce_create_product_callbback', 10, 4 );
function woocommerce_create_product_callbback( $product_id ) {
$WS_URL =''; //billing program url
$API_KEY = ''; //billing program API Key
$soap = '';
$APISession = '';
// Connect
$result = $soap->authenticate( $API_KEY );
$APISession = $result[1];
if( $APISession ) {
// Get_the WC_Product Object
$product = wc_get_product( $product_id );
// Product data
$status = $product->get_status();
$name = $product->get_name();
$description = $product->get_description();
$short_descr = $product->get_short_description();
$parent_id = $product->get_parent_id();
$menu_order = $product->get_menu_order();
$date_created = $product->get_date_created()->getOffsetTimestamp();
$date_created_gmt = $product->get_date_created()->getTimestamp();
$slug = $product->get_slug();
$author_id = get_post_field ('post_author', $product_id);
// Product meta data (and post terms)
$type = $product->get_type();
$tax_class = $product->get_tax_class();
$stock_status = $product->get_stock_status();
$price = $product->get_price();
$sku = $product->get_sku();
// Special
$active = $product->get_status() ==='publish' ? '1' : '0';
$hasStocks = $product->is_in_stock() ? '1' : '0';
// Undefined (not defined in WooCommerce
$shortName = '';
$tax = '';
$obs = '';
$isService = '0';
$vendorRef = ''; // May be the author ID
$ean = ''; // May be the SKU
// Send data and insert product
$product = $soap->insertProduct( $APISession, $ref, $designation, $shortName, $tax, $obs, $isService, $hasStocks, $active, $shortDesc, $longDesc, $price, $vendorRef, $ean);
}
}
but this catches me the following error :
Uncaught Error: Call to undefined function wc_get_product()
Hope someone can help me
2
Answers
I think you should use another hook
transition_post_status
:it’s work fine I checked. Please check the Documentation. Hope help you.
Once The SOAP issue will be solved, try the following using
woocommerce_admin_process_product_object
dedicated action hook (where the WC_Product Object is already defined as an argument in the function:Code goes in functions.php file of your active child theme (or active theme). It could works once you will solve the SOAP issue.