nobilik nobilik - 2 months ago 35
jQuery Question

How to update additional attributes in cart on cart update?

I have two additional attributes in my Woocommerce cart. On first load it works well, but I need to refresh the div on cart updated.

How should I call my function for update? I tried this way:

add_filter('woocommerce_update_cart_action_cart_updated', 'myprefix_cart_extra_info', 10);

but it's wrong. Help, please!

My function below:

add_action('woocommerce_cart_collaterals', 'myprefix_cart_extra_info');
function myprefix_cart_extra_info() {
$ves = 0;
$packmax = 0;

// Iterating though each item in cart
$cart_items = WC()->cart->get_cart();
foreach( $cart_items as $cart_item ){
$item_id = $cart_item['product_id'];
$terms = get_the_terms( $item_id , 'pa_ves-g');
foreach($terms as $key => $term)
if(!empty($term->name)) $ves += $term->name * $cart_item["quantity"];
$termspack = get_the_terms( $item_id , 'pa_obem-vlozheniya');
if(!empty($termspack))
foreach($termspack as $termpack)
if(!empty($termpack->name)) $packmax += $termpack->name * $cart_item["quantity"];
}
if ($ves > $packmax)
echo '<p class="overloaded">Слишком много наполнения!</p>';
echo '<div class="cart-extra-info">';
echo '<p class="total-weight">' . __('Общий вес наполнения:', 'woocommerce');
echo ' ' . $ves . ' ' . ' г';
echo '</p>';
echo '<p class="total-pack_max">' . __('Максимальный вес вложения:', 'woocommerce');
echo ' ' . $packmax . ' ' . ' г';
echo '</p>';
echo '</div>';
}


UPDATE
I'm pretty sure that it can be done in cart.js file. Here maybe:

update_cart: function() {
var $form = $( '.shop_table.cart' ).closest( 'form' );

block( $form );
block( $( 'div.cart_totals' ) );

// Make call to actual form post URL.
$.ajax( {
type: $form.attr( 'method' ),
url: $form.attr( 'action' ),
data: $form.serialize(),
dataType: 'html',
success: function( response ) {
update_wc_div( response );
},
complete: function() {
unblock( $form );
unblock( $( 'div.cart_totals' ) );
}
} );
},


But I don't know how. Help please!

Answer

It takes me few days, but I found solution. Maybe it will be useful for somebody. I don't know if my code is good, but it works.

add_action( 'wp_ajax_calculate_additional_info', 'calculate_additional_info' );
add_action( 'wp_ajax_nopriv_calculate_additional_info', 'calculate_additional_info' );

add_action('woocommerce_cart_collaterals', 'myprefix_cart_extra_info');
function myprefix_cart_extra_info() {
    echo '<div class="cart-extra-info">';
    echo calculate_additional_info();
    echo '</div>';
    }

function calculate_additional_info() {
    error_reporting(E_ERROR | E_PARSE);
    $ves = 0;
    $packmax = 0;
    $html = '';

        // Iterating though each item in cart
    $cart_items = WC()->cart->get_cart();
    foreach( $cart_items as $cart_item ){
        $item_id = $cart_item['product_id'];
        $terms = get_the_terms( $item_id , 'pa_ves-g');
            foreach($terms as $key => $term)
            if(!empty($term->name)) $ves += $term->name * $cart_item["quantity"];
        $termspack = get_the_terms( $item_id , 'pa_obem-vlozheniya');
        if(!empty($termspack))
            foreach($termspack as $termpack)
            if(!empty($termpack->name)) $packmax += $termpack->name * $cart_item["quantity"];
        }
        if ($ves > $packmax)
            $html .=  '<p class="overloaded">Слишком много наполнения!</p>';
     $html .= '<span class="total-weight">' . __('Общий вес наполнения:', 'woocommerce') . ' ' . $ves . ' ' . ' г' . '<br />' . '</span>' . '<span class="total-pack_max">' . 
    __('Максимальный вес вложения:', 'woocommerce') . ' ' . $packmax . ' ' . ' г' . '</span>';
    return $html;
    wp_die();
}

add_action( 'wp_footer', 'calculate_additional_info_javascript', 99 ); 

function calculate_additional_info_javascript() { ?>
    <script type="text/javascript" >
jQuery(document.body).on('updated_wc_div', function($) {
    var $form = jQuery( '.shop_table.cart' ).closest( 'form' );
    jQuery.ajax({
        type:     $form.attr( 'method' ),
                url:      $form.attr( 'action' ),
                data:     $form.serialize(),
                dataType: 'html',
        success: function( response ) {
            var $html       = jQuery.parseHTML( response );
            var $new_extra_info = jQuery( '.cart-extra-info', $html );
            jQuery( '.cart-extra-info' ).replaceWith( $new_extra_info); 
        }
    })
})

    </script> <?php
}
Comments