4

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

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

2 Answers2

4

IMPORTANT:
First you need to solve the "Fatal Error Call to a member function authenticate()" as this is related to your SOAP initialization, because $soap = ''; requires to be an object and not an empty string. That's why you get this error on the answer from Dmitry.

This problem can't not be handled by anyone as you doesn't give any related details or documentation (and this has nothing to do with a WooCommerce tagged question as It's related to PHP and SOAP first).

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:

add_action( 'woocommerce_admin_process_product_object', 'wc_admin_process_product_object_action_callbback', 900, 1 );
function wc_admin_process_product_object_action_callbback( $product ) {
    // On product creation and product has not been processed yet
    if ( ! $product->get_meta('_soap_prodcessed' ) {
        $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 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
            $result = $soap->insertProduct( $APISession, $ref, $designation, $shortName, $tax, $obs, $isService, $hasStocks, $active, $shortDesc, $longDesc, $price, $vendorRef, $ean);
            
            // Add custom meta to flag the product as processed via soap (avoid multiple insertions)
            $product->update_meta_data( '_soap_prodcessed', '1' );
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme). It could works once you will solve the SOAP issue.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for explaining the error ! Can i paste this code in my self plugin file ? – Hugo André Jul 27 '20 at 15:40
  • I Did edit my code to solve those issues but now it doesnt catch any error, is it because this if ? `if ( ! $product->get_meta('_soap_prodcessed' ) {` it isn't creating nothing on the billing program, maybe it stops there – Hugo André Jul 27 '20 at 15:52
  • @HugoAndré Maybe see: https://stackoverflow.com/questions/3934639/soap-authentication-with-php/3935816#3935816 – LoicTheAztec Jul 27 '20 at 16:04
  • I did change the `$soap= ' ';` already with the help of the billing program support, ill check that post right away – Hugo André Jul 27 '20 at 16:16
  • The soap is filled with what billing program gave me, so i guess it is probably correct – Hugo André Jul 27 '20 at 16:58
  • It doesn't give me any feedback, the product gets created on the woocommerce but it doesnt duplicate it on the billing program – Hugo André Jul 27 '20 at 17:02
  • It does happen the same as this solution – Hugo André Jul 27 '20 at 17:10
  • @HugoAndré So the problem comes from SOAP insertProduct() method… I think that this should be like an indexed array of parameters instead. Nobody will be able to help you on that… I have tested my code part and it works nice: I am able to register the data in an indexed array custom field… So the problem comes from SOAP insertProduct() method and the way the data stands inside. Ask again the billing program. – LoicTheAztec Jul 27 '20 at 17:10
0

I think you should use another hook transition_post_status:

function woocommerce_create_product_callbback( $new_status, $old_status, $post ) {

    if (
        $old_status != 'publish' &&
        $new_status == 'publish' &&
        !empty( $post->ID ) &&
        in_array( $post->post_type, array( 'product') ) )
    {
        $product = wc_get_product( $post->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 ) {

            // 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);
        }
    }
    add_action( 'transition_post_status', 'woocommerce_create_product_callbback', 10, 3 );

it's work fine I checked. Please check the Documentation. Hope help you.

Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42