2017-02-19 90 views
2

我已经构建了一个自定义WooCommerce购物车,您可以使用AJAX更改数量和/或从您的购物车中删除物品,但它有问题,我认为它与WooCommerce和有效性有关的WordPress随员。WordPress随机数不验证WooCommerce购物车操作

问题:

它的工作原理时,有您的购物车的产品,你刷新页面至少一次 - 加入产品给您的车后。

它不起作用当您第一次访问时,您会在购物车中添加产品,并尝试编辑产品数量或尝试删除它。

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

PHP - 随机数生成

/* 
** 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'); 

问题

为什么现时验证只增加点东西给我车后成功吗?

+0

你管理来解决这个问题?我现在有同样的一个 – 2017-06-22 14:23:31

回答

1

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

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

当您为没有购物车且没有会话的新用户创建随机数时,随机数是使用一组输入来计算的。当物品添加到购物车后,您检查随机数时,由于WooCommerce会话现在存在,因此生成的检查值具有不同的输入集。这会导致生成一个不同的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