skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search