WordPress现时不验证WooCommerce购物车行动

时间:2017-02-19 09:40:25

标签: wordpress woocommerce cart nonce

我已经构建了一个自定义WooCommerce购物车,您可以使用AJAX更改数量和/或从购物车中删除商品,但它有错误,我认为它与WooCommerce有关以及WordPress nonces的有效性。

问题:

当您的购物车中有产品并且您将产品添加到购物车后至少刷新一次页面

当您第一次访问时,它没有工作,您在购物车中添加产品并尝试编辑产品数量,或尝试删除它

请亲自查看https://staging.noten.nl/noten/ - 请检查您的智能手机。将产品添加到购物车,点击它并更改值(250克以上/ 250克以下/删除产品)

PHP - Nonce创建

/*
**  Theme scripts
*/
function scripts() {

    // Enqueue scripts
    wp_enqueue_script( 'noten-nl/js', Assets\asset_path('scripts/main.js?v=' . VERSION), ['jquery-core', 'wp-util'], null, true );

    // Localize script
    wp_localize_script( 'noten-nl/js', 'shop', array(
        'url'           => admin_url( 'admin-ajax.php' ),
        'cart_more'     => wp_create_nonce( 'cart-more-nonce' ),
        'cart_less'     => wp_create_nonce( 'cart-less-nonce' ),
        'cart_delete'   => wp_create_nonce( 'cart-delete-nonce' )
    ));

}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\scripts', 999 );

Javascript(在下面调用cart_more PHP函数)

/*
**  Edit items in cart
*/
function cartAction(event) {

    // Log
    console.log('cartAction');

    // Variables
    var action = $(event.currentTarget).attr('data-action'),
        product = $('.cart-products-scroll .row.active');

    // Load
    product.children('.cart-row-item-loading').show();

    // AJAX
    wp.ajax.send('cart_' + action, {
        data: {
            nonce:      shop['cart_' + action],
            id:         product.attr('data-product-id'),
            quantity:   product.attr('data-product-quantity'),
            key:        product.attr('data-product-cart-item-key')
        },
        success: function (fragments) {

            // Replace fragments
            $.each(fragments, function (key, value) {
                $(key).replaceWith(value);
            });

        },
        error: function (response) {
            console.log(response);
        }
    });

}

PHP

function cart_more() {

    // Log
    write_log( 'cart_more()' );

    // Variables
    $nonce = isset( $_POST['nonce'] ) ? $_POST['nonce'] : '';
    $product_id = isset( $_POST['id'] ) ? $_POST['id'] : '';
    $product_quantity = isset( $_POST['quantity'] ) ? $_POST['quantity'] : '';

    // Check data
    if ( wp_verify_nonce( $nonce, 'cart-more-nonce' ) && ! empty( $product_id ) && ! empty( $product_quantity ) ) {

        /*
        ** Removed for readability
        */

        // Send success
        wp_send_json_success( $fragments );

    } else {

        // Send error
        wp_send_json_error( ':\'(' );

    }

}
add_action( 'wp_ajax_nopriv_cart_more', __NAMESPACE__ . '\\cart_more' );
add_action( 'wp_ajax_cart_more', __NAMESPACE__ . '\\cart_more' );

问题

为什么在向我的购物车添加内容后,nonce验证才会成功?

1 个答案:

答案 0 :(得分:2)

为了使产品页面可缓存,在创建购物车之前不会创建WooCommerce会话。 1,2

WooCommerce会覆盖其中一个nonce参数,其值会根据是否已创建WooCommerce会话而更改。 3,4

为没有购物车且没有会话的新用户创建随机数时,将使用一组输入计算随机数。在项目添加到购物车后检查现时,使用不同的输入集生成检查值,因为现在存在WooCommerce会话。这会导致生成不同的nonce值,并且nonce检查旧的nonce值将失败。

一种解决方法是在创建nonce之前主动创建WooCommerce会话。请注意,这可能会影响您的网站的缓存方式。

  1. https://github.com/woocommerce/woocommerce/issues/4920#issuecomment-35846419
  2. https://mikejolley.com/2013/12/20/problems-with-cart-sessions-and-woocommerce/
  3. https://developer.wordpress.org/reference/functions/wp_create_nonce/
  4. https://github.com/woocommerce/woocommerce/blob/c16acc6b5104acb0ed082e7df1c63dfd77598459/includes/class-wc-session-handler.php#L224