2

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"

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Hugo André
  • 108
  • 1
  • 8

1 Answers1

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • What do you mean? – LoicTheAztec Jun 25 '20 at 14:38
  • I Was wondering if i put this code in a code snippet instead functions.php, it would have the same effect ? – Hugo André Jun 25 '20 at 14:52
  • Yes im still testing that's why i didnt mark it as correct, i am using a webhook so i don't need that "add_action" am i right ? – Hugo André Jun 25 '20 at 15:20
  • @HugoAndré I think that you don't need that webhook, as it's WooCommerce that is going to trigger the action inserting a product to the billing program when a product is created on WooCommerce… So my complete code should do the trick, but you need to set correctly the SOAP part and every variable that the billing program needs through your `insertProduct()` method. May be ask again the support of the billing program, adding the link to this answer… – LoicTheAztec Jun 25 '20 at 15:25
  • I'm trying in a lot of different ways and unfortunately isnt working, maybe because the billing program i would guess – Hugo André Jun 25 '20 at 16:46
  • `Constant expression contains invalid operations in /home/vossa2020shirts/public_html/insertProduct.php on line 5` It gives me this error, do you know what can it be ? Line 5 : `private $soap = new soapclient( $WS_URL );` – Hugo André Jun 29 '20 at 11:02
  • @HugoAndré I am not a SOAP but see [this thread](https://stackoverflow.com/questions/40171546/php-error-fatal-error-constant-expression-contains-invalid-operations) or [this other](https://stackoverflow.com/questions/40827870/constant-expression-contains-invalid-operations) related to *"Constant expression contains invalid operations in…"* error. – LoicTheAztec Jun 29 '20 at 12:18
  • This is giving me this error : `PHP Fatal error: Uncaught Error: Call to undefined function wc_get_product() in /insertProduct.php:31` How should i proceed ? – Hugo André Jul 15 '20 at 11:12