add minicart widget

This commit is contained in:
Thuan Bui
2017-10-17 23:34:30 -04:00
parent 4f0eb6fe03
commit 7b95006655
2 changed files with 96 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
<?php
require_once( 'ycb-minicart-widget.php' );
add_action( 'widgets_init', 'ycb_register_widgets', 50 );
//* Registers a widget
function ycb_register_widgets() {
//* Register Minicart widget
register_widget( 'YCB_Minicart_Widget' );
}
function ycb_wc_add_to_cart_fragment( $fragments ) {
global $woocommerce, $theme_options;
//$cart_icon = '<i class="ion-ios-cart-outline"></i>';
$cart_icon = '<i class="icon ycb-cart"></i>';
ob_start();
?>
<span class="widget_shopping_cart cart-contents">
<span class="minicart">
<?php echo $cart_icon; ?>
<span class="total-items">
<?php echo sprintf(_n( '%d', '%d', WC()->cart->cart_contents_count, 'woothemes' ), WC()->cart->cart_contents_count ); ?>
</span>
</span>
</span>
<?php
$fragments['span.cart-contents'] = ob_get_clean();
return $fragments;
}
add_filter('add_to_cart_fragments', 'ycb_wc_add_to_cart_fragment' );
@@ -0,0 +1,64 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Shopping Cart Widget
*
* Displays shopping cart widget
*
* @author Chinmoy Paul
* @category Widgets
* @version 1.0
* @extends WC_Widget
*/
class YCB_Minicart_Widget extends WC_Widget {
/**
* Constructor
*/
public function __construct() {
$this->widget_cssclass = 'woocommerce widget_minicart';
$this->widget_description = __( "Display the user's Cart.", 'ycb' );
$this->widget_id = 'woocommerce_widget_minicart';
$this->widget_name = __( 'YCB Mini Cart', 'ycb' );
$this->settings = array();
parent::__construct();
}
/**
* widget function.
*
* @see WP_Widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
global $theme_options;
$cart_icon = '<i class="icon ycb-cart"></i>';
$this->widget_start( $args, $instance );
echo '<div class="minicart-wrapper clearfix">' . "\n";
echo '<span class="widget_shopping_cart cart-contents">';
echo ' <span class="minicart">' . $cart_icon .
'<span class="total-items">' . sprintf(_n( '%d', '%d', WC()->cart->cart_contents_count, 'woothemes' ), WC()->cart->cart_contents_count ) . '</span>' .
' </span>' . "\n";
echo '</span>' . "\n";
echo '</div>';
// Insert cart widget placeholder - code in woocommerce.js will update this on page load
echo '<div class="widget_shopping_cart_content"></div>';
$this->widget_end( $args );
}
}