Я пытался заставить работать какой-то код, но, похоже, он не срабатывает. Думаю, мне что-то не хватает между версиями.
Я использую базовую проверку из плагина WooCommerce. В нижней части оформления заказа вместо кнопок отображается текст, но это может быть проблема с темой, и CSS не добавлен. Я почти уверен, что поставил в очередь сценарий из всего, что смог найти.
Вот что у меня есть:
В 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;
}
});
}
});






Вам просто нужно добавить «кнопку» класса CSS к каждому элементу, а также, чтобы получить 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>';
}
В зависимости от настроек вашей темы вы получите что-то вроде:
Также попробуйте заменить функцию перехвата 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);
}
Эй, спасибо! Я понял, что также не использовал 'jQuery( function( $ ) {' в верхней части моего js-файла. Как только я внес ваши изменения и добавил эту строку, все заработало! У меня есть еще один вопрос, но я думаю, что опубликую новый.