php - 在函数或数组中使用变量
问题描述
我正在尝试获取一个已停止使用三年的插件的 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;
}
解决方案
欢迎!
嗯,这很容易。您只需使用参数 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)
}
}
推荐阅读
- django - 将 PositiveIntegerField 迁移到 FloatField
- blockchain - 比特币交易验证如何与 scriptSig 和 sciptPubKey 一起工作?
- azure - Azure Web App 和 .NetCore WebAPI - 缺少 web.config
- python - Python 等效于将 POST 有效负载包装在单引号中
- sql - 在 SQL Server 中分组,然后合并行
- reactjs - Chrome 没有详细说明为什么 React Elements 需要很长时间才能在 User Timing 中呈现
- c++ - 无法理解它是如何工作的?
- c++ - 如何在 C++ 中将 base64 编码图像加载为 SDL 纹理?
- symfony - omines/datatables 捆绑包不会搜索日期时间列
- ios - 在 swift 4 中显示 Firebase 通知弹出窗口