我有这个代码来设置 WooCommerce 变量
// Defining User set variables
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions' );
但是如何在
$this->instructions
WooCommerce 模板中获取 thankyou.php
?
我已经尝试使用
$order->instructions
但随后出现错误
注意:指令调用不正确。订单属性应该 不能直接访问。回溯:require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/startup-company/page.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, do_shortcode, preg_replace_callback、do_shortcode_tag、WC_Shortcodes::checkout、 WC_Shortcodes::shortcode_wrapper、WC_Shortcode_Checkout::输出、 WC_Shortcode_Checkout::order_received、wc_get_template、 包括('/plugins/woocommerce/templates/checkout/thankyou.php'), WC_Abstract_Legacy_Order->__get, wc_doing_it_wrong 请参阅 在 WordPress 中调试以获取更多信息。 (此消息已添加 在 3.0 版本中。)
所以我尝试查看
$order
里面的内容,然后我看到一个很长的变量,其中没有我在我自己构建的 WooCommerce 支付网关插件中为 $this->instructions
设置的文本。
您可以使用
WC_Payment_Gateways
类获取所有 Woocommerce 付款方式。然后您可以通过以下方式获取结账可用付款方式并获取相关数据:
$wc_gateways = new WC_Payment_Gateways();
$payment_gateways = $wc_gateways->get_available_payment_gateways();
// Loop through Woocommerce available payment gateways
foreach( $payment_gateways as $gateway_id => $gateway ){
$title = $gateway->get_title();
$description = $gateway->get_description();
$instructions = property_exists( $gateway , 'instructions' ) ? $gateway->instructions : '';
$icon = $gateway->get_icon();
}
已在 Woocommerce 3+ 中测试并运行
或者您可以使用
$gateway_id
语句中的 IF
在 foreach 循环内定位特定支付网关。
您还可以调用自定义支付网关类的实例并在其上使用方法和属性,例如:
$bacs_gateway = new WC_Gateway_BACS(); // Get an instance of the WC_Gateway_BACS object
// Using methods
$title = $bacs_gateway->get_title(); // The title
$description = $bacs_gateway->get_description(); // The description
// OR Using properties
$title = $bacs_gateway->title; // The title
$description = $bacs_gateway->description; // The description
上面的例子很棒!这是在无线电输入或下拉列表中显示付款方式的另一个选项。短代码是
[display_payment_methods]
。将代码添加到子主题functions.php或使用代码片段插件。将短代码放在页面/帖子中以在前端查看。
add_shortcode('display_payment_methods','display_payment_methods');
function display_payment_methods(){
global $woocommerce;
$available_gatewayz = WC()->payment_gateways->get_available_payment_gateways();
if ( $available_gatewayz ) { ?>
<form id="add_payment_method" method="post">
<div id="payment" class="woocommerce-Payment">
<ul class="woocommerce-PaymentMethods payment_methods methods">
<?php
// Chosen Method.
if ( count( $available_gatewayz ) ) {
current( $available_gatewayz )->set_current();
}
foreach ( $available_gatewayz as $gatewayz ) {
?>
<li class="woocommerce-PaymentMethod woocommerce-PaymentMethod--<?php echo esc_attr( $gatewayz->id ); ?> payment_method_<?php echo esc_attr( $gatewayz->id ); ?>">
<input id="payment_method_<?php echo esc_attr( $gatewayz->id ); ?>" type="radio" class="input-radio" name="payment_method" value="<?php echo esc_attr( $gatewayz->id ); ?>" <?php checked( $gatewayz->chosen, true ); ?> />
<label for="payment_method_<?php echo esc_attr( $gatewayz->id ); ?>"><?php echo wp_kses_post( $gatewayz->get_title() ); ?> <?php echo wp_kses_post( $gatewayz->get_icon() ); ?></label>
<?php
if ( $gatewayz->has_fields() || $gatewayz->get_description() ) {
echo '<div class="woocommerce-PaymentBox woocommerce-PaymentBox--' . esc_attr( $gatewayz->id ) . ' payment_box payment_method_' . esc_attr( $gatewayz->id ) . '" style="display: none;">';
$gatewayz->payment_fields();
echo '</div>';
}
?>
</li>
<?php
}}
?>
</ul>
<!-- Enabled Payment Methods Dropdown Select -->
<select name="payment_method" class="select_field">
<option selected="selected" disabled="disabled" value="<?php echo esc_attr( $gatewayz->id ); ?>"><?php echo esc_attr( __( 'Select Payment Method' ) ); ?></option>
<?php
$available_gatewayz = WC()->payment_gateways->get_available_payment_gateways();
// Chosen Method.
if ( count( $available_gatewayz ) ) {
current( $available_gatewayz )->set_current();
}
foreach ( $available_gatewayz as $gatewayz ) {
$option = '<option value="' . esc_attr( $gatewayz->id) . '" ';
$option .= ( esc_attr( $gatewayz->id) == $available_gatewayz ) ? 'selected="selected"' : '';
$option .= '>';
$option .= wp_kses_post( $gatewayz->get_title() ) ;
$option .= '</option>';
echo $option;
}
?>
</select>
<?php
}