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;
}
Related
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;
}
Is it possible to find out if only the date/time of a post has been changed or something in the content? The the_modified_date() function covers both and I can't find a way to do a specific check.
The Background: I have a list of posts which will be published in the future and I want to have some feedback for the users if the date or time of an upcoming post changes.
This is my code so far:
<?php
$loop_ls = new WP_Query( $args_ls );
if($loop_ls->have_posts()) : while($loop_ls->have_posts()) : $loop_ls->the_post();
// set date/time variables
$live_date = get_the_date('j.n.');
$live_time = get_the_date('H:i');
?>
<?php if ( get_post_status() == 'future' ) : ?>
<div class="live live--upcoming">
<p><?php _e('Am', 'textdomain') ?> <span class="post-date"><?php echo $live_date; ?></span> <?php _e('um', 'textdomain') ?> <span class="post-time"><?php echo $live_time; ?></span> <?php _e('Uhr', 'textdomain') ?> <?php _e('zum Thema:', 'textdomain') ?>
<br/><span class="post-title"><?php echo get_the_title(); ?></span>
</p>
</div>
<?php elseif ( get_post_status() == 'publish' ) : ?>
<div class="live live--now">
<p><?php _e('Jetzt:', 'textdomain') ?> <span class="post-title"><?php echo get_the_title(); ?></span></p>
<a class="btn btn--default" href="<?php echo get_the_permalink(); ?>"><?php _e('zum LIVE Video', 'textdomain') ?></a>
</div>
<?php endif; ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
OK, I figured out a way to save a creation date for posts published in the future (code below). But I still have the problem, that I can't check if only a posts date/time is modified (not the content).
function wx_add_creation_date_to_live_videos( $post ) {
$post_id = get_the_ID();
$post_type = get_post_type($post_id);
if( $post_type == 'video' && has_term('live', 'video_category') ) {
// If this is a revision, do nothing.
if ( wp_is_post_revision( $post_id ) )
return;
$creation_date = get_the_modified_date('Y-m-d H:i:s', $post_id);
update_post_meta( $post_id, 'creation_date', $creation_date );
}
}
add_action( 'draft_to_future', 'wx_add_creation_date_to_live_videos', 10, 3 );
EDIT:
sometimes you just think too complicated. To check a change on post date/time I changed the code above to this
$orig_publish_date = get_the_date('Y-m-d H:i:s', $post_id);
update_post_meta( $post_id, 'orig_publish_date', $orig_publish_date );
And now I can compare the values of the orig_publish_date with the current publish_date. And that's it.
I'm trying to set up a taxonomy-business-categories.php template for a Wordpress theme.
I have the custom post type goto_listing, with custom taxonomy business-categories. There are two levels of business-categories, eg
Boatyards, Marinas & Moorings (parent term)
Boatyards
Marinas
Moorings
Cleaning (parent term)
Fuel Cleaning Service
Laundry Services
etc.
In my taxonomy-business-categories.php template I have a loop displaying:
Boats (child term)
Business 1
Business 2 (premium listing)
Business 3
Business 4 (listing with some text manually entered in post excerpt)
Marinas (child term)
Business 1 (listing with some text manually entered in post excerpt)
Business 2 (premium listing)
Business 3
etc.
All that is working (including links to the single listing for the business)
But I need to get three things to happen in the foreach loop.
I need to check if:
it is a premium listing (this is a custom field,
goto_premium_listing) and make that a link to the single listing.
does the post have a manually-inserted excerpt, if so show an icon by the listing and make it toggle to show the excerpt.
everything else should just be displayed without any icons or links.
I have achieved all three, but not altogether.
I can either get 1 and 3 working together or 2 and 3 working together. Never all three. I get Parse error: syntax error, unexpected 'else' (T_ELSE) or various other warnings.
The codes is as follows:
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
$wpq = array (
'post_type' => 'goto_listing',
'taxonomy'=>$taxonomyName,
'term'=>$term->slug,
'order'=>'asc'
);
$query = new WP_Query ($wpq);
echo "<h3>$term->name:</h3>";
?>
<ul class="goto-leaders">
<?php if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
// BELOW IS THE IF ELSE PART THAT'S NOT WORKING ALL TOGETHER
<?php
if (get_post_meta($post->ID, 'goto_premium_listing ', true)); { ?>
<li><span><?php the_title();?></span><span><?php echo CFS()->get( 'goto_telephone' ); ?></span></li>
<?php }
else {
if ($post->post_excerpt ) { ?>
<li><span><i class="fa fa-arrow-circle-o-down"></i> <?php the_title();?></span><span><?php echo CFS()->get( 'goto_telephone' ); ?></span></li>
<div id="panel-<?php echo $post->ID ; ?>" style="margin-top: 1rem;" class="goto-toggle hide" data-toggler=".hide"><?php echo $post->post_excerpt; ?></div>
<?php }
else { ?>
<li><span><?php the_title();?></span><span><?php echo CFS()->get( 'goto_telephone' ); ?></li>
<?php } ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
</ul>
<?php } ?> <!-- end foreach -->
I think the problem is with the if else statement within the foreach loop
How do I fix this?
Tracy
UPDATE: Using the following code results in no error warnings but only the first if statement is acted upon, ie I get the icon and toggle text on the business with an excerpt. The rest take the final else statement, ie a plain listing, no icons, not links, nada.
<?php
if($post->post_excerpt ){ ?>
<li><span><i class="fa fa-arrow-circle-o-down"></i> <?php the_title();?></span><span><?php echo CFS()->get( 'goto_telephone' ); ?></span></li>
<div id="panel-<?php echo $post->ID ; ?>" style="margin-top: 1rem;" class="goto-toggle hide" data-toggler=".hide"><?php echo $post->post_excerpt; ?></div>
<?php }
else {
if (get_post_meta($post->ID, 'goto_premium_listing ', true)) { ?>
<li><span><?php the_title();?></span><span><?php echo CFS()->get( 'goto_telephone' ); ?></span></li>
<?php }
else { ?>
<li><span><?php the_title();?></span><span><?php echo CFS()->get( 'goto_telephone' ); ?></li>
<?php }}?>
The above codes produces this:
Taking the first child cat Boatyards, you can see Clarke's Court Boatyard, which is a premium listing (with customfield of goto-premium-listing), Grenada Marina is listing with an excerpt, Spice Island Marine Services is a plain old listing.
Grenada Marina and Spice Island Marine Services are working correctly. Clarke's Court Boatyard is a premium listing and should be appearing the same as Spice Island Marine but with a link to the single listing page.
So, it's working correctly apart from putting a link on the premium listing.
UPDATE 2: Here is the output in html format:
<ul class="goto-leaders">
<li><span>**Clarkes Court Boatyard**</span><span>439 3939</span></li>
<li><span><i class="fa fa-arrow-circle-o-down"></i> Grenada Marine</span><span>443 1667</span></li>
<div id="panel-61" style="margin-top: 1rem;" class="goto-toggle hide" data-toggler=".hide">One-stop shop boatyard providing storage, repair and maintenance services.</div>
<li><span>Spice Island Marine Services</span><span>444 4257</li>
</ul>
etc
As you can see, it's only the permalink to the premium listing (marked in bold) that has not been actioned.
Your post was really confusing, the picture helps but i'm still not definite what the program flow should be.
What I think the flow should be is as follows:
If the post item has both a premium tag and a except -- put a link and a model on the box. --
If just a prem tag--- just a link
if just a excerpt-- just the model
any of the above are not true --- neither link nor model
The code to achieve this is below, see the && operator which basically means and so if ( A & B) are both true.
<?php if (get_post_meta($post->ID, 'goto_premium_listing ', true) && $post->post_excerpt): ?>
<!-- we place this first, if a post is both a premium lists and has a post excerpt this will run-->
<li>
<span>
<a href="<?php the_permalink(); ?>" data-toggle="panel-<?php echo $post->ID ; ?>">
<i class="fa fa-arrow-circle-o-down"></i>
<?php the_title();?>
</a>
</span>
<span>
<?php echo CFS()->get( 'goto_telephone' ); ?>
</span>
</li>
<?php elseif (get_post_meta($post->ID, 'goto_premium_listing ', true)): ?>
<!-- if the first condition is not true, but this one is this is the html that will be outputted -->
<!-- this is a premium post without a excerpt for the model -->
<li>
<span>
<?php the_title();?>
</span>
<span>
<?php echo CFS()->get( 'goto_telephone' ); ?>
</span>
</li>
<?php elseif($post->post_excerpt ):?>
<!-- if true, this html will be output.... there is what looks like a boostrap model control
should you wish a link in here replace '#' with <?php the_permalink(); ?>
-->
<li>
<span>
<a href="#" data-toggle="panel-<?php echo $post->ID ; ?>">
<i class="fa fa-arrow-circle-o-down"></i>
<?php the_title();?>
</a>
</span>
<span>
<?php echo CFS()->get( 'goto_telephone' ); ?>
</span>
</li>
<div id="panel-<?php echo $post->ID ; ?>" style="margin-top: 1rem;" class="goto-toggle hide" data-toggler=".hide">
<?php echo $post->post_excerpt; ?>
</div>
<?php else: ?>
<!-- this is the default when the if condition and subsequent elseif conditions are not true -->
<li>
<span>
<?php the_title();?>
</span>
<span>
<?php echo CFS()->get( 'goto_telephone' ); ?>
</span> <!-- missing span in original code -->
</li>
<?php endif;?>
You may have an issue with your model box, clicking on the link may be disabled by javascript to allow the model to display instead so if this is the case you will need to look into the js and ask a new Q
Using the Advanced Custom Fields plugin, I am trying to render an image on a single product page in WooCommerce.
I found this snippet of code from someone who is doing the same thing, but is rendering a text field instead. How would I change this to render an image?
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_top_category_desc', 1 );
function woocommerce_template_top_category_desc (){
$terms = get_the_terms( $post->ID, 'wc-attibute-class' );
if ( !empty($terms)) {
$term = array_pop($terms);
$text= get_field('txt-field', $term);
if (!empty($text)) {
echo $text;
}
}
}
So far I have this, but it's not working. The add action is correct because I was using it for something else, but starting at $terms is where I get lost and is obviously not right.
add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 );
function add_below_featured_image() {
$terms = get_the_terms( $post->ID, '496' );
if ( !empty($terms)) {
$term = array_pop($terms);
$image = get_field('banner_feedback', $term);
if (!empty($image)) {
echo $image;
}
}
}
Figured it out.
add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 );
function add_below_featured_image() {
if( is_single( pageidhere ) ) {
echo '<br><img src="urlhere">';
}
}
Steps to add custom gallery below featured image in woocommerce single product page(you can modify suitably to add just a single image):
Create an ACF for Image Gallery.
Copy the template for product-image.php from /plugins/woocommerce/templates/single-product to themes/your-child-theme/woocommerce/single-product/
Add the following code to product-image.php just before the closing div of
"images"
Add the images you want in the gallery to your single product admin page in the ACF custom field.
`
<?php $images = get_field('product_image_gallery'); ?>
<div class="mycustom_image_gallery">
<?php if($images): ?>
<div class="popup-gallery">
<?php foreach( $images as $image ): ?>
<a href="<?php echo $image['url']; ?>"
class="lightbox-link"
title="<?php echo $image['caption']; ?>"
data-description="<?php echo $image['description']; ?>">
<div class="image-wrap">
<img src="<?php echo $image['sizes']['thumbnail']; ?>">
</div>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
`
ok I am trying to retrive the category name of a woocommerce product displayed in a wordpress loop and use it as the class for a li also inside the loop i've tried this:
<div id="isocontent" class="products">
<ul><?php while (have_posts()) : the_post(); ?>
<li class="<?php echo $product->get_categories(); ?> box">
<?php echo the_post_thumbnail(); ?>
<p><?php the_title(); ?></p>
<span href="<?php the_permalink(); ?> " class="amount price" data-original="<?php echo get_woocommerce_currency(); ?><?php echo $product->get_price(); ?>" data-price="<?php echo $product->get_price(); ?>" title="Original price: <?php echo $product->get_price(); ?>"><?php echo get_woocommerce_currency(); ?><?php echo $product->get_price(); ?></span>
Add to Cart
</li>
<?php endwhile; ?>
</ul>
</div>
this being the part i'm trying to retrive the class with:
<li class="<?php echo $product->get_categories(); ?> box">
but it just outputs this:
<li class="<a href=" http:="" localhost.no="" fanny="" kategori="" interior-sv="" "="" rel="tag">
which does retrieve the category but also screws with the markup breaking the loop.
I've also tried this:
<li <?php post_class('box'); ?>
but because woocommerce uses taxonmys it retrives the tags but not the product category.
any help is much appriciated
Kind regards
Chris
It's not quite as easy as making a single call - get_categories() is designed to show an HTML representation of the product categories. The product categories are actually a custom taxonomy, so you have to use get_the_terms() to get at it.
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_id = $term->term_id;
$category_name = $term->name;
$category_slug = $term->slug;
break;
}