Skip to content

Instantly share code, notes, and snippets.

@marcosfreitas
Created February 18, 2014 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcosfreitas/9071657 to your computer and use it in GitHub Desktop.
Save marcosfreitas/9071657 to your computer and use it in GitHub Desktop.
woocommerce structure to apply ajax into add_to_cart button while updates the cart automatically
<?php
/**
* Part of archive-product.php - add_to_cart button
* Template to apply ajax in button add to cart
* Original way to do add_to_cart button
*/
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="button %s product_type_%s"><i class="icon icon-white icon-shopping-cart"></i> %s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
$product->is_purchasable() ? 'add_to_cart_button' : '',
esc_attr( $product->product_type ),
esc_html( $product->add_to_cart_text() )
),
$product );
/**
* To update automaticaly the cart. We need work the same way.
* Example of structure of the cart.
* The class .cart-contents is necessary
*/
?>
<div class="cart-top">
<i class="icon icon-shopping-cart"></i>
<?php
if($woocommerce->cart->cart_contents_count == 0 || empty($woocommerce->cart->cart_contents_count)){
// empty cart
echo '<a href="#" class="cart-contents cart-link empty-cart">Seu carrinho está vazio</a>';
}else{
// structure of cart
echo '<a class="cart-contents cart-link" href="'.$woocommerce->cart->get_cart_url().'">';
echo '<span class="qtd-cart">'.sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'),
$woocommerce->cart->cart_contents_count).' no carrinho</span>';
echo '<span clas="cart-link-total">'.$woocommerce->cart->get_cart_total().'</span>';
echo '</a>';
}
?>
</div>
<?php
/**
* And now we need put the same structure as hook in the functions.php
* See it...
* Ensure cart contents update when products are added to the cart via AJAX
*/
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->cart_contents_count == 0 || empty($woocommerce->cart->cart_contents_count)){
echo '<a href="#" class="cart-contents empty-cart">Seu carrinho está vazio</a>';
}else{
echo '<a class="cart-contents cart-link" href="'.$woocommerce->cart->get_cart_url().'">';
echo '<span class="qtd-cart">'.sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'),
$woocommerce->cart->cart_contents_count).' no carrinho</span>';
echo '<span clas="cart-link-total">'.$woocommerce->cart->get_cart_total().'</span>';
echo '</a>';
}
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment