Update woocommerce shopping cart icon dynamically with ajax - ajax

I Have searched for an answer to this, but not been able to find one.
In my woocommerce shop, i want a basket icon at the top of the site. When a customer puts something in the cart, they want to swap this icon for a different one - this icon ("not empty") will stay as long as the cart is not empty.
What I'm after is a PHP-type if / else call, which says "if the cart is empty, display icon A, else display icon B". If anyone could shine some light on this, I'd be most grateful!
I am able to update the cart text dynamically following this woocommerce tutorial,
http://docs.woothemes.com/document/show-cart-contents-total/
In my Page
<?php global $woocommerce;
if($woocommerce->cart->get_cart_total()=='0') { ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/empty_cart.png" alt="" width="30" height="30">
<?php }else{ ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/full_cart.png" alt="" width="30" height="30">
<?php } ?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >Your Cart : <?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
In Functions.php
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
if($woocommerce->cart->get_cart_total()=='0') { ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/empty_cart.png" alt="" width="30" height="30">
<?php
}else{ ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/full_cart.png" alt="" width="30" height="30">
<?php }
?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >Your Cart : <?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
currently it is showing both the images no matter what is in the cart.
You can see the implementation of the above at
http://fungass.com/testing/shop/uncategorized/abc/

Finally solved it like this,
<?php global $woocommerce; ?>
<a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" ><?php if ($woocommerce->cart->cart_contents_count == 0){
printf( '<img src="%s/images/empty.png" alt="empty" height="25" width="25">', get_stylesheet_directory_uri());
}else{
printf( '<img src="%s/images/full.png" alt="full" height="25" width="25">', get_stylesheet_directory_uri());
}
?> </a>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >
Your Cart : <?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>

I would actually recommend that you use cookies & JavaScript to handle this functionality for the added benefit of allowing caching plugins to serve static pages on non-WooCommerce pages but still have some dynamic pieces on the page (like cart totals or just simply a populated cart icon).
On the PHP side of things, on init, check for cart totals and set a cookie value...
// this should only ever fire on non-cached pages (so it SHOULD fire
// whenever we add to cart / update cart / etc
function update_cart_total_cookie()
{
global $woocommerce;
$cart_total = $woocommerce->cart->cart_contents_count;
setcookie('woocommerce_cart_total', $cart_total, 0, '/');
}
add_action('init', 'update_cart_total_cookie');
On the JavaScript side of things, examine the cookie and then change the icon...
// use the custom woocommerce cookie to determine if the empty cart icon should show in the header or the full cart icon should show
// *NOTE: I'm using the jQuery cookie plugin for convenience https://github.com/carhartl/jquery-cookie
var cartCount = $.cookie("woocommerce_cart_total");
if (typeof(cartCount) !== "undefined" && parseInt(cartCount, 10) > 0) {
$(".full-cart-icon").show();
$(".empty-cart-icon").hide();
// optionally you can even use the cart total count if you want to show "(#)" after the icon
// $(".either-cart-icon span").html("(" + cartCount + ")");
}
else {
$(".full-cart-icon").hide();
$(".empty-cart-icon").show();
}
Hope this helps a bit! Have fun!

Related

Woocommerce ajax message issue

I've got this working well except for one small problem.
I have a custom archive-product.php page which shows each product with a quantity selector...
<ul class="woo-products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 100, 'product_cat' => 'hair-extensions' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<h3><?php the_title(); ?></h3>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<?php echo the_excerpt(); ?>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php // woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php
add_action('woocommerce_after_shop_loop_item','woocommerce_template_single_add_to_cart');
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
do_action( 'woocommerce_after_shop_loop_item' );
?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
If I add a product from there, it goes into my cart no problem. But if I then click on the product to visit it's own page, I see a "this product was added to your cart" message.
Likewise if while on the shop page I add several different products they go into my cart, but if I visit one of those product pages I will see "this product was added to your cart" message several times (once for each product I added to my cart).
Here's an image which might help with my description of the problem: http://i.imgur.com/hou3ZRW.png
In my code above if I un-comment out this line...
woocommerce_template_loop_add_to_cart( $loop->post, $product );
...and remove these lines...
add_action('woocommerce_after_shop_loop_item','woocommerce_template_single_add_to_cart');
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
do_action( 'woocommerce_after_shop_loop_item' );
...it works fine except that I don't have the quantity selector option any more.
Any suggestions as to how to stop the "this product was added to your cart" message appearing when I visit a product page after adding it to the cart would be helpful.
This helped me get around the problem, but including the quantity selectors using a different method the problem went away:
http://christianvarga.com/how-to-add-quantity-to-product-archives-in-woocommerce-and-keep-ajax/
There is a function in WooCommerce called wc_clear_notices That does exactly what you need.
It was introduced in WooCommerce 2.1, you can see the very brief documentation here: http://hookr.io/functions/wc_clear_notices/
See Woocommerce cart notice showing multiple times for a way to implement it.

Hiding Magento's Layered Navigation from Search Engines

I’m hoping someone can help me with this problem I’ve been trying to solve for the past few days. I want to hide Magento’s Layered Navigation from the search engines entirely, but make it available to users. For SEO reasons, I don’t want to settle for NoFollowing all the links, or using noindex follow meta tags, or even blocking it entirely with Robots.txt. The most effective way of handling this would be only showing the layered Navigation to users with Cookies enabled, since Google doesn’t use cookies. The same effect could probably be achieved with JavaScript as well, but I’ve chosen the Cookie method.
So far I’ve managed to implement a crude piece of JS to check if cookies are enabled once the page has loaded (adapted from another thread on this forum). If cookies are enabled, it does nothing and layered nav displays, but if cookies are not enabled, I want to remove the “catalog.leftnav” block. I can’t for the life of me figure out how to do this from my JS script. All I’ve been able to achieve is removing the div element, or setting style.display to none etc., and while all of these techniques remove the links from the frontend, Google can still see them all. Here’s an example of the code I have so far in template/catalog/layer/filter.phtml
<div id="shop-by-filters">
<ol>
<?php foreach ($this->getItems() as $_item): ?>
<li>
<?php if ($_item->getCount() > 0): ?>
<?php echo $_item->getLabel() ?>
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>
<?php if ($this->shouldDisplayProductCount()): ?>
(<?php echo $_item->getCount() ?>)
<?php endif; ?>
</li>
<?php endforeach ?>
</ol>
</div>
<script type="text/javascript">
if (navigator.cookieEnabled) {
return true;
} else if (navigator.cookieEnabled === undefined) {
document.cookie = "testcookie";
if (cookie_present("testcookie"))
return true;
} else {
var elem = document.getElementById('shop-by-filters');
elem.parentNode.removeChild(elem);
}
</script>
Can anyone help me with this, or is there a better way of going about it? Please keep in mind that I am still trying to get my head around Magento, so I might need some instructions if the implementation is complicated.
Thank you.
Brendon
I'm not sure if the Google robot will reliably parse your javascript.
You may be better off hiding the layered nav based on the current session with php.
<?php if (Mage::getSingleton('customer/session')): ?>
...your nav code...
<?php endif ?>
First of all, Javascript will do nothing to stop Google from indexing that content.
Why don't you want 'to settle for NoFollowing all the links'? That is exactly what NoFollow is for. You can also tell Google to not pay attention to the qualifiers/query strings in Webmaster Tools.
If for some reason you really wanted to hide that block from Google, edit the template and string compare $_SERVER['HTTP_USER_AGENT'] against Google's very public list of user agents here http://support.google.com/webmasters/bin/answer.py?hl=en&answer=1061943
EDIT -- string compare
<?php if (stripos($_SERVER['HTTP_USER_AGENT'], 'Googlebot') !== false): ?>
<div id="shop-by-filters">
<ol>
<?php foreach ($this->getItems() as $_item): ?>
<li>
<?php if ($_item->getCount() > 0): ?>
<?php echo $_item->getLabel() ?>
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>
<?php if ($this->shouldDisplayProductCount()): ?>
(<?php echo $_item->getCount() ?>)
<?php endif; ?>
</li>
<?php endforeach ?>
</ol>
</div>
<?php endif; ?>
It is a slick subject. We used this code to hide the layered navigation from Google, but we are not sure is it working...
<div id="filters-no-follow"></div>
<?php
function prepare_for_echo($string) {
$no_br = trim(preg_replace('/\s+/', ' ', $string));
$no_slashes = str_replace('\'', '\\\'', $no_br);
return $no_slashes;
}
?>
<script>
function please_enable_cookies() {
var f = document.getElementById('filters-no-follow');
f.innerHTML = '<div class="no-cookies-error">Enable cookies to choose filters.</div>';
}
function please_load_filters() {
var f = document.getElementById('filters-no-follow');
f.innerHTML = '<?php if ( !empty($filtersHtml) || !empty($stateHtml) ): ?>'
+ '\n<div class="block block-layered-nav">'
+ '\n <div class="block-title">'
+ '\n <strong><span><?php echo prepare_for_echo($this->__('Shop By')); ?></span></strong>'
+ '\n </div>'
+ '\n <div class="block-content">'
+ '\n <?php echo prepare_for_echo($this->getStateHtml()); ?>'
+ '\n <?php if ($this->canShowOptions()): ?>'
+ '\n <p class="block-subtitle"><?php echo prepare_for_echo($this->__('Shopping Options')); ?></p>'
+ '\n <dl id="narrow-by-list">'
+ '\n <?php echo prepare_for_echo($filtersHtml); ?>'
+ '\n </dl>'
+ '\n <?php endif; ?>'
+ '\n </div>'
+ '\n</div>'
+ '\n<?php endif; ?>';
}
function are_cookies_enabled()
{
var cookieEnabled = (navigator.cookieEnabled) ? true : false;
if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
{
document.cookie="testcookie";
cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
}
return (cookieEnabled);
}
if(are_cookies_enabled()) {
please_load_filters();
} else {
please_enable_cookies();
}
</script>

Joomla 1.5. How to add Like / Dislike button on videos / images?

I'm useing Joomla 1.5 AllVideoShare (Version 1.2.4) extension.
QUESTION PART 1
I want to ask you how to make like button on videos thumbs/images? For example if user likes video clip or image he can click like button and It counts how many users like It. It can be simple like button or facebook button if possible. Also maybe possible to make Like / Dislike buttons (If user don't like this video/image he can dislike It)
Example where I want to place:
http://img689.imageshack.us/img689/268/likecp.png
This is my part of code where videos are publishing:
<?php if ($this->params->get('show_'.$header, 1)) : ?>
<h2> <?php echo $this->escape($this->params->get($header)); ?> </h2>
<?php endif; ?>
<div id="avs_gallery2<?php echo $this->escape($this->params->get('pageclass_sfx')); ?>">
<?php
if(!count($videos)) echo JText::_('ITEM_NOT_FOUND');
for ($i=0, $n=count($videos); $i < $n; $i++) {
$clear = '';
if($column >= $this->cols) {
$clear = '<div style="clear:both;"></div>';
$column = 0;
$row++;
}
$column++;
echo $clear;
?>
<span class="name"><?php echo $categories[$i]->name; ?></span>
<div id="testas" class="avs_thumb" style="width:190px;" onclick='javascript:location.href="<?php echo JRoute::_($link.$videos[$i]->slug.$qs); ?>"'>
<img class="arrow" src="<?php echo JURI::root(); ?>components/com_allvideoshare/assets/play.gif" border="0" style="margin-left:80px; margin-top:47px;" />
<img class="image" src="<?php echo $videos[$i]->thumb; ?>" width="190; " height="120;" title="<?php echo JText::_('CLICK_TO_VIEW') . ' : ' . $videos[$i]->title; ?>" border="0" />
<span class="title"><FONT COLOR="#000000"><?php echo $videos[$i]->title; ?></font></span>
<span class="views"><FONT COLOR="#000000"><?php echo JText::_('Peržiūros'); ?> : <strong><?php echo $videos[$i]->views*2+1; ?></strong></font></span>
<div class="avs_category_label"><?php echo JText::_('Kategorija'); ?> : <strong><?php echo $videos[$i]->category; ?></strong></div>
</div>
<?php } ?>
QUESTION PART 2
After I add Like / Dislike buttons will be possible to sort them by "Most popular" (Which video have most likes) or there are so hard work?
Example: http://www.anekdotai.lt/
QUESTION PART 3
In the same place as Like / Dislike button I want to put Facebook share button. I have script which works, but I can't succesfully add It to this place.
FB Share script:
<?php
defined('_JEXEC') or die('Restricted access');
$linkTxt = $params->get( 'linkTxt', '' );
$linkColor = $params->get( 'linkColor', '' );
$linkHColor = $params->get( 'linkHColor', '' );
$theUrl = $params->get( 'theUrl', '1' );
$wholeUrl = "location.href.substring(0,location.href.lastIndexOf('/'))";
$currentUrl = "location.href";
?>
<script>function fbs_click() {u=<?php if($params->get('theUrl', 1))
{
echo $currentUrl;
}
else{
echo $wholeUrl;
} ?>;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}</script><style> html .fb_share_button { display: -moz-inline-block; display:inline-block; padding:1px 1px 1px 20px; height:17px; color:#<?php echo $linkColor ?>; background:url(http://static.ak.facebook.com/images/share/facebook_share_icon.gif?6:26981) no-repeat left; } html .fb_share_button:hover { color:#<?php echo $linkHColor ?>; background:url(http://static.ak.facebook.com/images/share/facebook_share_icon.gif?6:26981) no-repeat left; text-decoration:none; } </style> <a rel="nofollow" href="http://www.facebook.com/share.php?u=<url>" class="fb_share_button" onclick="return fbs_click()" target="_blank" style="text-decoration:none;"><?php echo $linkTxt; ?></a>
If I put It to Video publishing code I get error:
Fatal error: Call to a member function get() on a non-object in ..../juokoera.lt/public_html/components/com_allvideoshare/views/videos/tmpl/default.php on line 26
Thank you for answers.
Try adding the following code which will define the $params variable:
$params = JComponentHelper::getParams( 'com_yourcomponent' );

related products in joomla

How to display only thumbnails of products in related products area, my current code is showing the link too i.e image tag with href title of product name. I just want to show the thumbnails
<?php
foreach($this->product->customfieldsRelatedProducts as $field) {
echo $field->display;
}
?>
I think code will help you.
<?php
$image_dir = JURI::root()."images/";
foreach($this->product->customfieldsRelatedProducts as $field) {
echo $field->display;
?>
<img src="<?php echo $image_dir.$field->image; ?>" alt="<?php echo $field->title ?>" />
<?php
}
?>

Display pricing and add to cart button in related products Virtuemart 2.0

I would like to display the related product pricing and have add to cart button along with each of the related products.
Below is the code snippet from the related products page. The $field does not have any pricing available. How can I show the pricing and "add to cart" button? Thanks in advance
<?php
foreach ($this->product->customfieldsRelatedProducts as $field) {
?><div class="product-field product-field-type-<?php echo $field->field_type ?>">
<span class="product-field-display"><?php echo $field->display ?></span>
<span class="product-field-desc"><?php echo jText::_($field->custom_field_desc) ?></span>
</div>
<?php } ?>
I have found solution here and it works for me:
no need to edit core files
It requires copying the "default_relatedproducts.php", "default_showprices.php" and "default_addtocart.php" to your "template/html/com_virtuemart/productdetails" folder. Then replace all of the code in the "default_relatedproducts.php" with the following code:
<?php
// Check to ensure this file is included in Joomla!
defined ( '_JEXEC' ) or die ( 'Restricted access' );
$model = new VirtueMartModelProduct();
$calculator = calculationHelper::getInstance();
$currency = CurrencyDisplay::getInstance();
?>
<div class="product-related-products">
<h4><?php echo JText::_('COM_VIRTUEMART_RELATED_PRODUCTS'); ?></h4>
<div>
<?php
foreach ($this->product->customfieldsRelatedProducts as $field) {
?>
<div class="product-field">
<?php
$product = $model->getProductSingle($field->custom_value,true);
?>
<h2><?php echo JHTML::link ($product->link, $product->product_name); ?></h2>
<a title="<?php echo $product->product_name ?>" rel="vm-additional-images" href="<?php echo $product->link; ?>">
<?php
echo $this->product->images[0]->displayMediaThumb('class="browseProductImage"', false);
?>
</a>
<div class="short_desc"><?php echo $product->product_s_desc; ?></div>
<?php include 'default_showprices.php'; ?>
<?php include 'default_addtocart.php'; ?>
</div>
<?php } ?>
</div>
</div>
Had a same problem. But I had to show only the price.
So the fastest way is to change sql select statement in customfields.php
Path in Joomla 2.5 for Virtuemart 2.0 administrator/components/com_virtuemart/models/customfields.php
line 548 of
public function getProductCustomsFieldRelatedProducts($product)
change only
$query=
with
'SELECT C.`virtuemart_custom_id` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value`
AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , C.`published` , field.`virtuemart_customfield_id` ,
field.`custom_value`, field.`custom_param`, price.`product_price`, field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
LEFT JOIN `#__virtuemart_product_prices` AS price ON
field.`custom_value` = price.`virtuemart_product_id`
Where field.`virtuemart_product_id` ='.(int)$product->virtuemart_product_id.' and `field_type` = "R"';
After all on line 559 change
$field->custom_price
to
$field->product_price
And finally...
In template view of product description insert the code below to show the prices of related products
<?php echo $field->product_price ?>
The only problem with the below solution is that it doesn't display the correct images for the related products. It's using the main products image and just repeating it.

Resources