• When I set the order not automatically completed, the credit would be apply to the user who changed the order status.

    How could I do to apply the credit to the user who buy the product, and set the order status from ‘on-hold’ to ‘completed’.

    /**
     * [custom_woocommerce_auto_complete_order description]
     * @param  [type] $order_id [description]
     * @return [type]           [description]
     */
    add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
    function custom_woocommerce_auto_complete_order( $order_id )
    {
      if ( !$order_id )
           return;
    
      $order = new WC_Order( $order_id );
      if ( count( $order->get_items() ) > 0 )
    	{
    		foreach ( $order->get_items() as $item )
    		{
    	   	if(has_term('credit', 'product_cat', $item['product_id']))
    	   		$order->update_status( '<strong>on-hold</strong>' );
    		}
    	}
    }

    https://www.ads-software.com/plugins/user-waller-credit-system/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Justin Greer

    (@justingreerbbi)

    Are you saying that the code you provided does not apply to the person that made the purchase? What you have there, should work just fine.

    Thread Starter reminute2008

    (@reminute2008)

    Sorry..

    I just need the customer to buy the products by the payment, and the customer will get the money into the account after I check the customer pay for it.

    Plugin Author Justin Greer

    (@justingreerbbi)

    OK, I see what you are saying. Currently the credit is applied at the thank you action for WC. I will see about modifying this in the next minor release so that the credit is applied on order status of complete.

    @justin,

    I just made an improvement to your code that addresses this problem (still wonder why Woocommerce makes the admin or shop manager the author of the order when he’s the one who completes it – still wonder why also there is no customer id in the order object :

    in /includes/functions.php :

    /**
     * [add_credits_to_user_account description]
     * @param [type] $order_id [description]
     *
     * @since 1.1 - Now fired on woocommerce_order_status_completed action. This is a    * change to rid infinite reloads
     * of credits after pruchase.
     *
    *Changed by Charles Senard on October, 2015, 17. The customer is credited with the *amount of credits he has bought, vs. before where it was the admin in some cases (when the admin performed an action on the order)
     */
    add_action( 'woocommerce_order_status_completed', 'add_credits_to_user_account' );
    function add_credits_to_user_account ( $order_id )
    {
    write_log( "function add_credits_to_user_account");
    	$order = new WC_Order( $order_id );
        $customer_id = get_post_meta( $order->id , '_customer_user', true );
    
    	if ( count( $order->get_items() ) > 0 )
    	{
    		foreach ( $order->get_items() as $item )
    		{
        	$product_name = $item['name'];
        	$product_id = $item['product_id'];
        	$product_variation_id = $item['variation_id'];
        	$credit_amount = floatval( get_post_meta( $product_id, "_credits_amount", true ) );
        	$current_users_wallet_ballance = floatval( get_user_meta( $customer_id, "_uw_balance", true ) );
        	$la62_success = update_user_meta( $customer_id, "_uw_balance", ( $credit_amount + $current_users_wallet_ballance ) );
    		}
    	}
    }
    
    /**
     * [custom_woocommerce_auto_complete_order description]
     * @param  [type] $order_id [description]
     * @return [type]           [description]
     *
    * Changed by Charles Senard on 2015, October 17th
     * Action deactivated - We may plan to delete it but remains for means of non -
    * regression tests
     */
    //add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
    function custom_woocommerce_auto_complete_order( $order_id )
    {
      if ( !$order_id )
           return;
    
      $order = new WC_Order( $order_id );
      if ( count( $order->get_items() ) > 0 )
    	{
    		foreach ( $order->get_items() as $item )
    		{
    	   	if(has_term('credit', 'product_cat', $item['product_id']))
    	   		$order->update_status( 'completed' );
    		}
    	}
    }

    Hey @justin,

    How about sharing this plugin on Github, so that we can make it better together?

    @ reminute2008,
    if this piece of code solved your problem – I think you should change directly in the plugin – , you might mark this thread as “resolved”. Thanks ??
    Have a good day,

    Charles

    Plugin Author Justin Greer

    (@justingreerbbi)

    @charlyox

    Deal! and Thank you for the code piece. What is your user name on GitHub?
    https://github.com/justingreerbbi/user-waller-credit-system.

    @justin
    great! that’s “charlyox”. see you there ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Not need automatically to complete, the credit problem.’ is closed to new replies.