WooCommerce payment页上,订单注册按钮位于页面底部,点击它,您将连接到支付门户.

现在我想在页面中间再放一个类似的按钮,这样客户就不必转到页面底部即可进入支付门户.

WooCommerce payment页的订单按钮的*form-pay.php*文件中有这样一个代码:

<?php echo apply_filters( 'woocommerce_pay_order_button_html', '<button type="submit" class="button alt' . esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ) . '" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' . esc_html( $order_button_text ) . '</button>' ); // @codingStandardsIgnoreLine ?>

但是,通过将此代码复制并粘贴到页面中间,我得到了一个错误.

In summary:

重复100文件中的订单注册按钮.

推荐答案

使用您的代码的问题是没有定义$order_button_text变量...

下面,我用订单支付页面(for checkout page see the addition below)的正确按钮代码创建了一个短码函数:

add_shortcode( 'pay_order_button', 'pay_order_button_shortcode' );
function pay_order_button_shortcode() {
    $order_button_text = apply_filters( 'woocommerce_pay_order_button_text', __( 'Pay for order', 'woocommerce' ) );
    return apply_filters( 'woocommerce_pay_order_button_html', 
        '<button type="submit" class="button alt' . 
        esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . 
        wc_wp_theme_get_element_class_name( 'button' ) : '' ) . 
        '" id="place_order" value="' . esc_attr( $order_button_text ) . 
        '" data-value="' . esc_attr( $order_button_text ) . '">' . 
        esc_html( $order_button_text ) . '</button>' );
}

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.

注意:这应该在Main <form>内部使用

Usage:个个个

你可以把它当做普通的短码[pay_order_button]来使用.

您可以在任何PHP代码中使用它:

echo do_shortcode("[pay_order_button]");

附加1:对于checkout page个按钮:

如果您想在 checkout 页面上添加第二个"下单"按钮,您可以使用以下快捷码函数:

add_shortcode( 'place_order_button', 'checkout_place_order_button_shortcode' );
function checkout_place_order_button_shortcode() {
    $order_button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
    return apply_filters( 'woocommerce_order_button_html', 
        '<button type="submit" class="button alt' . 
        esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . 
        wc_wp_theme_get_element_class_name( 'button' ) : '' ) . 
        '" name="woocommerce_checkout_place_order" id="place_order" value="' . 
        esc_attr( $order_button_text ) . '" data-value="' . 
        esc_attr( $order_button_text ) . '">' . 
        esc_html( $order_button_text ) . '</button>' ); // @codingStandardsIgnoreLine
}

Usage:个个个

你可以把它当做普通的短码[place_order_button]来使用.

您可以在任何PHP代码中使用它:

echo do_shortcode("[place_order_button]");

您可以在类似(displayed here before order review section)的挂钩函数中使用它:

add_action( 'woocommerce_checkout_before_order_review', 'additional_checkout_order_button' );
function additional_checkout_order_button() {
    echo do_shortcode("[place_order_button]");
}

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.

注意:这应该在Main <form>内部使用


附加2:在 checkout 页面或订单支付页面的主<form>内部或outside中使用一个额外的提交按钮,使用一点jQuery排队代码:

add_shortcode( 'submit_button', 'additional_submit_button_shortcode' );
function additional_submit_button_shortcode() {
    if( ! is_checkout() ) return;

    if ( is_wc_endpoint_url('order-pay') ) {
        $order_button_text = esc_html( apply_filters( 'woocommerce_pay_order_button_text', __( 'Pay for order', 'woocommerce' ) ) );
        $form_selector     = 'form#order_review';
    } else {
        $order_button_text = esc_html( apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) ) );
        $form_selector     = 'form.form.checkout';
    }
    // Enqueuing some Javascript
    wc_enqueue_js( "$('body').on( 'click', 'button#submit_order', function(){
         $('$form_selector').submit();
    });");

    return sprintf('<button type="button" class="button alt" id="submit_order">%s</button>', $order_button_text );
}

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.

Usage:个个个

你可以把它当做普通的短码[submit_button]来使用.

您可以在任何PHP代码中使用它:

echo do_shortcode("[submit_button]");

Php相关问答推荐

为什么PHP输出\n而不是实际创建一个新元素?

PHP在将数组子集赋给变量时是否复制数组子集?

如何在Livewire中验证多个 Select 字段

无法使用DOMPDF在PDF中呈现非ANSI字符

闭包绑定$This和垃圾收集器

无法使用php IMAP从Gmail获取收件箱

PHP -流加密启用服务器切断字节

Laravel 10中的Validator类

可以';exec、system和shell_exec PHP函数中的命令字符串上的字符数不得超过8175

Laravel withOnly不限制查询

AWS S3:当对象名称包含 % 时复制对象失败并出现错误 - 无效的复制源编码

在 WooCommerce 管理变量产品中启用其他变体图像

PHP简单地将物品装在盒子里会导致未使用的空间,但还有剩余容量

从 Laravel 机制中排除目录并直接访问该目录

根据WooCommerce中选定的变体重量更改输入数量步值

在PHP WordPress插件中使用变量连接路径时的问题解决方法

同一页面上多个组件的 Livewire 分页问题

我怎样才能在 .htaccess 中有 2 个参数?

使用来自 PHP 表单的数据更新 xml 命名空间

如何在 Laravel 中验证多个输入?