• Resolved manojr

    (@manojr)


    I am trying to show woocommece cart total in the navbar of my theme. This must show updated cart total via AJAX. In my store I am using WCML plugin as well. Problem is my Ajax action always return WCML converted prices for secondary languages instead of manually entered product price total in cart.

    How can I get WCML manually entered product price totals for secondary languages?

    This is my JS Ajax script

    //Call for cart date via Ajax
    function cartAjaxCall() {
      var currency = localeCurrency(currencyBaseOnLocale);
      var currencyData = {
        currentLang: currency[0],
        currency: currency[3],
        action: "wcml_switch_currency",
        "wc-ajax": "get_cart_totals",
      };
      var origin = window.location.origin;
      $(".cart-ajax").load(
        origin + "/wp-content/themes/peaceyard/partials/cart-ajax.php",
        currencyData,
        function () {
          var cartContentText = $(".cart-ajax").first().text();
        }
      );
    }

    And this is the cart-ajax.php which is sending cart total

     <?php
    
            //Check for an AJAX call
    
            check_is_ajax(__FILE__);
    
            function check_is_ajax($script)
            {
                $path = preg_replace('/wp-content.*$/', '', __DIR__);
                include($path . 'wp-load.php');
    
                $isAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) and
                    strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
                if (!$isAjax) {
                    trigger_error('Access denied - not an AJAX request...' . ' (' . $script . ')', E_USER_ERROR);
                } else {
                    add_filter('wcml_multi_currency_ajax_actions', 'add_action_to_multi_currency_ajax', 10, 1);
                    add_action('headerCart', 'get_cart_header_contents');
    
                    function add_action_to_multi_currency_ajax($ajax_actions)
                    {
                        $ajax_actions[] = "get_cart_header_contents"; // Add a AJAX action to the array.
                        return $ajax_actions;
                    }
                    do_action('headerCart');
                }
            }
    
            function get_cart_header_contents()
            {
    
                if (isset($_POST['currentLang'])) {
                    do_action('wpml_switch_language', $_POST["currentLang"]); // switch the content language
                }
    
                if (WC()->cart->get_cart_contents_count() == 0) {
                    echo "";
                } else {
    
                    echo sprintf(
                        _n('%d item', '%d items', WC()->cart->get_cart_contents_count(), 'woothemes'),
                        WC()->cart->get_cart_contents_count()
                    ); ?> -<?php
                    echo wp_kses_post(apply_filters('wcml_formatted_price', WC()->cart->cart_contents_total, $_POST["currency"]));
                }
            }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How can I get WCML manually entered product price cart totals via AJAX’ is closed to new replies.