Skip to content

Instantly share code, notes, and snippets.

@JasvinderSingh1
Created February 19, 2020 10:51
Show Gist options
  • Save JasvinderSingh1/1fe1b1119d47498f938d6642fa5d7eb0 to your computer and use it in GitHub Desktop.
Save JasvinderSingh1/1fe1b1119d47498f938d6642fa5d7eb0 to your computer and use it in GitHub Desktop.
<?php
/**
* Mini-cart
*
* Contains the markup for the mini-cart, used by the cart widget.
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/mini-cart.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 3.7.0
*/
defined( 'ABSPATH' ) || exit;
do_action( 'woocommerce_before_mini_cart' ); ?>
<?php if ( ! WC()->cart->is_empty() ) : ?>
<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' );
$products_in_cart = array();
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 );
$products_in_cart[] = $product_id;
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( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'woocommerce_cart_item_remove_link',
sprintf(
'<a href="%s" class="remove remove_from_cart_button" aria-label="%s" data-product_id="%s" data-cart_item_key="%s" data-product_sku="%s">&times;</a>',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
esc_attr__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $cart_item_key ),
esc_attr( $_product->get_sku() )
),
$cart_item_key
);
?>
<?php if ( empty( $product_permalink ) ) : ?>
<?php echo $thumbnail . $product_name; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php else : ?>
<a href="<?php echo esc_url( $product_permalink ); ?>">
<?php echo $thumbnail . $product_name; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</a>
<?php endif; ?>
<?php echo wc_get_formatted_cart_item_data( $cart_item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s &times; %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</li>
<?php
}
}
do_action( 'woocommerce_mini_cart_contents' );
?>
</ul>
<p class="woocommerce-mini-cart__total total">
<?php
/**
* Hook: woocommerce_widget_shopping_cart_total.
*
* @hooked woocommerce_widget_shopping_cart_subtotal - 10
*/
do_action( 'woocommerce_widget_shopping_cart_total' );
?>
</p>
<div class="cus_upsells">
<h3 style="margin-left: 20px;font-family: sans-serif;font-size: 22px;font-weight: 500;border-bottom: 1px solid;display: table;">Add More</h3>
<?php
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1
);
$published_products = get_posts( $args );
$script = '<script>if (typeof widgetvarids == "undefined") {var widgetvarids = new Array();}if (typeof widgetdpricehtml == "undefined") {var widgetdpricehtml = new Array();}if (typeof widgetdrpricehtml == "undefined") {var widgetdrpricehtml = new Array();}';
foreach($published_products as $each_pro_id){
if(!in_array($each_pro_id->ID,$products_in_cart)){
$eachid = $each_pro_id->ID;
$product = new WC_Product_Variable($eachid);
$sing_product = wc_get_product( $eachid );
$variations = $product->get_available_variations();
if (empty($variations)) {
$single_price = $sing_product->get_price();;
}
if(sizeof($proids)==3){
echo '<div class="col-sm-4">';
}else{
echo '<div class="cus_upsells_inner">';
}
$title = $product->get_name();
echo '<style>.sdropdown-menu{width: 98%;padding: 4px 7px;margin-right: 3px;margin-bottom: 0;}#dropdownMenusize_'.$eachid.',#dropdownMenucolor_'.$eachid.'{height: 48px;display: -ms-flexbox;display: -webkit-flex;display: flex;align-items: center;justify-content: space-between;width: 100%;text-align: left;background-color: transparent;border: 1px solid #aaaaaa;outline: none;color: #4f4f4f;font-size: 15px;border-radius: 5px;}
#dropdownMenusize_'.$eachid.' span.offer-price,#dropdownMenucolor_'.$eachid.' span.offer-price{margin-left: auto;margin-right: 5px;}</style>';
echo '<div class="new-cross-item" data-proid="'.$eachid.'">';
echo '<div class="product-variation varbox_'.$eachid.'"><div style="display:flex;"><img src="'.get_the_post_thumbnail_url($eachid,"large").'" style="max-height: 50px;">';
$drp = array();
foreach($variations as $eachvar){
$key = '';
if(isset($eachvar['attributes']['attribute_pa_size'])){
$li= '<option class="chngdrp" value="'.str_replace(' ','',$eachvar['attributes']['attribute_pa_size']).'">';
$li.= ucwords(str_replace("-"," ",$eachvar['attributes']['attribute_pa_size']));
$li.= '</option>';
$drp['size'][] = $li;
$key.= str_replace(' ','',$eachvar['attributes']['attribute_pa_size']);
}
if(isset($eachvar['attributes']['attribute_pa_color'])){
$li= '<option class="chngdrp" value="'.str_replace(' ','',$eachvar['attributes']['attribute_pa_color']).'">';
$li.= ucwords(str_replace("-"," ",$eachvar['attributes']['attribute_pa_color']));
$li.= '</option>';
$drp['color'][] = $li;
$key.= str_replace(' ','',$eachvar['attributes']['attribute_pa_color']);
}
$script.='widgetvarids["'.$eachid.'_'.$key.'"]="'.$eachvar['variation_id'].'";widgetdpricehtml["'.$eachid.'_'.$key.'"]="'.$eachvar['display_price'].'";widgetdrpricehtml["'.$eachid.'_'.$key.'"]="'.$eachvar['display_regular_price'].'";';
}
echo '<div class="cus_title"><a href="'.get_permalink($each_pro_id->ID).'">'.$title.'</a></div></div>';
if(!empty($drp['size'])){
echo '<div style="display:flex;"><div class="dropdown dropdown-select">';
echo '<select class="sdropdown-menu">';
echo implode('',array_unique($drp['size']));
echo '</select>';
echo '</div>';
}
if(!empty($drp['color'])){
echo '<div class="dropdown dropdown-select">';
echo '<select class="sdropdown-menu">';
echo implode('',array_unique($drp['color']));
echo '</select>';
echo '</div></div>';
}
echo '</div>';
if (empty($variations)) {
echo '<div><div style="line-height: 28px;">Price: <span class="offer-price">$'.$single_price;
} else {
echo '<div><div style="line-height: 28px;">Price: <span class="offer-price">$'.$variations[0]['display_price'];
}
if($variations[0]['display_price']<$variations[0]['display_regular_price']){
echo '<span>$'.$variations[0]['display_regular_price'].'</span>';
}
echo '</span></div>';
echo '<div class="add-to-cart">';
echo '<button style="width:100%;" class="widget_addcart thisid'.$eachid.'" data-sku="'.get_post_meta($eachid,'_sku',true).'">ADD TO CART</button>';
echo '</div></div>';
echo '</div>';
echo '</div>';
echo '<script>';
echo 'jQuery(".sdropdown-menu").on("change",function(){
var getthiskkey = "";
jQuery(this).closest(".new-cross-item").find(".sdropdown-menu").each(function(){
getthiskkey = getthiskkey+jQuery(this).val();
});
var thisproid = jQuery(this).closest(".new-cross-item").attr("data-proid");
var pricehtml = "";
pricehtml = pricehtml+"$"+widgetdpricehtml[thisproid+"_"+getthiskkey];
if(widgetdpricehtml[thisproid+"_"+getthiskkey]<widgetdrpricehtml[thisproid+"_"+getthiskkey]){
pricehtml = pricehtml+"<span>$"+widgetdrpricehtml[thisproid+"_"+getthiskkey]+"</span>";
}
jQuery(this).closest(".new-cross-item").find(".offer-price").html(pricehtml);
});';
echo 'jQuery(".thisid'.$eachid.'").on("click",function(){
jQuery(this).css("opacity","0.5");
jQuery(this).css("pointer-events","none");
var getthiskkey = "";
jQuery(this).closest(".new-cross-item").find(".sdropdown-menu").each(function(){
getthiskkey = getthiskkey+jQuery(this).val();
});
var getthisproid = jQuery(this).closest(".new-cross-item").attr("data-proid");
var getthisvarid = widgetvarids[getthisproid+"_"+getthiskkey];
jQuery.ajax({
url: "'.home_url().'/wp-admin/admin-ajax.php",
type: "POST",
datatype: "JSON",
data: {action:"addthistocart",varid:getthisvarid,proid:getthisproid,qty:1},
success:function(response){
var res = jQuery.parseJSON(response);
jQuery(".thisid'.$eachid.'").text("Added to cart");
jQuery(".thisid'.$eachid.'").closest(".new-cross-item").parent().remove();
jQuery(document.body).trigger("wc_fragment_refresh");
}
});
});';
echo '</script>';
}
}
$script.='</script>';
echo $script;
?>
</div>
<?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>
<?php do_action( 'woocommerce_widget_shopping_cart_after_buttons' ); ?>
<?php else : ?>
<p class="woocommerce-mini-cart__empty-message"><?php esc_html_e( 'No products in the cart.', 'woocommerce' ); ?></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