Woocommerce need to update total count shows next to minicart icon - wordpress

I'm using riode theme. I need to change the total cart items count shows in the header menu mini cart icon. I've 2 types of products. 1 is weight-based other is normal.
e.g.: My cart items => Product1 - 35gm - $50.00 ;
Product2 - 2(items) - $30.00 ;
here I need the total cart items to count as 3. but now it shows 37.
Here is my code.
<?php
add_filter( 'woocommerce_add_to_cart_fragments', 'add_to_cart_fragment',10,1);
function add_to_cart_fragment($fragments){
ob_start();
$items_count=0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$product_id = $cart_item['product_id'];
$quantity = $cart_item['quantity'];
if('some conditions'){
$items_count+=1;
}else{
$items_count+=$quantity;
}
}
?>
<a class="cart-toggle" href="<?php echo wc_get_cart_url(); ?>"><i class="d-icon-bag">
<span class="cart-count" style="opacity: 1;"><?php echo $items_count; ?> </span></i>
</a>
<?php
$fragments['a.cart-toggle'] = ob_get_clean();
return $fragments;
}

Simplify your code
add_filter( 'woocommerce_add_to_cart_fragments', 'add_to_cart_fragment',10,1);
function add_to_cart_fragment($fragments){
ob_start();
global $woocommerce;
$total_items = $woocommerce->cart->cart_contents_count;
?>
<a class="cart-toggle" href="<?php echo wc_get_cart_url(); ?>"><i class="d-icon-bag">
<span class="cart-count" style="opacity: 1;"><?php echo $total_items; ?> </span></i>
</a>
<?php
$fragments['a.cart-toggle'] = ob_get_clean();
return $fragments;
}

Related

ACF - How to exclude current page from acf relationship query

I have acf option page with the field Relationship. I display this block on many pages of the site. How can I exclude the current page from the list of displayed pages?
My code for Relationship:
<?php $coin_pages = get_field('sb_sector_pages_pages', 'option');
if( $coin_pages ):
foreach( $coin_pages as $post ) :
$permalink = get_permalink( $post->ID );
$thumbnail = get_the_post_thumbnail( $post->ID, 'full');
$title = get_the_title( $post->ID );
setup_postdata($post); ?>
<li class="coin-article__sidebar-list-item">
<a href="<?php echo esc_html( $permalink ); ?>" class="coin-article__sidebar-list-link">
<div class="coin-article__sidebar-coin-ico">
<?php echo $thumbnail; ?>
</div>
<span class="coin-article__sidebar-coin-title"><?php echo esc_html( $title ); ?></span>
</a>
</li>
<?php endforeach; ?>
I tried to add this to my functions.php but it doesn’t work for me:
add_filter('acf/fields/relationship/query/name=sb_sector_pages_pages', 'exclude_id', 10, 3);
function exclude_id ( $args, $field, $post ) {
$args['post__not_in'] = array( $post );
return $args;
}
How to exclude a current page from the ACF relationship query??

cart items in popup updating only when page is refreshed in woocommerce

i have a cart icon on the header.When the page is scrolled this cart icon is floating in a fixed position.when hover over cart icon the cart contents like name price subtotal all are displaying.now when adding a product the cart count is updating correctly.nut cart contents are updating only after refresh.
code of cart in header.php
<li>
<a href="<?php echo get_template_directory_uri(); ?>/cart"
><i class="uil uil-shopping-cart-alt"></i>
<div>Cart</div></a
>
<span class="counter" id="cart-count"><?php
$cart_count = WC()->cart->get_cart_contents_count();
//echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
echo $cart_count;
?></span>
</li>
updating cart count code in functions.php
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_cart_count', 50, 1 );
function refresh_cart_count( $fragments ){
ob_start();
?>
<span class="counter" id="cart-count"><?php
$cart_count = WC()->cart->get_cart_contents_count();
//echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
echo $cart_count;
?></span>
<?php
$fragments['#cart-count'] = ob_get_clean();
return $fragments;
}
this is the code displaying when hovering over the cart
<div class="miniBasketContent"><div class="miniBasketHeader">Your basket</div><div class="items"><?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$total = $woocommerce->cart->total;
foreach($items as $item => $values) { $_product = wc_get_product( $values['data']->get_id() );$getProductDetail = wc_get_product( $values['product_id'] );$price = get_post_meta($values['product_id'] , '_price', true);?><div class="item"><div class="image"><?php echo $getProductDetail->get_image();?></div><div class="info"><?php echo $_product->get_title();?></div><div class="price"><?php echo $price;?> AED</div></div><?php } ?></div><div class="miniBasketFooter"><div class="info"><div class="total"><span><?php echo $total; ?>AED</span>Total</div>Show basketGo to checkout</div></div></div>
what to do to update cart contents inside popup without reloading page.
You can try the below solution
demo_cart_link() Use this function where you want to display cart count HTML.
function.php
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '2.3', '>=' ) ) {
add_filter( 'woocommerce_add_to_cart_fragments', 'demo_cart_link_fragment', 10 , 1 );
} else {
add_filter( 'add_to_cart_fragments', 'demo_cart_link_fragment', 10 , 1 );
}
if ( ! function_exists( 'demo_cart_link_fragment' ) ) {
function demo_cart_link_fragment( $fragments ) {
global $woocommerce;
ob_start();
demo_cart_link();
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
}
if ( ! function_exists( 'demo_cart_link' ) ) {
function demo_cart_link() {
?>
<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'paperfig' ); ?>" type="button" data-toggle="modal">
<i class="fas fa-shopping-basket"></i>
<span class="cart-numbers"><?php echo wp_kses_data( sprintf( _n( ' %d', ' %d', WC()->cart->get_cart_contents_count(), 'paperfig' ), WC()->cart->get_cart_contents_count() ) );?></span>
</a>
<?php
}
}

cart count not updating in mobile devices woocommerce

i am using this code to update cart count in woocommerce to update woocommerce cart.it is working correctly in larger devices.but not working in mobile devices.anly after refresh it works.
i am attaching code below.
in functions.php this is the code
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_cart_count', 50, 1 );
function refresh_cart_count( $fragments ){
ob_start();
?>
<span class="counter" id="cart-count"><?php
$cart_count = WC()->cart->get_cart_contents_count();
echo $cart_count;
?></span>
<?php
$fragments['#cart-count'] = ob_get_clean();
return $fragments;
}
in header.php cart section in large devices and mobile devices
<li>
<a href="<?php echo get_template_directory_uri(); ?>/cart"
><i class="uil uil-shopping-cart-alt"></i> Cart</a
>
<span class="counter" id="cart-count-mobile"><?php
$cart_count = WC()->cart->get_cart_contents_count();
echo $cart_count;
?></span>
</li>
This is the same code repeating 2 times for cart in both large and mobile devices.
in functions.php use classname instead of id.it will work.
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_cart_count', 50, 1 );
function refresh_cart_count( $fragments ){
ob_start();
?>
<span class="counter" id="cart-count"><?php
$cart_count = WC()->cart->get_cart_contents_count();
echo $cart_count;
?></span>
<?php
$fragments['span.counter'] = ob_get_clean();
return $fragments;
}

Thumbnail and price not showing on related products section

Well, I'm trying to create a module with the related products I have in my shop. My template has a file in the route /theme-name/woocommerce/single-product/related.php. In this we can see some code of how it call to the related product. It use the template as the listing product page (same template), but when I tried to shpw the thumbnail or the price it doesn't work. It only show me the title of the product, nothing else.
This is the code I have in my template for the file /related.php
<?php
if ( $related_products ) : ?>
<section id="nm-related" class="related products">
<div class="nm-row">
<div class="col-xs-12">
<h2><?php esc_html_e( 'Related products', 'woocommerce' ); ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php foreach ( $related_products as $related_product ) : ?>
<?php
$post_object = get_post( $related_product->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; ?>
<?php woocommerce_product_loop_end(); ?>
</div>
</div>
</section>
<?php endif;
After that I use the "content-product" template like the listing page, where I don't have any problem.
For example, to show the product thumbnail I tried this:
<div class="nm-shop-loop-thumbnail nm-loader">
<a href="<?php echo esc_url( get_permalink() ); ?>" class="nm-shop-loop-thumbnail-link woocommerce-LoopProduct-link">
<?php
$id_pro = get_the_ID();
$pro_2 = get_post_thumbnail_id($id_pro);
$featured_image_url = wp_get_attachment_url( $pro_2 );
if(! empty( $featured_image_url )) { ?>
<?php
/**
* Hook: woocommerce_before_shop_loop_item_title.
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
} else { ?>
<img src="https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg" data-src="https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg" data-srcset="https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 350w, https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 250w, https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 400w" alt="" sizes="(max-width: 350px) 100vw, 350px" width="350" height="420" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail wp-post-image lazyloaded" srcset="https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 350w, https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 250w, https://cdn.mtods.ch/wp-content/uploads/sites/2/20190409092145/nopic.jpg 400w">
<?php
}
?>
</a>
</div>
And as I said, it works for the listing page but not inside single page product in related products section.
The $id_pro variable work in both pages, it shows the id of the product, but when I try to get the thumbnail URL with this id it return me "".
In the first image you can see the example for the listing page, on the other hand, in the second image you can see the example for the related products for the single product page:
I change the functionality and I created a new related product module, but still not working I don't know why...
I tried this:
In my related.php:
// Same category products query
$mproduct = new WP_Query(array(
'post_type' => 'product',
'product_cat' => $cat_slug,
'posts_per_page' => -1,
));
// Get query data
$posts = $mproduct->get_posts();
$num = $mproduct->post_count;
// Create new array with the actual product in it
$array_pro = [];
$array_pro[] = $product_id;
// Function for check if product is in array
function test_function($rand_pro, $array_pro, $posts, $num) {
if (in_array($rand_pro, $array_pro)) {
$new_pro_id = $posts[rand(0,$num-1)]->ID;
return true;
} else {
return false;
}
}
// If array is not full insert next elements
while (count($array_pro) < 5) {
$rand_pro = $posts[rand(0,$num-1)]->ID;
$pro_in_array = test_function($rand_pro, $array_pro, $posts, $num);
if ($pro_in_array == false) {
$array_pro[] = $rand_pro;
}
}
// Remove first element of array
array_shift($array_pro);
?>
<div class="related-products-row">
<?php
foreach( $array_pro as $post ) {
$rel_product_id = $post;
$rel_product_title = get_the_title($rel_product_id);
$rel_product_link = get_the_permalink($rel_product_id);
$rel_product_img_url = get_the_post_thumbnail($rel_product_id);
$_product = wc_get_product( $rel_product_id );
$rel_product_ref = $_product->get_attribute( 'reference' );
?>
<div class="col-md-3">
<p><?php echo $rel_product_img_url; ?></p>
</div>
<?php
}
?>
</div>
If you can see, when I try to getthe name I don't have problems but when I try to get the post_thumbnail_url or ref_number it doesn't show anything I don't know why.

Woocommerce Related Products - show products in price interval

I'm trying to modify related.php in order to randomly show any 3 products from the shop that has a price in the range of plus/minus 100.
I'm using ACF fields and Woocommerce 3.2.
The problem is that although the products are selected correctly, their price is not displayed. Instead, the price of the reference product is displayed for all 3 products.
Here is the code (price_obj is the ACF field for price):
global $product, $woocommerce_loop;
$product = new WC_Product(get_the_ID());
$price_product = get_field('price_obj',get_the_ID());
$args1=array(
'post_type' => 'product',
'posts_per_page' => -1,
'post__not_in' => array( $product->get_id() )
);
$products_in_range = array();
$my_query = new wp_query($args1);
if( $my_query->have_posts() ) {
$val = count($my_query->get_posts());
while ($my_query->have_posts()) {
$my_query->the_post();
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
$price = get_field('price_obj');
$id = get_the_ID();
if ((($price_product-100) <= $price) && ($price <= ($price_product+100))){
array_push($products_in_range,$id);
}
}
}
wp_reset_query();
$rand_products = array_rand($products_in_range, 3);
?>
<?php if ($rand_products){ ?>
<div class="related products">
<h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>
<ul class="products">
<?php
foreach ($rand_products as $prod){
$title = get_the_title($products_in_range[$prod]);
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($products_in_range[$prod]), 'large');
$link = get_permalink($products_in_range[$prod]);
$product_prod = new WC_Product($products_in_range[$prod]);
$price = wc_price($product->get_price());
?>
<li class="product type-product status-publish has-post-thumbnail first instock shipping-taxable purchasable product-type-simple">
<a href="<?php echo $link; ?>" class="woocommerce-LoopProduct-link">
<span class="et_shop_image">
<img width="400" height="400"
src="<?php echo $featured_image[0]; ?>"
class="attachment-shop_catalog size-shop_catalog wp-post-image"
alt=""
title="">
<span class="et_overlay"></span>
</span>
<h3><?php echo $title; ?></h3>
<span class="price">
<span class="woocommerce-Price-amount amount">
<?php echo $price; ?>
</span>
</span>
</a>
</li>
<?php } ?>
</ul>
</div>
Many thanks for any help!
get_field() can take three parameters. The first one is mandatory but the last 2 are optional.
get_field($selector, [$post_id], [$format_value]);
Where the $selector is the name of the field.The $post_id is self explanatory, but is defaulted to the current post and the $format_value decides whether you want to apply formatting logic.
Because you are calling the function via get_field('price_obj') and omitting the ID of the post you want it is defaulting to the current post in this case the post of the main item.
This is wrong, because you were inside the WordPress loop the right object was being saved into $price.
As you mentioned below, when you went to access the object via
$price = wc_price($product->get_price());
You were accessing the $product object, which is the main item. But your sub-product were stored in $product_prod, so to access it's price you had to change your code to
$price = wc_price($product_prod->get_price());

Resources