Support » Plugin: WooCommerce » Adding a product to an order without Woocommerce functions

  • Resolved Mostafa Shahiri

    (@mostafadeveloper)


    Hi
    I want to create a new order and add a product to it, but for some reasons I don’t want to use Woocommerce classes and methods to do this. I know I can add a new order and its options by using the wp_insert_post and update_post_meta functions, but for adding a product, I don’t know where should I insert the information of product for current order. Please explain with an example.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Mike M. a11n

    (@mikedmoore)

    Automattic Happiness Engineer

    Hi there,

    Order items, such as products, are stored in the wp_woocommerce_order_items table in the database while order metadata is stored in the wp_woocommerce_order_itemmeta table. I’m not sure you’ll be able to update the order items without using WooCommerce functions or direct database inserts into those tables.

    Thread Starter Mostafa Shahiri

    (@mostafadeveloper)

    @mikedmoore Thanks for reply. Well I turn off loading Woocommerce.php in wp-settings.php file because I just want to include Woocommerce manually in my code when I need it. Also, I don’t want to include and use all files of Woocommerce. I just want to create objects of Order and Product classes, but I don’t know which files of Woocommerce should I include. I use following code before creating the objects of Order and Product classes, but it doesn’t solve my problem and I receive the error :”Call to a member function get_product() on null”.

    
            require_once( plugin_dir_path(__DIR__) . '/woocommerce/woocommerce.php' );
             WC_Post_Types::register_taxonomies();
             WC_Post_Types::register_post_types();
             WC_Post_Types::register_post_status();
            do_action( 'woocommerce_after_register_post_type' ); 
            do_action( 'woocommerce_init' );
            global $woocommerce;
    
    

    Please guide me.

    Plugin Support Jesse Pearson (a11n)

    (@jessepearson)

    Automattic Happiness Engineer

    @mostafadeveloper If you were running into issues while using WooCommerce’s built in classes and functions we may be able to assist you, however since you are not we cannot. Without completely duplicating the code, you will not be able to create an order and assign everything correctly. It is recommended that you load the WooCommerce code and use the existing functions and methods.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding a product to an order without Woocommerce functions’ is closed to new replies.