Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter iygen

    (@iygen)

    I recieve problem with hide total.
    In my theme total hide, i change place of it more up (before time be in class “row”). All class row is hide.

    How to update totals:

    // calculate the item price
            if ( !empty($_POST['cart_item_key']) ) {
                $items = WC()->cart->get_cart();
    	    $total = WC()->cart->get_cart_total();
                $cart_item_key = $_POST['cart_item_key'];
    
                if ( array_key_exists($cart_item_key, $items)) {
                    $cart_item = $items[$cart_item_key];
                    $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
                    $price = apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
                    $resp['price'] = $price;
                }
            }
    
    <strong>        $total = apply_filters( 'woocommerce_cart_item_total', WC()->cart->get_cart_total());
            $resp['total'] = $total;</strong>

    after:
    el_qty.closest(‘.cart_item’).find(‘.product-subtotal’).html(resp.price)
    add:
    $(‘.amount’).html(resp.total);
    el_qty.closest(‘.amount’).html(resp.total);

    How to recieve problem when push button update change on error:
    change:
    $(“input[name=’update_cart’]”).val(‘Update’);
    //$(“a.checkout-button.wc-forward”).removeClass(‘disabled’).html(resp.checkout_label);

    Thread Starter iygen

    (@iygen)

    This code recieve problems with script:
    1.Hide totals in some themes;
    2.Problems with errors (sometime can happens when push update cart). More better just hide this button.

    Full code.
    wooajaxcart.php

    <?php
    /*
      Plugin Name: Woo AJAX Cart
      Plugin URI: https://ragob.com/wooajaxcart
      Description: Change the default behavior of WooCommerce Cart page, making AJAX requests when quantity field changes
      Version: 1.2
      Author: Ygen Modify, author - Moises Heberle
      Author URI: https://codecanyon.net/user/moiseh
     */
    
    // templates reference: cart.php , cart-totals.php
    
    add_action('init', 'wac_init');
    function wac_init() {
        // force to make is_cart() returns true, to make right calculations on class-wc-cart.php (WC_Cart::calculate_totals())
        // this define fix a bug that not show Shipping calculator when is WAC ajax request
        if ( !empty($_POST['is_wac_ajax']) && !defined( 'WOOCOMMERCE_CART' ) ) {
            define( 'WOOCOMMERCE_CART', true );
        }
    }
    
    add_action('woocommerce_before_cart_contents', 'wac_cart_demo');
    function wac_cart_demo() {
        if ( defined('IS_DEMO')) {
            wac_demo_msg('Change the product quantity to see the AJAX update made by WooAjaxCart plugin');
        }
    }
    
    add_action('woocommerce_archive_description', 'wac_shop_demo');
    function wac_shop_demo() {
        if ( defined('IS_DEMO')) {
            wac_demo_msg('Add some items to cart and go to the "Cart" page to see this plugin in action');
        }
    }
    
    // on submit AJAX form of the cart
    // after calculate cart form items
    add_action('woocommerce_cart_updated', 'wac_update');
    function wac_update() {
        // is_wac_ajax: flag defined on wooajaxcart.js
    
        if ( !empty($_POST['is_wac_ajax'])) {
            $resp = array();
            $resp['update_label'] = __( 'Update Cart', 'woocommerce' );
            $resp['checkout_label'] = __( 'Proceed to Checkout', 'woocommerce' );
            $resp['price'] = 0;
    
            // render the cart totals (cart-totals.php)
            ob_start();
            do_action( 'woocommerce_after_cart_table' );
            do_action( 'woocommerce_cart_collaterals' );
            do_action( 'woocommerce_after_cart' );
            $resp['html'] = ob_get_clean();
            $resp['price'] = 0;
            $resp['total'] = 0;
    
            // calculate the item price
            if ( !empty($_POST['cart_item_key']) ) {
                $items = WC()->cart->get_cart();
    	    $total = WC()->cart->get_cart_total();
                $cart_item_key = $_POST['cart_item_key'];
    
                if ( array_key_exists($cart_item_key, $items)) {
                    $cart_item = $items[$cart_item_key];
                    $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
                    $price = apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
                    $resp['price'] = $price;
                }
            }
    
            $total = apply_filters( 'woocommerce_cart_item_total', WC()->cart->get_cart_total());
            $resp['total'] = $total;
    
            echo json_encode($resp);
            exit;
        }
    }
    
    // on render cart table page
    add_action('woocommerce_before_cart_table', 'wac_cart_table');
    function wac_cart_table() {
        // add js hacks script
        wp_register_script('wooajaxcart', plugins_url('wooajaxcart2.js', __FILE__));
        wp_enqueue_script('wooajaxcart');
    }
    
    function wac_demo_msg($text) {
        echo sprintf('<div style="background-color: lightgreen; padding: 5px; margin: 5px; border-radius: 3px">* %s</div>', $text);
    }

    wooajaxcart2:

    jQuery(document).ready(function($){
        $('.qty').on('change', function(){
            form = $(this).closest('form');
    
            // emulates button Update cart click
            $("<input type='hidden' name='update_cart' id='update_cart' value='1'>").appendTo(form);
    
            // plugin flag
            $("<input type='hidden' name='is_wac_ajax' id='is_wac_ajax' value='1'>").appendTo(form);
    
            el_qty = $(this);
            matches = $(this).attr('name').match(/cart\[(\w+)\]/);
            cart_item_key = matches[1];
            form.append( $("<input type='hidden' name='cart_item_key' id='cart_item_key'>").val(cart_item_key) );
    
            // get the form data before disable button...
            formData = form.serialize();
    
            $("input[name='update_cart']").val('Обновление…');
            //$("input[name='update_cart']").val('Updating…').prop('disabled', true);
            //$("a.checkout-button.wc-forward").addClass('disabled').html('Updating…');
    
            $.post( form.attr('action'), formData, function(resp) {
                // ajax response
                //$('.cart-collaterals').html(resp.html);
                $('.totalik').html(resp.total);
    
                el_qty.closest('.cart_item').find('.product-subtotal').html(resp.price);
                el_qty.closest('.totalik').find('.amount').html(resp.total);
    
                $('#update_cart').remove();
                $('#is_wac_ajax').remove();
                $('#cart_item_key').remove();
    
                $("input[name='update_cart']").val('Пересчитать корзину');
    
                //$("a.checkout-button.wc-forward").removeClass('disabled').html(resp.checkout_label);
    
                // when changes to 0, remove the product from cart
                if ( el_qty.val() == 0 ) {
                    el_qty.closest('tr').remove();
                }
            },
            'json'
            );
        });
    });

    add in your cart.php theme code to show totals:
    <div><h3>Total: <div style='float:right;' class='totalik'>?<?php wc_cart_totals_order_total_html(); ?></div></h3></div>

    Plugin Author moiseh

    (@moiseh)

    Closing this support because other users not related this issue. Reopen or answer here if needed.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Problem with update (hide total)’ is closed to new replies.