问题
I have a billing program and to connect that program to my online store in wordpress i used a plugin but now i've been requested to do the following update :
- Whenever we create a product on woocommerce it creates automatically on the billing program aswell
i asked the billing program support for help to do this and they gave me the following example
$result = $soap->authenticate( $API_KEY ); $APISession = $result[1]; $ref = "002"; $designation = "Produto de teste"; $shortName = "Ptest"; $tax = "23"; $obs = "teste"; $isService = "0"; $hasStocks = "0"; $active = "1"; $shortDesc = "Descricao 123"; $longDesc = "Descricao longa, teste 123."; $price = "100"; $vendorRef = ""; $ean = ""; $product = $soap->insertProduct( $APISession, $ref, $designation, $shortName, $tax, $obs, $isService, $hasStocks, $active, $shortDesc, $longDesc, $price, $vendorRef, $ean);
But thats not what i want since we insert the parameters manually on this example and i want that automatically, i explained this to the support of the billing program but they said they couldn't help me with that, so i'm asking here with the hope that someone can help me on this.
Note : These parameters are the billing program parameters and i want to fill these parameter with what comes from the woocommerce product, i am using a webhook to call the function "insertProduct"
回答1:
The hook woocommerce_new_product
is triggered when a product is created, so it's the hook you need. So you don't need a webhook, as soap is going to do the job.
Now in the code below you will need to define variables $soap
and $API_KEY
.
Also note that in WooCommerce Products, the variables $shortName
, $tax
, $obs
, $isService
, $vendorRef
, $ean
doesn't exits.
Your code should be something like:
// On product creation
add_action( 'woocommerce_new_product', 'woocommerce_create_product_callbback', 10, 4 );
function woocommerce_create_product_callbback( $product_id ) {
// First define variables $soap and $API_KEY
$soap = ; // <== To be defined !
$API_KEY = ; // <== To be defined !
// 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
$soap->insertProduct( $session, $product_id, $name, $shortName, $tax, $obs, $isService, $hasStocks, $active, $short_descr, $description, $price, $vendorRef, $ean);
}
}
Code goes in functions.php file of your active child theme (or active theme). It should works.
来源:https://stackoverflow.com/questions/62573536/automatic-product-insert-via-soap-on-woocommerce-product-creation