I want to be able to show the current price and sale price if it exists within my WP_Query products loop. At the moment my code just replaces the current price with the sale price. I want an if statement that checks if the sale price is there or not. If it is I want to display both;
Here's my code;
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 4,
);
$query = new WP_Query( $args );
?>
<?php if($query->have_posts()): ?>
<div class="container" id="best_sellers">
<div class="row">
<?php while( $query->have_posts() ): $query->the_post(); ?>
<?php $product = wc_get_product(get_the_ID());?>
<div class="col-md-3">
<div class="cont">
<a href="<?php the_permalink(); ?>">
<div class="image_cont">
<?php the_post_thumbnail(); ?>
</div>
<p class="sku"><?php echo $product->get_sku(); ?></p>
<p class="title"><?php the_title(); ?></p>
</a>
<div class="quantity">
<p>Quantity: </p>
<button class="increment">+</button>
<input type="text" value="1" min="1" max="<?php echo $product->get_stock_quantity(); ?>">
<button class="decrement">-</button>
</div>
<p class="price"><?php echo wc_price( wc_get_price_including_tax( $product ) ); ?></p>
Add to Basket
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php endif; ?>
If you look at the price class you can see my code for getting the price.
update: I have been looking through the WooCommerce templates files and found this;
<?php if ( $product->is_on_sale() ) : ?>
This works, I just need to be able to grab both prices separately as at the moment
<?php echo wc_price( wc_get_price_including_tax( $product ) ); ?>
just gets overwritten by the sale price
Do you mean regular and sale prices?
You can work with below code but this will only with simple product, it won't work on product variation.
global $product;
if( $product->is_on_sale() ) {
$sale_price = $product->get_sale_price();
}
$regular_price = $product->get_regular_price();
I managed to find this
<?php echo $product->get_price_html(); ?>
It does everything I wanted without having to make an if statement
If you want to get the sale price with taxes, try this:
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price()) );
You will have to prefix the currency which can be achieved via get_woocommerce_currency_symbol()
Related
Following the guide here http://www.w3bdeveloper.com/how-to/how-to-get-regular-price-of-a-product-in-wordpress-woocommerce/
Using the following code to call the price inside the loop, the price is displaying the wrong price per item.
The first item has no price, the second item has the first items price, the third item has the second price and so on.
<div class='price'>
<?php echo $product->regular_price; ?>
</div>
If I use <?php echo $product->get_price_html(); ?> it displays correctly, but I'd like to be able to display the sale price and the regular price separately. Also for some reason, if I use this code before the button code, I get a fatal error.
The loop code is as follows:
<div class='post'>
<a class='oxy-post-image' href='<?php the_permalink(); ?>'>
<div class='oxy-post-image-fixed-ratio' style='background-image: url(<?php echo get_the_post_thumbnail_url(); ?>);'></div>
<div class='price-overlay'>
<?php echo $product->regular_price; ?>
</div>
</a>
<div class='post-wrapper'>
<a class='oxy-post-title' href='<?php the_permalink(); ?>'><?php the_title(); ?></a>
<div class='oxy-post-meta'>
<div class='cart-button'>
<?php global $product; echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '%s',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
$product->is_purchasable() ? 'add_to_cart_button' : '',
esc_attr( $product->get_type() ),
esc_html( $product->add_to_cart_text() ) ),$product ); ?>
</div>
</div>
</div>
</div>
A screenshot of the items in the loop:
Try to declare global $product before using echo $product->regular_price; so :
<div class='price-overlay'>
<?php global $product; echo $product->regular_price; ?>
</div>
I'm building a custom e-commerce website using woocommerce and I'm having some trouble fixing the "add to cart button". Whenever I add the multiple amounts in the input box/quantity box it only increments or adds one item to the cart. This only happens when I create a custom loop.
On the shop and single-product page, it works fine. If I add 10 items and press the add to cart button. It exactly adds 10 items to cart.
Here is the template I have been working.
<?php
/*
* Template Name: Home
*/
get_header(); ?>
<section class="full-width home-template">
<div class="full-width shop-section">
<div class="container">
<?php
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
);
$crate_products = new WP_Query ( $args );
if ( $crate_products->have_posts() ) : while ( $crate_products->have_posts() ) :
$crate_products->the_post();
?>
<div id="post-<?php the_ID() ?>" class="three columns product-post">
<?php // wc_get_template_part('content', 'product'); ?>
<figure class="featured-image">
<?php
//Display Product Thumbnail
$product_thumbnail = woocommerce_get_product_thumbnail();
?>
<a href="<?php the_permalink()?>" ><?php echo $product_thumbnail ?></a>
</figure>
<h2 class="product-price"><?php wc_get_template( 'single-product/price.php' ); ?></h2>
<span class="product-name"><?php the_title(); ?></span>
<?php // woocommerce_quantity_input(); ?>
<div class="add-to-cart-btn">
<?php woocommerce_template_loop_add_to_cart( $crate_products->post, $product ); ?>
<?php // do_action( 'woocommerce_after_shop_loop_item' ); ?>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
</section>
<?php get_footer(); ?>
What's confusing also is that the AJAX functionality works on the upsells template(up-sells.php) which is a template of woocommerce and it works fine.
<?php
/**
* Single Product Up-Sells
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/up-sells.php.
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product, $woocommerce_loop;
$upsells = $product->get_upsells();
if ( sizeof( $upsells ) === 0 ) {
return;
}
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__in' => $upsells,
'post__not_in' => array( $product->id ),
'meta_query' => $meta_query
);
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<div class="upsells products">
<div class="twelve columns">
<h2><?php // _e( 'You may also likeā¦', 'woocommerce' ) ?></h2>
</div>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<div id="post-<?php the_ID() ?>" class="three columns product-post">
<?php wc_get_template_part('content', 'product'); ?>
</div>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();
I have already tried applying the solutions from this developer
https://gist.github.com/claudiosmweb/5114131
and also this one
https://gist.github.com/webaware/6260468
But it still produces the same output. I really don't know why it only increments one item to the cart. I have checked the browser console for any errors and also have commented out some parts of the code to ensure or let you know that I have tried different methods or options in making the functionality work
Follow these steps
Uncomment woocommerce_quantity_input();
Check in Browser Console, if there are any errors in console or not. If yes, then please share your errors here.
If there are no errors then replace
woocommerce_template_loop_add_to_cart( $crate_products->post, $product );withprint_r(woocommerce_template_loop_add_to_cart( $crate_products->post, $product ));
and check whether it returns any data or not.
Also try uncommenting do_action( 'woocommerce_after_shop_loop_item' );
Here is an updated version.
<?php
/*
* Template Name: Home
*/
get_header(); ?>
<section class="full-width home-template">
<div class="full-width shop-section">
<div class="container">
<?php
global $product;
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
);
$posts = get_posts( $args );
foreach( $posts as $post ) :
setup_postdata( $post );
wc_setup_product_data( $post );
$product = wc_get_product( $post->ID ); ?>
<div id="post-<?php the_ID() ?>" class="three columns product-post">
<figure class="featured-image">
<a href="<?php the_permalink()?>" ><?php echo woocommerce_get_product_thumbnail(); ?></a>
</figure>
<h2 class="product-price"><?php wc_get_template( 'single-product/price.php' ); ?></h2>
<span class="product-name"><?php the_title(); ?></span>
<?php woocommerce_quantity_input(); ?>
<div class="add-to-cart-btn">
<?php woocommerce_template_loop_add_to_cart(); ?>
</div>
</div>
<?php endforeach; ?>
<script type="text/javascript">
(function($){
$(document).ready(function(){
$(document).on( "keyup", "input.qty", function(){
$(this).parent().next().find("a").attr( "data-quantity", $(this).val() );
});
});
})(jQuery);
</script>
</div>
</div>
</section>
<?php get_footer(); ?>
<?php // woocommerce_quantity_input(); ?>
Should be
<?php woocommerce_quantity_input(); ?>
I need to show the product's excerpt in the Orders page. I spent several hours trying to find a solution but nothing.
I already show the image & the title of the product (Thanks to #helgatheviking & this thread) , but I can't get the excerpt to show. This is my code:
<div id="order-column" class="my_account_orders">
<div class="wrap">
<?php
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( );
$order->populate( $customer_order );
foreach( $order->get_items() as $item_id => $item ) {
$product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$product->get_image();
$product->get_title();
}
$item_count = $order->get_item_count();
?>
<div class="orders-wrap">
<div class="preview">
<div class="image">
<div class="image-wrap"><?php echo $product->get_image(); ?></div>
</div>
<div class="bottom">
<div class="details">
<h3 class="name"><a title="View Order" href="<?php echo $order->get_view_order_url(); ?>"><?php echo $product->get_title(); ?></a></h3>
<h4 class="subtitle"><?php the_excerpt(); ?></h4>
</div>
</div>
The excerpt should appear in subtitle.
I have checked and tried the suggestions in these threads:
Woocommerce - description in products page
Adding a product description to the woocommerce cart page
This should do it. the_excerpt can only be used in combination with the_post() as it depends on the global $post object. But this pretty much reassembles what happens inside it.
<h4 class="subtitle"><?php echo apply_filters( 'the_excerpt', $product->post->post_excerpt ); ?></h4>
A bit late but always use this here:
get_the_excerpt( $product->get_id() );
The other solution from Johan Palmfjord will work but WooCommerce says that you never should access the product data directly.
I am calling product list on click of category button
as follow :
$id = $_GET['ServiceId'];
?>
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => $id, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div>
<li class="food_menu">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>
<div class="heading_food">
<h3 class="food_head"><?php the_title(); ?></h3>
<p class="food_head1">Description 1</p>
<p class="food_head1">Description 2</p>
</div>
<div class="price"><?php echo $product->get_price_html(); ?></div>
<div class="qty1" style="width:100%;margin-top:60px">
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</div>
</li>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
It shows me the list of product of that category, It shows a Add to cart button, I want is that if anyone click on this button then it should cahnge to added to cart and add to cart button should be disabled. How can I do this ?
Try this code.I hope, Its will help you..
HTML:
<input type="submit" value="Add to cart" class="add_cart">
Script:
jQuery('.add_cart').on('click', function () {
jQuery(this).prop('disabled', true); // for disabled button
jQuery(this).val('Added to cart'); // for change button as Added to cart
}
Thank You!!
I was make custom template for my WordPress site. So I need to show my product in my custom template dynamically. How can i do that?
This is my html code-->
<div class="all_content_shop">
<div class="col-md-3 single_all_c_s">
<div class="shop_product_inner">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<img src="http://jetsetbabies.com/wp-content/uploads/2006/07/100Juice_Organic_Apple_MAIN_v2.jpg" alt="Prduct Image" />
<div class="shop_product_inner_caption">
<h2>Product Title</h2>
<p>$200</p>
Add to Cart
</div>
</a>
</div>
</div>
</div>
================================================
Also i was make simple query but i did not found add to cart button dynamic code???
This is my query code---->
<div class="all_content_shop">
<?php $new_posts = new WP_Query(array(
'post_type' => 'product', //post of page of my post type
'cat' => 0, // category id, 0 for all categories.
'posts_per_page' => 12,
'offset' => 0, //how many posts you want to eliminate from the query
'orderby' => '', // order by title or date ?
'order' => 'DESC') // order as ASC or DESC
); ?>
<?php if ($new_posts->have_posts()) :
while ($new_posts->have_posts()) :
$new_posts->the_post(); ?>
<div class="col-md-3 single_all_c_s">
<div class="shop_product_inner">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>
<div class="shop_product_inner_caption">
<h2><?php the_title(); ?></h2>
<p><?php if ( ! defined( 'ABSPATH' ) ) exit; global $post, $product; ?> <?php echo $product->get_price_html(); ?></p>
Add to Cart
</div>
</a>
</div>
</div>
<?php endwhile;//Possibility to add else statement ?>
<?php wp_reset_postdata(); ?>
<?php else:?>
<p class="not_found">Sorry, The post you are looking is unavailable!</p>
<?php endif; wp_reset_query(); ?>
</div>
So my question is--->
My query is right or wrong?? if my query is wrong, please give me right query... Also if my query is almost right so please give me just "Add to cart" (http://prntscr.com/439lxj) button dynamic Ajax code...
Please help me...
Please help me...
Thanks !!!
I have made the necessary changes for the code to work. Make sure woocommerce add-to-cart.js or add-to-cart.min.js is enqueued in the page.
<div class="all_content_shop">
<?php wc_print_notices(); ?>
<?php $new_posts = new WP_Query( array(
'post_type' => 'product', //post of page of my post type
'cat' => 0, // category id, 0 for all categories.
'posts_per_page' => 12,
'offset' => 0, //how many posts you want to eliminate from the query
'orderby' => '', // order by title or date ?
'order' => 'DESC'
) // order as ASC or DESC
); ?>
<?php if ( $new_posts->have_posts() ) :
while ( $new_posts->have_posts() ) :
$new_posts->the_post();
global $product;
$product = get_product( get_the_ID() ); //set the global product object?>
<div class="col-md-3 single_all_c_s">
<div class="shop_product_inner">
<a href="<?php the_permalink() ?>">
<?php if ( has_post_thumbnail( get_the_ID() ) ) {
echo get_the_post_thumbnail( get_the_ID(), 'shop_single' );
} else {
echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" />';
} ?>
<div class="shop_product_inner_caption">
<h2><?php the_title(); ?></h2>
<p><?php echo $product->get_price_html(); ?></p>
<?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>
</div>
</a>
</div>
</div>
<?php endwhile;//Possibility to add else statement ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p class="not_found">Sorry, The post you are looking is unavailable!</p>
<?php endif;
wp_reset_query(); ?>