首页 > 解决方案 > 在函数或数组中使用变量

问题描述

我正在尝试获取一个已停止使用三年的插件的 php 文件,而不是作为程序员我没有成功.. 在实践中,一旦从 woocommerce 购物车中提取价格,我想要它用于更新二维码生成器的价格。第一部分有效,第二部分也有效,但我无法获取“金额”参数来读取 $ 总值。感谢谁会帮助我。

// WooCommerce 
    add_action( 'woocommerce_order_details_after_order_table', array( $this, 'order_details_after_order_table' ) );
  }

 
  /**
   * @todo refactor
   *
   * @param WC_Order $order
   */
  public function order_details_after_order_table( $order ) {
    $payment_method = $order->get_payment_method();
    $total          = $order->get_total();
    if ( $payment_method === 'bacs' ) {
      // temporary
      echo $this->shortcode_qrcode();
      echo $total;                                                      // correct return value (626)
    }
    

  }



  public function shortcode_qrcode( $atts = [] ) {
    $options = $this->options;

    // custom param
    $custom = shortcode_atts( array(
      'id'     => $options[ $this->field_key->field_promptpay_id ],
      'amount' => 0                                                     // I want use the $total in this point
    ), $atts );

    $html = sprintf( '<div class="ppy-card"
      data-promptpay-id="%s"
      data-amount="%f"
      data-show-promptpay-logo="%s"
      data-show-promptpay-id="%s"
      data-account-name="%s"
      data-shop-name="%s"
      data-card-style="%s"
      ></div>',
      $custom['id'],
      $custom['amount'],
      $options[ $this->field_key->field_show_promptpay_logo ],
      $options[ $this->field_key->field_show_promptpay_id ],
      $options[ $this->field_key->field_account_name ],
      $options[ $this->field_key->field_shop_name ],
      1
    );

    return $html;
  }

标签: phparraysfunction

解决方案


欢迎!

嗯,这很容易。您只需使用参数 atts 提供对“shortcode_qrcode($atts = [])”的调用,这是一个包含您的“金额”的数组,以便将其替换为您的总计值。

public function order_details_after_order_table( $order ) {
        $payment_method = $order->get_payment_method();
        $total          = $order->get_total();
        if ( $payment_method === 'bacs' ) {
          // temporary
          echo $this->shortcode_qrcode(array("amount"=>$total)); // this would be the change to make
          echo $total; // correct return value (626)
        }
}

推荐阅读