如何在购物车更新中更新购物车中的其他属性?

时间:2016-09-08 07:00:39

标签: jquery ajax wordpress woocommerce cart

我的Woocommerce购物车中还有两个附加属性。在第一次加载时它运行良好,但我需要更新购物车更新的div。

我该如何调用我的函数进行更新?我试过这种方式:add_filter('woocommerce_update_cart_action_cart_updated', 'myprefix_cart_extra_info', 10); 但这是错的。求救!

我的功能如下:

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>';
}

更新 我很确定它可以在cart.js文件中完成。在这里可能:

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' ) );
                }
            } );
        },

但我不知道怎么做。求救!

1 个答案:

答案 0 :(得分:1)

我需要几天时间,但我找到了解决方案。也许它会对某些人有用。我不知道我的代码是否合适,但它确实有用。

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
}