Asked  1 Year ago    Answers:  5   Viewed   8 times

I’m trying to add this code to my WooCommerce setup that adds a shopping cart link wherever I put the PHP and then updates it upon changing items in the cart with AJAX: https://docs.woocommerce.com/document/show-cart-contents-total/

Here are the snippets:

HTML - PHP:

<a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>

In functions.php file:

function woocommerce_header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;

    ob_start();

    ?>
    <a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
    <?php
    $fragments['a.cart-customlocation'] = ob_get_clean();
    return $fragments;
}

But the AJAX is not working. Is the second snippet all I need to add to the functions.php?

It feels like I should call the function and not just define it?

Or do I need to activate AJAX in general in some way to get it to work?

 Answers

2

The filter hook woocommerce_add_to_cart_fragments is missing from your function…

To get it work, it should be:

add_filter( 'woocommerce_add_to_cart_fragments', 'header_add_to_cart_fragment', 30, 1 );
function header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;

    ob_start();

    ?>
    <a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
    <?php
    $fragments['a.cart-customlocation'] = ob_get_clean();

    return $fragments;
}

Code goes in function.php file of your active child theme (or active theme). Untested.

Thursday, April 1, 2021
 
Baba
 
3

There is some mistakes and missing things in your code. For the cart item count in your header the following will solve the problem.

1) The HTML code in your header.php file:

<ul>
    <li>
        <a href="/cart" id="cart_icon"></a>
    </li>
    <li>
        <span class="counter" id="cart-count"><?php
        $cart_count = WC()->cart->get_cart_contents_count();
        echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
        ?></span>
    </li>
    <li id="access"><?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?></li>
</ul>

2) Your related hooked function code to enable cart item count Ajax refreshed:

add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_cart_count', 50, 1 );
function refresh_cart_count( $fragments ){
    ob_start();
    ?>
    <span class="counter" id="cart-count"><?php
    $cart_count = WC()->cart->get_cart_contents_count();
    echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
    ?></span>
    <?php
     $fragments['#cart-count'] = ob_get_clean();

    return $fragments;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


Related: Ajaxify the cart items count in Woocommerce

Thursday, April 1, 2021
 
Gerardo
 
3

The following code will Ajax refresh your shortcode custom product category "72" item count:

// Custom conditional function that checks also for parent product category terms
function has_product_category( $product_id, $category_ids, $taxonomy = 'product_cat' ) {
    $term_ids = array(); // Initializing

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $term_ids[] = $term->parent; // Set the parent product category
            $term_ids[] = $term->term_id;
        } else {
            $term_ids[] = $term->term_id;
        }
    }
    return array_intersect( $category_ids, array_unique($term_ids) );
}

// Custom function that count cart items remaining to a product_category
function get_bottiglie_count( $term_ids ){
    $count = 0; // Initializing

    // Loop through each cart item
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( has_product_category( $cart_item['product_id'], $term_ids ) ) {
            $count += $cart_item['quantity'];
        }
    }

    return $count;
}

// Ajax refressing count
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_bottiglie_count', 50, 1 );
function refresh_bottiglie_count( $fragments ){

    $fragments['#bottiglie-count'] = do_shortcode( "[bottiglie]" );

    return $fragments;
}

// Shortcode that display the count cart items remaining to a product_category
add_shortcode('bottiglie', 'shortcode_bottiglie_count');
function shortcode_bottiglie_count( $atts ) {
    return '<span id="bottiglie-count">' . get_bottiglie_count( array(72) ) . '</span>';
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

Saturday, May 29, 2021
 
Juicy
 
3

You have forget this essential process:

add_action('wp_enqueue_scripts', 'add_my_ajax_scripts'); 

function add_my_ajax_scripts() {
    // Here you register your script located in a subfolder `js` of your active theme
    wp_enqueue_script( 'ajax-script', get_template_directory_uri().'/js/script.js', array('jquery'), '1.0', true );
    // Here you are going to make the bridge between php and js
    wp_localize_script( 'ajax-script', 'cart_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}

Then you will retrieve "ajaxurl" and "cart_ajax" in your javascript file in "url:":

$.ajax({
  url: cart_ajax.ajaxurl,
  ...
})

Your javascript function will not work. Here are some functional examples of what you need to do:

  • WooCommerce - auto update total price when quantity changed?

  • Wordpress passing ajax value to a specific page using Wordpress

  • Using AJAX With PHP on Your WordPress Site Without a Plugin

  • How to use Ajax with your WordPress Plugin or Theme?

Wednesday, August 11, 2021
 
4

Ok so I just realized that I can use woocommerce_add_to_cart_fragments filter 2 times, like so:

add_filter( 'woocommerce_add_to_cart_fragments', function($fragments) {

    ob_start();
    ?>

    <div class="cart-contents">
        <?php echo WC()->cart->get_cart_contents_count(); ?>
    </div>

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

    return $fragments;

} );

add_filter( 'woocommerce_add_to_cart_fragments', function($fragments) {

    ob_start();
    ?>

    <div class="header-quickcart">
        <?php woocommerce_mini_cart(); ?>
    </div>

    <?php $fragments['div.header-quickcart'] = ob_get_clean();

    return $fragments;

} );

First updating quantity and aother refreshing mini-cart view.

Wednesday, September 22, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share