Custom Shopping Cart Image not working anymore

Hi

FAO Ash

I have a custom shopping cart which works pretty well on my site. The code is

add_filter( 'wp_nav_menu_items', 'add_cart_link', 10, 2 );
function add_cart_link( $items, $args ) {
    global $mp;
    $items .= '<li class="menu_cart">'.mp_cart_link( false, false, 'Cart - <span>' . get_cart_value(false) . '</span>' ).'';
    return $items;
}

add_action( 'wp_footer', 'add_cart_value' );
function add_cart_value() {
    ?>
    <script type="text/javascript">
    jQuery(function($){

        // update cart upon ajax complete
        $(document).ajaxComplete( function( event, xhr, settings ) {

            var AjaxData = settings.data;

            if ( ( typeof AjaxData && AjaxData !== undefined ) && AjaxData.indexOf("cart_action=add_item") >= 0 ) {
                $.post( '<?php echo admin_url( 'admin-ajax.php' ); ?>', { action: 'get_cart_value' }, function(response) {
                    $('.menu_cart a span').text(response);
                });
            }

        });

    });
    </script>
    <?php
}

add_action( 'wp_ajax_get_cart_value', 'get_current_cart_value' );
add_action( 'wp_ajax_nopriv_get_cart_value', 'get_current_cart_value' );
function get_cart_value( $echo = true ) {
    global $mp_cart;
    $count = 0;

    if ( empty( $mp_cart ) ) {
        $mp_cart = MP_Cart::get_instance();
    }

    $items = $mp_cart->get_items();

    if ( !empty( $items ) ) {
        foreach ( $items as $id => $qty ) {
            $count += intval( $qty );
        }
    }

    if ( $echo ){
        echo $count . ' item(s) - £' . $mp_cart->product_total();
    } else{
        return $count . ' item(s) - £' . $mp_cart->product_total();
    }
}

function get_current_cart_value() {

    global $mp_cart;
    $count = 0;

    if ( empty( $mp_cart ) ) {
        $mp_cart = MP_Cart::get_instance();
    }

    $items = $mp_cart->get_items();

    if ( !empty( $items ) ) {
        foreach ( $items as $id => $qty ) {
            $count += intval( $qty );
        }
    }

    if (defined('DOING_AJAX') && DOING_AJAX) {
        echo $count . ' item(s) - £' . $mp_cart->product_total();
        exit;
    }  // end - doing_ajax

}

Then in my child style.css I have

.menu_cart a {
background: url(http://treatmeathome.com/wp-content/uploads/2014/09/cart1-e1411662428797.png) no-repeat 0 50%  !important;
padding-left: 38px;

The problem I have is quite minor but the shopping cart image I have in my navigation menu isn't working now I've upgraded to Avada 5.0. I was wondering if someone could help me. Was working fine in old version of Avada. :slight_frown:

Ash helped me originally hence the direct request to Ash. It's not urgent as I understand it's custom but would appreciate any help as hopefully it's an easy fix

Kind Regards
Kieran

  • Adam Czajczyk

    Hello Kieran,

    I hope you're well today and thank you for your question!

    The image that you are referring to should be visible around the area that I'm snowing on the screenshot below, is that correct?

    I examined site's CSS and it looks like this code isn't applied at all:

    .menu_cart a {
    background: url(http://treatmeathome.com/wp-content/uploads/2014/09/cart1-e1411662428797.png) no-repeat 0 50%  !important;
    padding-left: 38px;
    }

    When I added it via browser CSS editor, a nice shopping cart image showed up. You mentioned that this started only after Avada update. I checked the site source and it looks like the Avada parent theme was currently active on your site. Can you please double check that?

    If that's not a case, could you please grant me a support access to your site? This is described here:

    https://premium.wpmudev.org/manuals/wpmu-dev-dashboard-enabling-staff-login/

    Best regards,
    Adam

  • Kieran

    Hi Adam

    Sorry I was in the middle of checking I hadn't activated my Avada parent theme by accident. I then took the chance to upgrade the theme and set it back to Avada Child. I can confirm it's on the child theme now and still not working. Yes its suppose to have a little icon!

    I agree it doesn't look like my style.css is being correctly picked up on. I'm not sure why. I've granted you support access. Let me know if you need ftp access.

    Thanks!
    Kieran

  • Adam Czajczyk

    Hello Kieran!

    Thank you for explanation and for granting access.

    I checked your child theme and I think the main culprit is that part of code in "functions.php" file of the child theme:

    function avada_child_scripts() {
    	if ( ! is_admin() && ! in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) ) {
    		$theme_info = wp_get_theme();
    		wp_enqueue_style( 'avada-child-stylesheet', get_template_directory_uri() . '/style.css', array(), $theme_info->get( 'Version' ) );
    	}
    }
    add_action('wp_enqueue_scripts', 'avada_child_scripts');

    To be precise: it seems like the "if..." condition is preventing child-theme's style.css from being loaded.

    This line

    if ( ! is_admin() && ! in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) )

    includes two checks that are not necessary:

    1. "! is_admin()"

    This is supposed to load style.css only if pages other than dashboard or other parts of admin panel are loaded but the "style.css" file is not used on this page anyway.

    2. "! in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) )"

    That's supposed to load styles if a current page is not a "wp-login.php" and not a "wp-register.php" page. However, there's also no need to use this because styles for these pages are handled separately from "style.css" file.

    Let's then start with a simple test: please replace the code that I mentioned above (in child theme's "functions.php" file) with this one:

    function avada_child_scripts() {
    		$theme_info = wp_get_theme();
    		wp_enqueue_style( 'avada-child-stylesheet', get_template_directory_uri() . '/style.css', array(), $theme_info->get( 'Version' ) );
    }
    add_action('wp_enqueue_scripts', 'avada_child_scripts');

    Let me know of the result please!

    Best regards,
    Adam

  • Adam Czajczyk

    Hello Kieran!

    Thank you for your replay. I just checked your site again and got kind of "enlightenment" :slight_smile: I'm not sure why I didn't notice that before but actually you are not loading the child theme's style anywhere.

    What we did to the child themes code in "functions.php" recently was a bit of a clean up so that's fine. More changes are necessary though.

    This is the current code:

    function avada_child_scripts() {
    		$theme_info = wp_get_theme();
    		wp_enqueue_style( 'avada-child-stylesheet', get_template_directory_uri() . '/style.css', array(), $theme_info->get( 'Version' ) );
    }
    add_action('wp_enqueue_scripts', 'avada_child_scripts');

    This line

    wp_enqueue_style( 'avada-child-stylesheet', get_template_directory_uri() . '/style.css', array(), $theme_info->get( 'Version' ) );

    despite that it says "avada-child-stylesheet" is actually loading Avada parent theme "style.css" file. That's good because it's necessary but since a style.css file of your child-theme includes some style as well it has to be explicitly loaded too.

    That said, please try this updated code instead:

    function avada_child_scripts() {
    		wp_enqueue_style( 'avada-parent-stylesheet', get_template_directory_uri() . '/style.css');
                    wp_enqueue_style( 'avada-child-stylesheet',  get_stylesheet_directory_uri() . '/style.css', array( $parent_style ),  wp_get_theme()->get('Version')  );
    }
    add_action('wp_enqueue_scripts', 'avada_child_scripts');

    This should load both styles (from parent and from child theme) correctly. Let me know if that works please.

    Best regards,
    Adam

  • Adam Czajczyk

    Hello Kieran!

    This is getting weird. This should work. It seems like the line that should include that child theme style just fails and I'm not sure why.

    I would like to access your site directly. I'm sure we can get that to work and I got a feeling that the issue is quite simple in fact but I just can't spot it. Would you mind letting me access your WP install via FTP? You can do this by sending in:

    send in:

    Subject: "Attn: Adam Czajczyk"

    - Mark to my attention, the subject line should contain only: ATTN: Adam Czajczyk
    - Do not include anything else in the subject line, doing so may delay our response due to how email filtering works.
    - Link back to this thread
    - login URL and admin account login credentials (may be a temporary admin account) data
    - Include FTP log-in details (hostname, username & password)
    - Include hosting control panel access details (login address, username & password) - cPanel's usually the control panel used for this, but your provider may use something else; I'll need this for accessing your site's database if necessary and/or use cPanel's File Manager in case FTP didn't work
    - Include any relevant URLs for your site

    Please use our contact form here http://premium.wpmudev.org/contact/

    Select "I have a different question" from the drop-down list. 

    Please be sure to make a full backup of your entire site first!

    Best regards,
    Adam

  • Adam Czajczyk

    Hello Kieran!

    I did make additional change in the code when I accessed the site right before replying to you last time but those changes didn't seem to work. If you did not make any changes since that it may indeed prove that it has just been a caching issue.

    If you are experiencing the same issue with other sites, then yes - please try applying the same solution (though please take the code from your child theme functions.php file of a site assigned to this thread instead of this support forum - I made changed there directly) to these sites and let me know if that works.

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.