-
-
Save qant/26c607d74a4c3c4bf3542fab36d8a4fe to your computer and use it in GitHub Desktop.
Woocommerce Mini Cart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Override woocommerce product categories widgets | |
add_action( 'widgets_init', 'floweb_override_woocommerce_widgets', 15 ); | |
function floweb_override_woocommerce_widgets() { | |
// Ensure our parent class exists to avoid fatal error | |
if ( class_exists( 'WC_Widget_Cart' ) ) { | |
unregister_widget( 'WC_Widget_Cart' ); | |
require get_template_directory() . '/functions/custom-widgets/floweb-class-wc-widget-cart.php'; | |
register_widget( 'Floweb_WC_Widget_Cart' ); | |
} | |
} | |
//mini-cart items counter | |
add_filter( 'woocommerce_add_to_cart_fragments', 'floweb_cart_count_fragments', 10, 1 ); | |
function floweb_cart_count_fragments( $elements ) { | |
$elements['div.header-cart-count'] = '<div class="header-cart-count">' . WC()->cart->get_cart_contents_count() . '</div>'; | |
return $elements; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// theme/wocoomerce/cart/mini-cart.php | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
do_action( 'woocommerce_before_mini_cart' ); ?> | |
<?php if ( ! WC()->cart->is_empty() ) : ?> | |
<div class="mini-cart"> | |
<p class="woocommerce-mini-cart__total total"> | |
<span class="woocommerce-mini-cart__icon"> | |
<span class="fa fa-shopping-cart fa-fw"></span> | |
<span class="woocommerce-mini-cart__total-count"><?php echo WC()->cart->get_cart_contents_count(); ?></span> | |
</span> | |
<span class="woocommerce-mini-cart__subtotal"><?php echo WC()->cart->get_cart_subtotal(); ?></span> | |
</p> | |
<div class="mini-cart-dropdown"> | |
<ul class="woocommerce-mini-cart cart_list product_list_widget <?php echo esc_attr( $args['list_class'] ); ?>"> | |
<?php | |
do_action( 'woocommerce_before_mini_cart_contents' ); | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); | |
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key ); | |
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) { | |
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ); | |
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key ); | |
$product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); | |
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key ); | |
?> | |
<li class="woocommerce-mini-cart-item <?php echo esc_attr( apply_filters( 'woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key ) ); ?>"> | |
<?php | |
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf( | |
'<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s">×</a>', | |
esc_url( WC()->cart->get_remove_url( $cart_item_key ) ), | |
esc_attr__( 'Remove this item', 'woocommerce' ), | |
esc_attr( $product_id ), | |
esc_attr( $_product->get_sku() ) | |
), $cart_item_key ); | |
?> | |
<?php if ( ! $_product->is_visible() ) : ?> | |
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?> | |
<?php else : ?> | |
<a href="<?php echo esc_url( $product_permalink ); ?>"> | |
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?> | |
</a> | |
<?php endif; ?> | |
<?php echo WC()->cart->get_item_data( $cart_item ); ?> | |
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?> | |
</li> | |
<?php | |
} | |
} | |
do_action( 'woocommerce_mini_cart_contents' ); | |
?> | |
</ul> | |
<?php do_action( 'woocommerce_widget_shopping_cart_before_buttons' ); ?> | |
<p class="woocommerce-mini-cart__buttons buttons"><?php do_action( 'woocommerce_widget_shopping_cart_buttons' ); ?></p> | |
</div> | |
</div> | |
<?php else : ?> | |
<p class="woocommerce-mini-cart__empty-message"> | |
<span class="fa fa-shopping-cart fa-fw"></span> | |
<span><?php _e( 'No products in the cart.', 'woocommerce' ); ?></span> | |
</p> | |
<?php endif; ?> | |
<?php do_action( 'woocommerce_after_mini_cart' ); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment