我一直在try 让一些代码工作,但似乎没有启动.我觉得我可能在不同版本之间漏掉了什么.

我正在使用WooCommerce插件的基本结帐.文本出现在结帐的底部,而不是按钮,但这可能是一个主题问题,并没有添加CSS.我很确定我已经把 playbook 排好了.

这就是我所拥有的:

In Function.php

add_action( 'woocommerce_after_checkout_form', 'step_controls');
function step_controls() {
    echo '
        <div class="step_controls_container">
            <a class="btn-primary step_back_to_cart" href="'.site_url("/cart", "https").'">Back to Cart</a>
            <a class="btn-primary step_next">Next</a>
            <a class="btn-primary step_prev">Previous</a>
        </div>
    ';
}
//

function cart_steps() {
    wp_enqueue_script('jquery');
    wp_enqueue_script('cartsteps', get_stylesheet_directory_uri() . '/js/cart-steps.js');

} 
add_action( 'wp_enqueue_scripts', 'cart_steps' ); 

在自定义JS文件中

var steps = [
    {
        name: 'Billing & Shipping Details',
        selector: '#customer_details'
    },
    {
        name: 'Order Review & Payment',
        selector: '#order_review'
    }
]
var activeStep;
// Adjust the array length to match zero-base
var stepsLengthAdjusted = steps.length - 1;

// Utility functions
function initSteps() {
    // Set first checkout step
    sessionStorage.setItem('checkout_step', '0');
}
function getCurrentStep() {
    return sessionStorage.getItem('checkout_step');
}
function showCurrentStep() {
    activeStep = getCurrentStep();
    // Loop through the steps and see which is currently active
    for (let i = 0; i < steps.length; i++){
        let stepSelector = steps[i].selector;
        if ( i != activeStep ) {
            // Hide if not the current step
            $(stepSelector).hide();
        } else {
            // Show the correct step div
            $(stepSelector).fadeIn();
            // Show or hide navigation  buttons as appropriate
            $('.step_next, .step_prev').show();
            if ( getCurrentStep() == stepsLengthAdjusted ) {
                // This is the last step, so remove next button
                $('.step_next').hide();
            }
            if ( getCurrentStep() == 0 ) {
                // This is the first step, so remove previous button
                $('.step_prev').hide();
            }
        }
    }
    // Always go to the top of the steps
    $("body").get(0).scrollIntoView();
}
function nextStep() {
    if ( getCurrentStep() < stepsLengthAdjusted ) {
        var nextStep = parseInt(getCurrentStep()) + 1;
        sessionStorage.setItem('checkout_step', nextStep);
        showCurrentStep();
    }
}
function previousStep() {
    if ( getCurrentStep() > 0 ) {
        var previousStep = parseInt(getCurrentStep()) - 1;
        sessionStorage.setItem('checkout_step', previousStep);
        showCurrentStep();
    }
}

// Initialise
if ( getCurrentStep() == null ) {
    initSteps();
}
// Always show the correct step
showCurrentStep();
// Navigation
$('.step_next').click(function() {
    nextStep();
});
$('.step_prev').click(function() {
    previousStep();
});
// Hide a elements not in parent containers!
$('h3#order_review_heading').hide();

// Flush the current step when navigating away from checkout to prevent customer confusion
if ( !$('body').hasClass('woocommerce-checkout') ) {
    initSteps();
}

$('body').on('checkout_error', function(){
    for (let i = 0; i < steps.length; i++){
        let stepSelector = steps[i].selector;
        let fields = stepSelector + ' p';
        $( fields ).each(function() {
            if ( $(this).hasClass('woocommerce-invalid') ) {
                sessionStorage.setItem('checkout_step', i);
                showCurrentStep();
                return false;
            }
        });
    }
});

推荐答案

您只需 for each 元素添加一个css类"Button",并获得购物车URL,最好使用wc_get_cart_url() WooCommerce函数,如下所示:

function step_controls() {
    echo '<div class="step_controls_container">
        <a class="btn-primary button step_back_to_cart" href="'.wc_get_cart_url().'">Back to Cart</a>
        <a class="btn-primary button step_next">Next</a>
        <a class="btn-primary button step_prev">Previous</a>
    </div>';
}

根据您的主题设置,您将获得如下内容:

enter image description here

另外,try 将wp_enqueue_scripts钩子函数替换为:

add_action( 'wp_enqueue_scripts', 'cart_steps_enqueue_script' ); 
function cart_steps_enqueue_script() {
    wp_enqueue_script('cartsteps', get_stylesheet_directory_uri() . '/js/cart-steps.js', array( 'jquery' ), '', true);
} 

Php相关问答推荐

从WooCommerce购物车和 checkout 页面自定义发货标签

在WooCommerce产品中按类型对产品属性术语进行分类以供显示

如何显示每个分类的当前术语的帖子中包含的前20个标签,不包括标签分类

WooCommerce在购买后多次重定向

Codeigniter 3中SMTP.office 365.com的截断问题

PHP文件上载问题-";Move_Uploaded_Files";未按预期工作

PHP:为什么递归需要这么长时间

Laravel 10中的Validator类

根据WooCommerce中的自定义用户元数据值更改用户角色

Mockery在模拟Redis连接时抛出错误:Mockery\Exception\BadMethodCallException

根据WooCommerce的定制订单状态增加产品库存

使用 wp_footer 挂钩添加一些 CSS 不起作用

图像未在 Laravel Ajax Crud 中显示

注册命名空间后如何获取xml node 值?

Laravel的Storage::exists返回false,而file_exists返回true.

来自 GoDaddy 的邮箱在 Gmail 中显示为via

MySQLI bind_param导致的Cannot modify readonly property错误

使用php删除数组中相同数字的所有变体

如何使用属性创建属性预设?

如何将子查询结果添加到学说 2 中主查询的末尾?