【问题标题】:Filter which BACS account information is sent to users via email过滤哪些 BACS 帐户信息通过电子邮件发送给用户
【发布时间】:2022-01-31 16:54:43
【问题描述】:

成功实施了以下解决方案https://stackoverflow.com/a/55174664/1759546,但是,如何在发送给用户的订单确认电子邮件中应用相同的逻辑?我正在尝试仅发送多个可用帐户中的一个,具体取决于订单中的元数据。

提前致谢

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    好吧,那我就傻了。

    我们只需将第二个参数传递给woocommerce_bacs_accounts,即 order_id。当过滤器在 order_email 中运行时,它会应用相同的规则。

    add_filter( 'woocommerce_bacs_accounts', 'filter_woocommerce_bacs_accounts_callback', 10, 2 );
    function filter_woocommerce_bacs_accounts_callback( $bacs_accounts, $order_id ){
        if ( empty($bacs_accounts) ) {
            return $bacs_accounts; // Exit
        }
    
        if( is_wc_endpoint_url('order-received') ) {
            $endpoint = 'order-received';
            // Get the WC_Order Object
            $order = wc_get_order( get_query_var($endpoint) );
        } elseif( is_wc_endpoint_url('view-order') ) {
            $endpoint = 'view-order';
            // Get the WC_Order Object
            $order = wc_get_order( get_query_var($endpoint) );
        } else if ($order_id){
            // Get the WC_Order Object
            $order = wc_get_order($order_id );
        }
    
        $sort_codes = []; // Initializing variable array
    
        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            $sort_codes[] = $item->get_meta("pa_sede");
        }
    
        if ( empty($sort_codes) ) {
            return $bacs_accounts; // Exit
        }
    
        // Loop through Bacs accounts
        foreach ( $bacs_accounts as $key => $bacs_account ) {
            $bacs_account = (object) $bacs_account;
    
            // Remove the non matching bank accounts
            if ( ! in_array($bacs_account->sort_code, $sort_codes ) ) {
                unset($bacs_accounts[$key]);
            }
        }
        return $bacs_accounts;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 2010-10-19
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      相关资源
      最近更新 更多