• Hello,

    I’m currently trying to modify the processing flow for BACS. I’ve gotten it to work, but it feels like “A very dirty hack”(TM), so I’d like to know if there’s a better way to do it.

    What I want to do:
    * Customer puts order in
    * Order gets moved immediately to processing (instead of on-hold), BUT the content of the bank details is sent in the processing mail (instead of where it’s usually sent in the on-hold mail)

    My way of getting it to work, was by adding this in functions.php of my child theme:

    
    remove_filter( 'woocommerce_payment_gateways', 'core_gateways' );
    add_filter( 'woocommerce_payment_gateways', 'my_core_gateways' );
    
    function my_core_gateways($methods)
    {
      foreach ($methods as &$method){
        if($method == 'WC_Gateway_BACS')
        {
          $method = 'WC_Gateway_BACS_custom';
        }
      }
      return $methods;
    }
    
    /* custom gateway processor for BACS */
    class WC_Gateway_BACS_custom extends WC_Gateway_BACS
    {
    	public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
    
    		if ( ! $sent_to_admin && 'bacs' === $order->payment_method && $order->has_status( 'processing' ) ) {
    			if ( $this->instructions ) {
    				echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
    			}
          
          /* dirty hack to get access to bank_details */
          $reflector = new ReflectionObject($this);
          $method = $reflector->getMethod('bank_details');
          $method->setAccessible(true);
          
          $result = $method->invoke($this, $order->id);
    		}
    
    	}
    
    	public function process_payment( $order_id ) {
    
    		$order = wc_get_order( $order_id );
    
    		// Mark as on-hold (we're awaiting the payment)
    		$order->update_status( 'processing', __( 'Awaiting BACS payment', 'woocommerce' ) );
    
    		// Reduce stock levels
    		$order->reduce_order_stock();
    
    		// Remove cart
    		WC()->cart->empty_cart();
    
    		// Return thankyou redirect
    		return array(
    			'result'    => 'success',
    			'redirect'  => $this->get_return_url( $order )
    		);
    
    	}
    }
    

    I’ve seen another way to immediately move from state on-hold to processing via an action, like
    https://stackoverflow.com/questions/36597663/woocommerce-change-order-status-bacs-processing – but this doesn’t solve my issue of adding a chunk to the mail.

    Any ideas?

    • This topic was modified 9 years, 3 months ago by devilkin.
Viewing 1 replies (of 1 total)
  • Thread Starter devilkin

    (@devilkin)

    Re-iterating over the code I came up with this. Better, since I don’t need to re-define part of the class to get my workflow. Still not 100% ideal.

    
    add_action( 'woocommerce_email_before_order_table', 'add_order_email_instructions', 10, 2 );
    add_action( 'woocommerce_thankyou', 'bacs_order_payment_processing_order_status', 10, 1 );
    
    function bacs_order_payment_processing_order_status( $order_id ) 
    {
      if ( ! $order_id ) {
        return;
      }
    
      $order = new WC_Order( $order_id );
      
      if ('bacs' === $order->payment_method && ('on-hold' == $order->status || 'pending' == $order->status)) {
        $order->update_status('processing');
      } else {
        return;
      }
    }
    
    function add_order_email_instructions( $order, $sent_to_admin ) {
      
      if ( ! $sent_to_admin && 'bacs' === $order->payment_method && $order->has_status( 'processing' ) ) {
        $gw = new WC_Gateway_BACS();
      
        $reflector = new ReflectionObject($gw);
        $method = $reflector->getMethod('bank_details');
        $method->setAccessible(true);
          
        $result = $method->invoke($gw, $order->id);
      }
    }
Viewing 1 replies (of 1 total)

The topic ‘Changing BACS workflow & mailings’ is closed to new replies.