WordPress.org

Forums

WooCommerce - excelling eCommerce
[resolved] woocommerce on order complete insert quantity data into custom database table (15 posts)

  1. lunaticalm
    Member
    Posted 2 years ago #

    I need in woocommerce after successful Paypal payment and product status change to complete, to insert some info data about the order product e.g product quantity to one of my custom database tables. what action/filter will do the job any example?

    http://wordpress.org/extend/plugins/woocommerce/

  2. giuseppeurso
    Member
    Posted 2 years ago #

    I'm in the very same situation.
    Wordpress + Moodle.
    I think we have two options:
    1) add a few lines of php at checkout success that dump all needed data into a flat table.
    2) switch ecommerce system in favour of one with flat orders and products tables.

    Please, let me know if you find out something interesting.
    I'll do the same

  3. royho
    Member
    Posted 2 years ago #

    Yes there are action hooks available for this. See -> http://wcdocs.woothemes.com/codex/extending/hooks/

  4. lunaticalm
    Member
    Posted 2 years ago #

    I try this action but did not worked for me.

    function add_qty($quantity){
    	global $wpdb, $current_user;
    	get_currentuserinfo();
    	$user_id = $current_user->ID;
    	$thetime=$quantity*60;
    	$user_qty_table=$wpdb->prefix . 'user_qty';
    	$wpdb->update( $user_qty_table , array('Time' => $thetime), array('user_id' => $user_id));
    }
    
    function check_quantity()  {
    global $woocommerce;
    	$my_id = 8;
    	foreach( $woocommerce->cart->cart_contents as $id => $cart_item) {
    	 $quantity=$cart_item['quantity'];
    	if ( $cart_item['product_id'] == $my_id ) {
    	add_qty($quantity);
    	}
    }
    }
    
    add_action('woocommerce_payment_complete', 'check_quantity');

    And this too did not worked for me.
    add_action( 'woocommerce_order_status_completed', 'check_quantity' );

    Any idea ?

  5. lunaticalm
    Member
    Posted 2 years ago #

    function check_quantity($order_id) {
    
    $order = new WC_Order( $order_id );
    
    if($order['product_id'] == 8){
    add_qty($order['qty'] );
    }
    }
    add_action( 'woocommerce_order_status_completed', 'check_quantity' );

    Not working too.

  6. royho
    Member
    Posted 2 years ago #

    When you say it doesn't work, do you mean those hooks never gets fired? If so, are you sure the conditions are correct?

  7. lunaticalm
    Member
    Posted 2 years ago #

    When i say doesn't work my custom database table is not updating with the new values, when i make an succesfull order, and I'm not giving any error or warnig etc.. about actions that i try to use about that.

    I'm not sure if that conditions are correct , i found that examples on some forums and I'm trying to implement that.

    So if you know a right action for this then give me an example how i must acomplisg this, or if you think that there is wrong code point me what i must change or fix.

  8. royho
    Member
    Posted 2 years ago #

    Perhaps you should first check if those are even getting fired and at what points. Instead of writing your code to update your custom database, do a log instead so at least you know for sure the event is called. So perhaps write to a file or try var_dump just to know if it is working first.

  9. lunaticalm
    Member
    Posted 2 years ago #

    I think that hooks never get fired.

    i have made change to the function add_qty($quantity) to write quantity result to a file.txt
    the file is empty after the order complete
    i can't see a var_dump at time of execution becouse the order complete code is executed trought paypal api and at the time of execution i am redirected in the paypal payment enviroment and when i back to the site the order is complete

    if i run php file with add_qty($quantity) function inside manualy
    the file.txt write result just fine.

  10. royho
    Member
    Posted 2 years ago #

    Hmmm I can't think of why that hook would not fire unless the conditions are not correct as in does that hook mean any orders that are completed or just orders that are processed and paid...Or maybe that hook only fires when order status changes instead. Then in that case, that is not the right hook to use. I am certain there is a hook available for your needs. Just matter of finding the right one.

  11. lunaticalm
    Member
    Posted 2 years ago #

    I'm using this hook to change the status of the order after payment from procesing to complete for virtual products.
    This hook work fine.

    function virtual_order_payment_complete_order_status( $order_status, $order_id ) {
    
    $order = new WC_Order( $order_id );
    
      if ( ($order_status == 'processing') &&
         ( $order->status == 'on-hold' || $order->status == 'pending' || $order->status == 'failed' ) ) {
        $virtual_order = true;
    
        if ( count( $order->get_items() ) > 0 ) {
          foreach( $order->get_items() as $item ) {
    
            if ( $item['id'] > 0 ) {
              $_product = $order->get_product_from_item( $item );
              $quantity=$item['qty'];
    
              if ( ! $_product->is_virtual() ) {
                $virtual_order = false;
                break;
              }
    
            }
          }
        }
    
        // virtual order, mark as completed
        if ( $virtual_order ) {
          return 'completed';
        }
      }
    
      // non-virtual order, return original status
      return $order_status;
    }
    
    add_action( 'woocommerce_payment_complete_order_status', 'virtual_order_payment_complete_order_status', 10, 2 );
  12. helgatheviking
    Member
    Posted 2 years ago #

    I wonder why woocommerce_payment_complete doesn't work for you. I am going to be doing something similar with data after purchase and I was thinking that was the right hook. @lunaticalm i appreciate you pasting your code... that will come in really handy when I begin development later this week. cheers!

  13. lunaticalm
    Member
    Posted 2 years ago #

    After some more testing this hooks worked for me

    add_action( 'woocommerce_order_status_completed', 'check_quantity' );
    function check_quantity($order_id) {
    
    // order object (optional but handy)
    $order = new WC_Order( $order_id );
    
    if ( count( $order->get_items() ) > 0 ) {
      foreach( $order->get_items() as $item ) {
    
       // for all products if ( $item['id'] > 0 ) {
         if ( $item['id'] == 8 ) {
          $quantity=$item['qty'];
          add_qty($quantity);
    
            }
          }
        }
    
    }
  14. helgatheviking
    Member
    Posted 2 years ago #

    super cool. thanks so much for this. i was struggling with it all morning, but I found that i had hacked some WP core while debugging something else and forgot to undo it. now that is fixed, your code is working great for me.

  15. lunaticalm
    Member
    Posted 2 years ago #

    One more thing

    //For paypal payments  in function
    add_qty($quantity);
    //the
    $user_id = $current_user->ID;
    //will not work becouse you are redirecting
    //so you can get userid with
    add_qty($userid,$quantity);
    //where
    $userid = (int)$order->user_id;

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic