【问题标题】:Select BACS account to show in thankyou page in WooCommerce选择 BACS 帐户以显示在 WooCommerce 的感谢页面中
【发布时间】:2019-07-29 15:06:33
【问题描述】:

我有一家 WooCommerce 商店,其中一种产品可以由不止一位艺术家进行个性化。每个艺术家都有自己的银行账户来接收他们的付款;所以我需要出现在感谢页面和相应电子邮件中的银行帐户是属于所选艺术家的帐户。为了识别每个银行账户和艺术家,我做了以下操作:

  1. 我使用 slug 为每个产品变体(艺术家)分配了一个 3 个字符的标识符。
  2. 我还使用排序代码字段为支付网关中的每个银行账户分配了相同的 3 字符标识符。

现在,我需要找出哪个银行帐户的排序代码等于所选的变体 slug account_details[x]['sort_code'] = (the variation slug)

有人能指出我正确的方向吗?我需要一个循环来禁用 account_details 中的所有行,除了与变体 slug 匹配的行。


我找到了通过将银行帐户与字符串进行比较来选择银行帐户的方法。为此,我在文件 class-wc-gateway-bacs.php 的第 255 行添加了条件if ( $bacs_account->sort_code != 'ztc' ) { continue; }

    foreach ( $bacs_accounts as $bacs_account ) {
        $bacs_account = (object) $bacs_account; if ( $bacs_account->sort_code != 'ztc' ) { continue; }
        if ( $bacs_account->account_name ) {
            $account_html .= '<h3 class="wc-bacs-bank-details-account-name">' . wp_kses_post( wp_unslash( $bacs_account->account_name ) ) . ':</h3>' . PHP_EOL;

但是,我找不到将变体 slug 与$bacs_account-&gt;sort_code(而不是字符串)进行比较的方法。另外,我认为最好由functions.php文件修改而不是弄乱class-wc-gateway-bacs.php文件。

有人可以帮我做这些吗?

【问题讨论】:

    标签: php wordpress function plugins woocommerce


    【解决方案1】:

    完成!

    找到了从 https://stackoverflow.com/questions/53009224/get-order-item-meta-data-in-an-unprotected-array-in-woocommerce-3 获取变体 slug 的方法。所以我只是添加了:

    foreach ( $order->get_items() as $item ) {$sede = $item->get_meta("pa_sede");}; if ( $bacs_account->sort_code != $sede ) { continue; };

    在文件“class-wc-gateway-bacs.php”的第 255 行之后。我还注释了第 272-275 行以隐藏排序代码字段,因为它对选择帐户很有用,但对用户没有用。

                foreach ( $bacs_accounts as $bacs_account ) {
                    $bacs_account = (object) $bacs_account;
    
        foreach ( $order->get_items() as $item ) {$sede = $item->get_meta("pa_sede");};
        if ( $bacs_account->sort_code != $sede ) { continue; };
    
                    if ( $bacs_account->account_name ) {
                        $account_html .= '<h3 class="wc-bacs-bank-details-account-name">' . wp_kses_post( wp_unslash( $bacs_account->account_name ) ) . ':</h3>' . PHP_EOL;
                    }
                    $account_html .= '<ul class="wc-bacs-bank-details order_details bacs_details">' . PHP_EOL;
                    // BACS account fields shown on the thanks page and in emails.
                    $account_fields = apply_filters(
                        'woocommerce_bacs_account_fields',
                        array(
                            'bank_name'      => array(
                                'label' => __( 'Bank', 'woocommerce' ),
                                'value' => $bacs_account->bank_name,
                            ),
                            'account_number' => array(
                                'label' => __( 'Account number', 'woocommerce' ),
                                'value' => $bacs_account->account_number,
                            ),
    //                      'sort_code'      => array(
    //                          'label' => $sortcode,
    //                          'value' => $bacs_account->sort_code,
    //                      ),
                            'iban'           => array(
                                'label' => __( 'IBAN', 'woocommerce' ),
                                'value' => $bacs_account->iban,
                            ),
                            'bic'            => array(
                                'label' => __( 'BIC', 'woocommerce' ),
                                'value' => $bacs_account->bic,
                            ),
                        ),
                        $order_id
                    );
                    foreach ( $account_fields as $field_key => $field ) {
                        if ( ! empty( $field['value'] ) ) {
                            $account_html .= '<li class="' . esc_attr( $field_key ) . '">' . wp_kses_post( $field['label'] ) . ': <strong>' . wp_kses_post( wptexturize( $field['value'] ) ) . '</strong></li>' . PHP_EOL;
                            $has_details   = true;
                        }
                    }
                    $account_html .= '</ul>';
                }
    

    一切都按预期工作。

    【讨论】:

      【解决方案2】:

      我遇到过类似的情况,但我在结帐页面上启用了网关。网关在每个订单后以循环方式更改。假设第一个订单使用网关 1,第二个使用网关 2,第三个网关 3 再次使用网关 1。我使用 CSS 根据之前订单使用的网关一次仅隐藏/显示一个网关。

      function filter_gateways($gateways){
        global $woocommerce;
        //$WC_Payment_Gateway = wc_get_payment_gateway_by_order( $order );
        $latest_order_id = get_last_order_id(); //Get latest used gateway.
        $order_method = get_post_meta( $latest_order_id, '_payment_method', true );
      //Disable /hide gateways based on last order.
          if ($order_method == 'nmi_gateway_woocommerce_credit_card_cloneC') {?>
          <style type="text/css">
          li.payment_method_nmi_gateway_woocommerce_credit_card_cloneC{display: none !important;}
          li.payment_method_nmi_gateway_woocommerce_credit_card_cloneF{display: none !important;}
          li.payment_method_nmi_gateway_woocommerce_credit_card_cloneK{display: none !important;}
          </style>
          <?php
      
          } elseif ($order_method == 'nmi_gateway_woocommerce_credit_card_cloneE') {?>
          <style type="text/css">
          li.payment_method_nmi_gateway_woocommerce_credit_card_cloneC{display: none !important;}
          li.payment_method_nmi_gateway_woocommerce_credit_card_cloneE{display: none !important;}
          li.payment_method_nmi_gateway_woocommerce_credit_card_cloneK{display: none !important;}
          </style>
      
          <?php
      
      
          } elseif ($order_method == 'nmi_gateway_woocommerce_credit_card_cloneF') {?>
          <style type="text/css">
          li.payment_method_nmi_gateway_woocommerce_credit_card_cloneC{display: none !important;}
          li.payment_method_nmi_gateway_woocommerce_credit_card_cloneE{display: none !important;}
          li.payment_method_nmi_gateway_woocommerce_credit_card_cloneF{display: none !important;}
          </style>
      
      
      
          return $gateways;
      }
      
      add_filter('woocommerce_available_payment_gateways','filter_gateways');
      

      因此,在您的情况下,获取 slug url 并基于此在显示的 BACS 中应该遵循一些样式约定,您可以使用它来禁用其他网关。

      如果你没有得到slug: 1.首先从订单id中获取产品id。 2.然后你可以使用产品ID(Woocommerce: How do I get the product slug from the id?)获取slug

      希望对您有所帮助。

      【讨论】:

      • 亲爱的 Ram,我尝试按照您分享的链接中的建议获取 slug,但我无法获取用于比较的 slug。
      • 你能分享 URL @Roasty 以便我检查一下吗。
      猜你喜欢
      • 1970-01-01
      • 2018-11-05
      • 2021-04-04
      • 2017-01-14
      • 2016-10-04
      • 2013-08-25
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多