WooCommerce cart items number based on unique products - wordpress

Is there any way to change the items in cart number based only on the unique products?
I can’t find anything to start play with. Any help greatly appreciated!
Example:
Pen 3x
Book 2x
Currently :
5 items on Cart
Should be :
2 items on Cart.
Thanks
This is the current code on the theme and I target the kleo-notifications new-alert:
if ( ! function_exists( 'kleo_woo_get_mini_cart' ) ) {
function kleo_woo_get_mini_cart( $just_inner = false ) {
global $woocommerce;
//Enqueue variations script for quick view
wp_enqueue_script( 'wc-add-to-cart-variation' );
$cart_output = "";
$cart_total = $woocommerce->cart->get_cart_total();
$cart_count = $woocommerce->cart->cart_contents_count;
$cart_count_text = kleo_product_items_text( $cart_count );
$cart_has_items = '';
if ( $cart_count != "0" ) {
$cart_has_items = ' has-products';
}
if ( ! $just_inner ) {
$cart_output .= '<li class="menu-item kleo-toggle-menu shop-drop">'
. '<a class="cart-contents js-activated" href="' . wc_get_cart_url() . '" title="' . __( "View Cart", "woocommerce" ) . '">'
. '<span class="cart-items' . $cart_has_items . '"><i class="icon icon-basket-full-alt"></i> ';
if ( $cart_count != "0" ) {
$cart_output .= "<span class='kleo-notifications new-alert'>" . $cart_count . "</span>";
}

add_filter( 'woocommerce_add_to_cart_fragments', 'test_cart_count_fragments', 20, 10 );
function test_cart_count_fragments( $fragments ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$fragments['span.new-alert'] = '<span class="kleo-notifications new-alert">' . count($items). '</span>';
return $fragments;
}

Related

How can I edit the ratings output in woocommerce to remove the text and just how the stars without using css

I'm new to coding and wordpress/woocommerce and am trying to find a simple way to edit the output of wc_get_rating_html.
At the moment it outputs the following:
function wc_get_rating_html( $rating ) {
if ( $rating > 0 ) {
$rating_html = '<div class="star-rating" title="' . sprintf( esc_attr__( 'Rated %s out of 5', 'woocommerce' ), $rating ) . '">';
$rating_html .= '<span style="width:' . ( ( $rating / 5 ) * 100 ) . '%"><strong class="rating">' . $rating . '</strong> ' . esc_html__( 'out of 5', 'woocommerce' ) . '</span>';
$rating_html .= '</div>';
} else {
$rating_html = '';
}
return apply_filters( 'woocommerce_product_get_rating_html', $rating_html, $rating );
}
I simply want to output the star rating and not the text that goes before and after. Any suggestions on how i can do this as i obv cant edit the template file. thanks
Wordpress utilizes filters which allow you to replace and/or modify the output of what the filter processes. Here is the full block for the wc_get_rating_html function:
function wc_get_rating_html( $rating, $count = 0 ) {
$html = '';
if ( 0 < $rating ) {
/* translators: %s: rating */
$label = sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $rating );
$html = '<div class="star-rating" role="img" aria-label="' . esc_attr( $label ) . '">' . wc_get_star_rating_html( $rating, $count ) . '</div>';
}
return apply_filters( 'woocommerce_product_get_rating_html', $html, $rating, $count );
}
In your example, woocommerce_product_get_rating_html is the filter name and takes three arguments:
$rating_html which represents the output which the user sees
$rating which is the numerical rating
$count which is the total number of ratings.
If you wish to replace the output, you'll need to hook into the filter woocommerce_product_get_rating_html with a function that will return the modified content. You'll definitely need to review Hooks & Filters and how they work in Wordpress if you are not familiar with utilizing hooks and filters in Wordpress.

Woocommerce: display custom text under products on checkout page based on shipping class

I found this function that is working good on the cart page, but I also want to use it on the checkout page, what would I need to modify to make it work there? thanks in advance!
// Display a custom text under cart item name in cart page
add_filter( 'woocommerce_cart_item_name', 'custom_text_cart_item_name', 10, 3 );
function custom_text_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
// Only on cart page
if( ! is_cart() )
return $item_name;
// Here below define your shipping classes slugs (one by line)
$shipping_class_1 = '5-gallon';
$shipping_class_2 = '10-gallon';
$shipping_class = $cart_item['data']->get_shipping_class();
// First shipping class
if( $shipping_class === $shipping_class_1 ) {
$item_name .= '<br /><div class="item-shipping-class">' . __("Please call confirm shipping rates", "woocommerce") . '</div>';
}
// 2nd shipping class
elseif( $shipping_class === $shipping_class_2 ) {
$item_name .= '<br /><div class="item-shipping-class">' . __("Some different message…", "woocommerce") . '</div>';
}
return $item_name;
}
Hey why are you checking just for cart page if you want it on both the page?
Just check for both.
if( ! ( is_cart() || is_checkout()) )
return $item_name;
So the code becomes.
add_filter( 'woocommerce_cart_item_name', 'bks_custom_text_cart_item_name', 10, 3 );
function bks_custom_text_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
if( ! ( is_cart() || is_checkout()) ) // CHANGE HERE
return $item_name;
$shipping_class_1 = '5-gallon';
$shipping_class_2 = '10-gallon';
$shipping_class = $cart_item['data']->get_shipping_class();
if( $shipping_class === $shipping_class_1 ) {
$item_name .= '<br /><div class="item-shipping-class">' . __("Please call confirm shipping rates", "woocommerce") . '</div>';
}
elseif( $shipping_class === $shipping_class_2 ) {
$item_name .= '<br /><div class="item-shipping-class">' . __("Some different message…", "woocommerce") . '</div>';
}
return $item_name;
}
Screenshot of Checkout:

Custom Option Cart Woocommerce

to everyone !
Someone can help me with my proyect, Is very simple but I have one thing that makes me crazy.
I have this:
every checkbox have a value (additional value price)
I need change the price wen I checked in one, two or all checkbox and save this options to send it to the checkout and email....And show this options to order information in the back-end.
add_filter( 'woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3 );
function custom_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
if( ! is_cart() )
return $item_name;
$item_name .= $value3.'<div class="lineList">';
$value2 = $cart_item['data']->get_meta('_preciosCata');
if( $value1 = $cart_item['data']->get_meta('_varArrayTitle') ) {
for ($i=0;$i<count($value1); $i++) {
//$item_name .= '<div class="lineOne lorem"><input type="checkbox" name="cart[%s][lorem]" id="addPrice" value="' . $value2[$i] . '"> <span class="custom-field"> ' . $value1[$i] . ' </span></div>';
$item_name .= sprintf( '<div class="lineOne lorem"><input type="checkbox" id="_checkbox' . $i . '" name="_checkbox' . $i . '" value="' . $value2[$i] . '" class="checkedBox url text" /> <span class="custom-field">' . $value3.$value1[$i] . ' </span></div>', $cart_item_key, esc_attr( $values['url'] ) );
}
}
$item_name .= '</div>';
return $item_name;
}
I update the car here but dont save the value:
add_action( 'wp_footer', 'bbloomer_cart_refresh_update_qty' );
function bbloomer_cart_refresh_update_qty() {
if (is_cart()) {
?>
<script>
jQuery('div.woocommerce').on('change', '.checkedBox', function(){
jQuery("[name='update_cart']").prop("disabled", false);
jQuery("[name='update_cart']").trigger("click");
//alert($(this).val());
});
</script>
<?php
}
}
I need help please

can I add id or class 'wc_display_item_meta'

in woocommerce checkout result page, 'wc-item-meta' list
I want add id or class in 'strong' and 'p' element, to control with css.
(change text to image)
like p class="01_S", strong class="wc-item-meta-label 01_S"
is there any solution?
<ul class="wc-item-meta">
<li>
<strong class="wc-item-meta-label">sleeve :</strong>
<p>01_S</p>
</li>
<li>
<strong class="wc-item-meta-label">hood :</strong>
<p>02_M</p>
</li>
</ul>
Use the follows code snippet in your active theme's functions.php to achieve the above -
function modify_woocommerce_display_item_meta( $html, $item, $args ) {
$strings = array();
$html = '';
foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
// remove strip tags
$display_value = wp_strip_all_tags( $meta->display_value );
$value = $args['autop'] ? wp_kses_post( $display_value ) : wp_kses_post( make_clickable( trim( $display_value ) ) );
$args['label_before'] = '<strong class="wc-item-meta-label ' . wp_strip_all_tags( $display_value ) . '">';
$args['label_after'] = ':</strong> ';
$strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . '<p class="' . wp_strip_all_tags( $display_value ) . '">' . $value . '</p>';
}
if ( $strings ) {
$html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
}
return $html;
}
add_filter( 'woocommerce_display_item_meta', 'modify_woocommerce_display_item_meta', 99, 3 );

Use different size thumbnails for subcategories and products in Woocommerce

I need to set a different size of thumb for subcategory and product, is this possible?
I need to show a list of subcategories with thumb 500 x 100 and a list of products with thumbnail 300 x 390.
I have already setting in woocommerce> setting > product > display > Product images but i cannot set a different size for subcategory or product.
lol i develop a clean solution:
function register_size_image() {
add_image_size( 'category_thumb', 1170, 585,true );
add_image_size( 'product_thumb', 750, 940,true );
}
add_action( 'after_setup_theme', 'register_size_image' );
function size_of_category_thumb($u)
{
return array(1170, 585,true);
}
add_filter('subcategory_archive_thumbnail_size', 'size_of_category_thumb');
function size_of_product_thumb($u)
{
return array(750, 940,true);
}
add_filter('single_product_archive_thumbnail_size', 'size_of_product_thumb');
i resolve overriding woocommerce's function, i put this on my functions.php
add_image_size( 'category_thumb', 500, 100,1 );
add_image_size( 'product_thumb', 300, 390,1 );
function woocommerce_subcategory_thumbnail( $category ) {
$small_thumbnail_size = 'category_thumb';
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
if ( $thumbnail_id ) {
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
$image = $image[0];
$image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumbnail_size ) : false;
$image_sizes = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumbnail_size ) : false;
} else {
$image = wc_placeholder_img_src();
$image_srcset = $image_sizes = false;
}
if ( $image ) {
// Prevent esc_url from breaking spaces in urls for image embeds
// Ref: https://core.trac.wordpress.org/ticket/23605
$image = str_replace( ' ', '%20', $image );
// Add responsive image markup if available
if ( $image_srcset && $image_sizes ) {
echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
} else {
echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" />';
}
}
}
function woocommerce_get_product_thumbnail( $size = 'shop_catalog', $deprecated1 = 0, $deprecated2 = 0 )
{
global $product;
// $image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );
return $product ? $product->get_image( 'product_thumb' ) : '';
}
it works!
Yes, That is quite possible. Follow these steps -
Add new image size. To do that add following code to your theme's functions.php
add_image_size( 'your-custom-size', 500, 100 );
// You can add multiple image sizes by repeating this line with different values (as per you need)
After this step upload images.
If it is overhead for you to upload images again, you can use this plugin
This will create thumbnails with all your specified sizes.
Now in your theme, whereever you are showing images with function like -
the_post_thumbnail( 'your-custom-size' ) or wp_get_attachment_image( 42, 'your-custom-size' ), pass your image size name as param.
function yourFunctionName()
{
return array(1140, 300,true);
}
add_filter('subcategory_archive_thumbnail_size', 'yourFunctionName');
Works for me

Resources