• Resolved Florian

    (@pictibe)


    Hi,

    is use this function and how can i add it to your ajax?

    add_action('woocommerce_cart_collaterals', 'myprefix_cart_extra_info');
    function myprefix_cart_extra_info() {
    	global $woocommerce;
    	echo '<div class="cart-extra-info">';
    	echo '<p class="total-weight"><small>Gewicht Ihrer Bestellung:';
    	echo ' ' . $woocommerce->cart->cart_contents_weight . ' ' . get_option('woocommerce_weight_unit');
    	echo '</small></p>';
    	echo '</div>';
    }

    How can i update the section “.cart-extra-info” with the fragements cart update button?

    Florian

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author taisho

    (@taisho)

    Hello Florian,

    I assume that you have this code in your child theme’s functions.php. You can add the following:

    // Enqueue js file residing in current (parent or child) theme and enable Ajax url in this file through wp_localize_script.
    add_action( 'wp_enqueue_scripts', 'taisho_weight_update' );
    function taisho_weight_update() {
    	wp_enqueue_script( 'taisho-weight-update', get_stylesheet_directory_uri() . '/js/weight_update.js', array('jquery'));
    	wp_localize_script( 'taisho-weight-update', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    }
    
    // Update custom weight on the cart page with Ajax.
    add_action('wp_ajax_ajax_update_weight', 'ajax_update_weight');
    add_action('wp_ajax_nopriv_ajax_update_weight', 'ajax_update_weight');
    function ajax_update_weight() {
    	global $woocommerce;
        echo '<small>Gewicht Ihrer Bestellung: ' . $woocommerce->cart->cart_contents_weight . ' ' . get_option('woocommerce_weight_unit') . '</small>';
        die();
    }

    This code works when you have a folder named “js” within your child theme folder and inside “js” folder, a file “weight_update.js” with this code:

    jQuery(document.body).on('updated_cart_totals', function () {
    	jQuery.ajax(
    	{
    		type: "post",
    		url: my_ajax_object.ajax_url,
    		data: {
    			action: 'ajax_update_weight'
    		},
    		success: function(response){				
    			jQuery('p.total-weight').html(response);	
    		}
    	});	
    });

    Weight will update with a small delay compared to other cart totals, but with a fast website, it’s hardly noticeable.

    • This reply was modified 5 years, 5 months ago by taisho.
    Plugin Author taisho

    (@taisho)

    Looks like I can’t edit the original post anymore – a good practice would be to add this single line of code as the first line of taisho_weight_update function (above wp_enqueue_script line):

    if ( ! is_cart() ) return; // load script only on cart page

    This functionality is only for cart page, so no need to clutter all other pages.

    • This reply was modified 5 years, 5 months ago by taisho.
    • This reply was modified 5 years, 5 months ago by taisho.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add auto ajax to weight calculate’ is closed to new replies.