WooCommerce - Ajax添加到购物车并更新总计

时间:2022-10-25 10:01:41

I'm working on a website that allows users to add products to cart from the home page. I've followed a few resources online from their website and other SO questions which allows me to add products to the cart via Ajax but the cart total will not update without a page reload.

我正在开发一个网站,允许用户从主页上将产品添加到购物车。我从他们的网站和其他SO问题在线跟踪了一些资源,这些问题允许我通过Ajax将产品添加到购物车,但是如果没有页面重新加载,购物车总数将不会更新。

WooCommerce's documentation is where the cpp_header_add_to_cart_fragment function came from and it doesn't seem to work at all. Originally I was using add_to_cart_fragments but I found out that was deprecated and I should be using woocommerce_add_to_cart_fragments but that change doesn't help either.

WooCommerce的文档是cpp_header_add_to_cart_fragment函数的来源,它似乎根本不起作用。最初我使用的是add_to_cart_fragments,但我发现它已被弃用,我应该使用woocommerce_add_to_cart_fragments,但这种改变也无济于事。

The more I read the code... I'm noticing that the fragments are begin returned from the ajax call so I'm beginning to think I need to replace the html that is showing the cart total with what is returned from the javascript?

我读的代码越多......我注意到片段是从ajax调用开始返回的,所以我开始认为我需要用javascript返回的内容替换显示购物车总数的html?

page_home.php

page_home.php

<!-- Cart link to be updated when products are added -->
<a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>">
    <?php echo WC()->cart->get_cart_total(); ?>
</a>

functions.php

的functions.php

add_action('wp_enqueue_scripts', 'cpp_enqueue_scripts');
function cpp_enqueue_scripts() {
    /* Other enqueue/registers */
    wp_register_script('diy_kits', get_template_directory_uri().'/js/diy_kit.js');
    wp_enqueue_script('diy_kits');
    wp_localize_script(
        'diy_kits',
        'cpp_ajax',
        array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'diy_product_nonce' => wp_create_nonce('diy_product_nonce')
        )
    );
}

add_action('wp_ajax_nopriv_cpp_ajax-submit', 'cpp_ajax_submit');
add_action('wp_ajax_cpp_ajax-submit', 'cpp_ajax_submit');
function cpp_ajax_submit() {
    global $woocommerce;

    $nonce = $_POST['nonce'];
    if(!wp_verify_nonce($nonce, 'diy_product_nonce')) {
        wp_die('Busted!');
    }

    // Add product to cart... this works        
    $product_id = $_POST['product_id'];
    if( $woocommerce->cart->add_to_cart( $product_id ) ) {
        $data = apply_filters('woocommerce_add_to_cart_fragments', array());
        do_action('woocommerce_ajax_added_to_cart', $product_id);
    } else {
        $data = array( 'success' => false, 'product_id' => $product_id );
    }
    $response = json_encode($data);
    header("Content-Type: application/json");
    echo $response; 
    exit;
}

cpp_header_add_to_cart_fragment

cpp_header_add_to_cart_fragment

// CART UPDATE AJAX this doesn't work
add_filter('woocommerce_add_to_cart_fragments', 'cpp_header_add_to_cart_fragment');
function cpp_header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;
    ob_start(); ?>
    <a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>">
        <?php echo WC()->cart->get_cart_total(); ?>
    </a>

    <?php
    $fragments['a.cart-contents'] = ob_get_clean();
    return $fragments;
}

diy_kits.js

diy_kits.js

// setup and other stuff...
links.click(function(e) {
    /* unrelated stuff */
    jQuery.post(
        cpp_ajax.ajaxurl, 
        {
            action      : 'cpp_ajax-submit',
            nonce       : cpp_ajax.diy_product_nonce,
            product_id  : jQuery(this).attr('data-product-id')
        },
        function(response) {
            console.log(response);
        }
    );
});

1 个解决方案

#1


6  

In case someone happens upon this... woocommerce_add_to_cart_fragments was returning the new html string in the $fragments array and since I was calling it in my ajax function that was being turned into a json object. So in my diy_kit.js in the success part of the jquery function, I just had to use that string to update the cart total. I'll paste the edits below:

如果有人发生这种情况...... woocommerce_add_to_cart_fragments在$ fragments数组中返回新的html字符串,因为我在我的ajax函数中调用它,该函数被转换为json对象。所以在我的diy_kit.js中,在jquery函数的成功部分,我只需要使用该字符串来更新购物车总数。我将粘贴以下编辑内容:

page_home.php

page_home.php

<div id="cart_container">
    <a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>">
        <?php echo WC()->cart->get_cart_total(); ?>
    </a> 
</div>

diy_kit.js

diy_kit.js

/*inside jQuery.post() function */
function(response) {
    jQuery('#cart_container').html(response['a.cart-contents']);
}

#1


6  

In case someone happens upon this... woocommerce_add_to_cart_fragments was returning the new html string in the $fragments array and since I was calling it in my ajax function that was being turned into a json object. So in my diy_kit.js in the success part of the jquery function, I just had to use that string to update the cart total. I'll paste the edits below:

如果有人发生这种情况...... woocommerce_add_to_cart_fragments在$ fragments数组中返回新的html字符串,因为我在我的ajax函数中调用它,该函数被转换为json对象。所以在我的diy_kit.js中,在jquery函数的成功部分,我只需要使用该字符串来更新购物车总数。我将粘贴以下编辑内容:

page_home.php

page_home.php

<div id="cart_container">
    <a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>">
        <?php echo WC()->cart->get_cart_total(); ?>
    </a> 
</div>

diy_kit.js

diy_kit.js

/*inside jQuery.post() function */
function(response) {
    jQuery('#cart_container').html(response['a.cart-contents']);
}