Woocommerce BACS在处理电子邮件时添加银行帐号

时间:2016-10-18 15:21:02

标签: php wordpress woocommerce

我添加了扩展类WC_Gateway_BACS的覆盖函数。该功能将更新订单的状态,从暂停到处理。问题是电子邮件现在缺少银行详细信息。在我在电子邮件中包含银行帐号之前,但在自定义之后,电子邮件不包含它,我认为这是因为订单状态现在正在处理中。

这里有没有人做同样的事情并提出解决方案?我在这里包含了一些暂停和处理电子邮件的图像。我想将帐号添加到processing-email

class WC_Gateway_BACS_custom extends WC_Gateway_BACS {
    /**
     * Process the payment and return the result
     *
     * @access public
     * @param int $order_id
     * @return array
     */
    function process_payment( $order_id ) {
        global $woocommerce;
        $order = new WC_Order( $order_id );
        // Mark as processing (or anything you want)
        $order->update_status('processing', __( 'Awaiting BACS payment', 'woocommerce' ));

        // Reduce stock levels
        $order->reduce_order_stock();

        // Remove cart
        $woocommerce->cart->empty_cart();

        // Return thankyou redirect
        return array(
            'result'  => 'success',
            'redirect'  => $this->get_return_url( $order )
        );
    }

    /**
     * Add content to the WC emails.
     *
     * @param WC_Order $order
     * @param bool $sent_to_admin
     * @param bool $plain_text
     */
    // public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {

    //  if ( ! $sent_to_admin && 'bacs' === $order->payment_method && ($order->has_status( 'on-hold' ) || $order->has_status( 'processing' )) ) {
    //      if ( $this->instructions ) {
    //          echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
    //      }
    //      $this->bank_details( $order->id );
    //  }

    // }
}

on-hold email processing email

1 个答案:

答案 0 :(得分:0)

我今天遇到了同样的问题。我想出了两个可能的解决方案:

扩展课程,如上所述:

/* override gateway for BACS */
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
{

    /**
     * Add content to the WC emails.
     *
     * @param WC_Order $order
     * @param bool $sent_to_admin
     * @param bool $plain_text
     */
    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);
        }
    }

    /**
     * Process the payment and return the result.
     *
     * @param int $order_id
     * @return array
     */
    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 )
        );
    }
}

或者,在我看来,更清洁,添加两个动作

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);
  }
}

从长远来看,第二种解决方案的维护要求最低。