慢ajax添加到购物车可变产品Woocommerce

时间:2014-02-26 10:34:55

标签: jquery sql ajax wordpress woocommerce

我有一个使用ajax将产品添加到购物车的woocommerce安装,但是将产品添加到购物车需要很长时间(7-10秒)。

我开始记录并意识到每次将产品添加到购物车时都会运行数百个SQL查询,这就是所有时间。应该可以将产品添加到购物车而不运行我认为的那么多查询。

我注意到WPML可能是罪魁祸首,但我对SQL并不是很好。

使用AJAX添加产品是否有更优化的方法?这是我今天使用的代码:

// AJAX buy button for variable products
function setAjaxButtons() {
   $('.single_add_to_cart_button').click(function(e) {
      var target = e.target;
      loading(); // loading
      e.preventDefault();
      var dataset = $(e.target).closest('form');
      var product_id = $(e.target).closest('form').find("input[name*='product_id']");
      values = dataset.serialize();
        $.ajax({
          type: 'POST',
          url: window.location.hostname+'/?post_typ=product&add-to-cart='+product_id.val(),
          data: values,
          success: function(response, textStatus, jqXHR){
                loadPopup(target); // function show popup
                updateCartCounter();
            },
        });    
      return false;
   });    

}

以下是每次运行的一些SQL(有数千行,但我无法发布所有这些):

FROM wp_icl_translations
        WHERE element_id='1354' AND element_type='post_product'
     1935 Query SELECT post_type FROM wp_posts WHERE ID=1354
     1935 Query SELECT post_type FROM wp_posts WHERE ID=1354
     1935 Query SELECT post_type FROM wp_posts WHERE ID=1354
     1935 Query SELECT post_type FROM wp_posts WHERE ID=162
     1935 Query SELECT * FROM wp_posts WHERE ID = 1639 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1639)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1442)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1443)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1444)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1445)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1694 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1694)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1726 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1726)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1603 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1603)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1695 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1695)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1442 LIMIT 1
     1935 Query SELECT * FROM wp_posts WHERE ID = 1446 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1446)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1443 LIMIT 1
     1935 Query SELECT * FROM wp_posts WHERE ID = 1444 LIMIT 1
     1935 Query SELECT * FROM wp_posts WHERE ID = 1445 LIMIT 1
     1935 Query SELECT post_type FROM wp_posts WHERE ID=1441
     1935 Query SELECT trid, language_code, source_language_code
        FROM wp_icl_translations
        WHERE element_id='1441' AND element_type='post_product'
     1935 Query SELECT post_type FROM wp_posts WHERE ID=1441
     1935 Query SELECT post_type FROM wp_posts WHERE ID=1441
     1935 Query SELECT post_type FROM wp_posts WHERE ID=1441
     1935 Query SELECT post_type FROM wp_posts WHERE ID=162
     1935 Query SELECT * FROM wp_posts WHERE ID = 1652 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1652)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1530)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1531)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1532)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1533)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1696 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1696)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1716 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1716)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1577 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1577)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1697 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1697)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1530 LIMIT 1
     1935 Query SELECT * FROM wp_posts WHERE ID = 1564 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1564)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1531 LIMIT 1
     1935 Query SELECT * FROM wp_posts WHERE ID = 1532 LIMIT 1
     1935 Query SELECT * FROM wp_posts WHERE ID = 1533 LIMIT 1

140226 11:34:04 1935查询

SELECT post_type FROM wp_posts WHERE ID=1529
     1935 Query SELECT trid, language_code, source_language_code
        FROM wp_icl_translations
        WHERE element_id='1529' AND element_type='post_product'
     1935 Query SELECT post_type FROM wp_posts WHERE ID=1529
     1935 Query SELECT post_type FROM wp_posts WHERE ID=1529
     1935 Query SELECT post_type FROM wp_posts WHERE ID=1529
     1935 Query SELECT post_type FROM wp_posts WHERE ID=162
     1935 Query SELECT * FROM wp_posts WHERE ID = 1638 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1638)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1342)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1343)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1344)
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1345)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1698 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1698)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1727 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1727)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1628 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1628)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1699 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1699)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1342 LIMIT 1
     1935 Query SELECT * FROM wp_posts WHERE ID = 1352 LIMIT 1
     1935 Query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1352)
     1935 Query SELECT * FROM wp_posts WHERE ID = 1343 LIMIT 1
     1935 Query SELECT * FROM wp_posts WHERE ID = 1344 LIMIT 1
     1935 Query SELECT * FROM wp_posts WHERE ID = 1345 LIMIT 1
     1935 Query SELECT post_type FROM wp_posts WHERE ID=1340
     1935 Query SELECT trid, language_code, source_language_code

3 个答案:

答案 0 :(得分:1)

管理好几个小时后找到解决方案,问题是woocommerce在添加到购物车之后为索引页面执行了GET,这花费了所有时间来加载。我注释掉了重定向,现在它的工作速度非常快。

in woocommerce-functions.php

if ( $was_added_to_cart ) {

    $url = apply_filters( 'add_to_cart_redirect', $url );

    // If has custom URL redirect there
    if ( $url ) {
        wp_safe_redirect( $url );
        exit;
    }

    // Redirect to cart option
    elseif ( get_option('woocommerce_cart_redirect_after_add') == 'yes' && $woocommerce->error_count() == 0 ) {
        wp_safe_redirect( $woocommerce->cart->get_cart_url() );
        exit;
    }

    // Redirect to page without querystring args
    elseif ( wp_get_referer() ) {
        // Commented the line below
        //wp_safe_redirect( add_query_arg( 'added-to-cart', implode( ',', $added_to_cart ), remove_query_arg( array( 'add-to-cart', 'quantity', 'product_id' ), wp_get_referer() ) ) );
        exit;
    }

}

答案 1 :(得分:0)

就我而言,我正在使用带有可变产品详细信息的模态弹出窗口,并且需要通过AJAX使“添加到购物车”按钮工作,而不是重定向到产品页面。由于我在扩展WooCommerce的功能方面相当新,我使用上面的daklock代码来推导我的解决方案:

// AJAX Add To Cart button for variable products
    $(document).on('click', '.single_add_to_cart_button', function(e) {

        // stop default action of Add To Cart button
        e.preventDefault();

        // find/set the form in the DOM and the product_id
        var dataset = $(e.target).closest('form');
        var product_id = $(e.target).closest('form').find("input[name*='product_id']");

        // do all your data serialization and make POST the AJAX request
        values = dataset.serialize();
        $.ajax({
          type: 'POST',
          url: '/?post_type=product&add-to-cart='+product_id.val(),
          data: values,
          success: function(response, textStatus, jqXHR){
                console.log('product was added to cart!');
            },
        });    
        return false;
    });

答案 2 :(得分:0)

wp_postmeta的索引可以加速,如下所述:

http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta

wp_icl_translations _和element_idelement_type是否有复合索引? (订购)

为什么在世界上,WP使用2个查询而不是1来执行此操作?

SELECT * FROM wp_posts WHERE ID =       1628 LIMIT 1
SELECT post_id, meta_key, meta_value
     FROM wp_postmeta WHERE post_id IN (1628)