Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jQuery hide div class if total equal to 0

Status
Not open for further replies.

bluemask11

Programmer
Feb 25, 2015
12
0
0
RO
Hi!

I'm struggling for a few days to solve a problem. I have a floating cart plugin from MarketPress, it's a free plugin, I want to make it hide when it's empty but it doesn't work.
I've added an if statement where the code is underlined but it doesn't work. Can you help me please?
Cheers,
Nelutu

here is the code
JavaScript:
<!-- Load JS -->
				<script type="text/javascript">
				
				jQuery(document).ready(function () {
					
					// floating cart		
					jQuery(document).ajaxComplete(function(e, xhr, settings) {
						var tot = 0;
						var cart_tot = 0;
                        
						
						<?php if ( $showcartitem == 'yes' ) { ?> 	
							jQuery.each(jQuery('#cart-section td.mp_cart_col_quant'),function(){
								var qty = jQuery(this).html() - 0;
								if(!isNaN(qty)){
									tot += qty;
                              
								}
							});
                                    
							jQuery('.floating-cart span.cart-total-items').html(tot);
                            if (tot <=0){
                          $(".floating-cart").hide();                   
                                    }
						<?php } ?>

						<?php if ( $showcartamount == 'yes' ) { ?> 	

							jQuery.each(jQuery('#cart-section td.mp_cart_col_total'),function(){
								cart_tot = jQuery(this).html();
							});

							jQuery('.floating-cart span.cart-total-amount').html(cart_tot);

						<?php } ?>

					});

				});

				</script>

				<?php

			}

		}

		function floating_cart_contents_in_button( $echo = true , $settings = '' ){

			$showbtntext = esc_attr($settings['fcsettings_general_show_button_text']);
			$buttontext = esc_attr($settings['fcsettings_general_button_text']);
			$showcartitem = esc_attr($settings['fcsettings_general_show_cart_total_item']);
			$showcartamount = esc_attr($settings['fcsettings_general_show_cart_total_amount']);

			$output = '';

			if ( $showcartitem == 'yes' )
				$output .= '<span class="cart-total-items">'.mp_items_count_in_cart().'</span>'.__( ' item(s)' , 'floating-cart' );

			if ( $showcartitem == 'yes' && $showcartamount ==  'yes'  )
				$output .= ' - ';

			if ( $showcartamount ==  'yes' )
				$output .= '<span class="cart-total-amount">'.$this->floating_cart_total_amount_in_cart( false ).'</span>';
			
			if ( $showbtntext == 'yes' )
				$output .= '<span class="view-cart"> - '.__( $buttontext, 'floating-cart' ).'</span>';

			if ($echo) {
			    echo $output;
			} else {
			    return $output;
			}
		}

		function floating_cart_total_amount_in_cart( $echo = true ) {

		  	global $mp, $blog_id;
		  	$blog_id = (is_multisite()) ? $blog_id : 1;
		  	$current_blog_id = $blog_id;

			$global_cart = $mp->get_cart_contents(true);
		  	if (!$mp->global_cart)  //get subset if needed
		  		$selected_cart[$blog_id] = $global_cart[$blog_id];
		  	else
		    	$selected_cart = $global_cart;

		    $totals = array();

		    if ( !empty($selected_cart)) {

			    foreach ($selected_cart as $bid => $cart) {

					if (is_multisite())
				        switch_to_blog($bid);

				    if (!empty($cart)) {
					   	foreach ($cart as $product_id => $variations) {
					        foreach ($variations as $variation => $data) {
					          $totals[] = $data['price'] * $data['quantity'];
					        }
					      }
				    }

			    }
		    }
		    
			if (is_multisite())
		      switch_to_blog($current_blog_id);

		    $total = array_sum($totals);

			if ($echo) {
			    echo $mp->format_currency('', $total);
			} else {
			    return $mp->format_currency('', $total);
			}
		}

		function floating_cart_plugin_updater_init() {

			include_once( $this->plugin_path .'inc/fc-updater.php' );

			if ( ! defined( 'WP_GITHUB_FORCE_UPDATE' ) ) {
				define( 'WP_GITHUB_FORCE_UPDATE', true );
			}

			if ( is_admin() ) {

				$config = array(
					'slug' => plugin_basename( __FILE__ ),
					'proper_folder_name' => 'mp-floating-cart',
					'api_url' => '[URL unfurl="true"]https://api.github.com/repos/nathanonn/mp-floating-cart',[/URL]
					'raw_url' => '[URL unfurl="true"]https://raw.github.com/nathanonn/mp-floating-cart/master',[/URL]
					'github_url' => '[URL unfurl="true"]https://github.com/nathanonn/mp-floating-cart',[/URL]
					'zip_url' => '[URL unfurl="true"]https://github.com/nathanonn/mp-floating-cart/zipball/master',[/URL]
					'sslverify' => true,
					'requires' => '3.5',
					'tested' => '3.5.1',
					'readme' => 'README.md',
					'access_token' => '',
				);

				new WP_GitHub_Updater( $config );

			}
		}

	}

	new MPFloatingCart();

?>
 
I take it you put that below this line
Code:
jQuery('.floating-cart span.cart-total-items').html(tot);
if so and nothing is output to the console I can only assume
Code:
<?php if ( $showcartitem == 'yes' ) { ?>
is evaluating to false?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
@bluemask11

you need to help yourself a bit. or at least help us to help you.

either this is a php question (related to incorrect parsing/setting of $showcartitem) or it is a javascript question.

if it is a php question you need to provide your php code and the mechanisms for setting those variables so we can help.

if it is a javascript question you need to provide either a link to the site (and if you are developing on a localhost then buy a cheap vps such as those at digitalocean and upload the files to that. For 5$ a month it won't break the bank ... OR post the entire RENDERED code here so that we can duplicate the environment and work out what is going wrong.

If $showcartitem is evaluating to false then simply set it to true at least for debugging. then you can narrow down the issue to one line of enquiry or the other.
 
Hi guys,

I solved the problems, I used another plugin. Thank you for your cooperation. When it will be finished i`ll invite you to see it.
Have a nice day.
:);)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top