How to hide Woocommerce "out of stock" items price - wordpress

I'd like to display my out of stock items, but without price in WooCommerce. Is there any easy way to hide price of "out of stock" items?
Thanks!

Adding these to the CSS worked for me. The first one removes the price from the out of stock item's page and the second removes the price from the out of stock item in search results.
.outofstock .price{display:none}
.outofstock .amount{display:none}

Create a file price.php in /your-theme/woocommerce/single-product/ folder. Paste the following code.
$pricelabel = ""; - will be the variable that will display instead of the price if the stock quantity is 0.
If you use $pricelabel = ""; - this will remove the price. You can try $pricelabel = "SOLD OUT!"; or whatever message you want to display if you want.
I actually wrote this code to display a text message instead of a particular price. I just changed it to check stock quantity instead of price.
<?php
/**
* Single Product Price, including microdata for SEO
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $product;
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<p class="price"> <?php
$stockamount = $product->get_stock_quantity();
$price = $product->get_price_html();
$pricelabel = "";
if($stockamount == 0)
{
echo $pricelabel;
}
else
{
echo $price;
};
?>
</p>
<meta itemprop="price" content="<?php echo $product->get_price(); ?>" />
<meta itemprop="priceCurrency" content="<?php echo get_woocommerce_currency(); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
</div>

stick this in functions
add_filter( 'woocommerce_get_price_html', 'remove_price_ofs', 10, 2 );
function remove_price_ofs( $price, $product ) {
if ( ! $product->is_in_stock()) {$price = '';}
return $price;
}

In cases like this I use CSS.
I would typically go use Chrome's inspect element (or any code browser), find the class of the Out of Stock price (could be something like .outOfStockPrice).
Then I would use something like Simple Custom CSS to apply my custom CSS (so I do not have to look for a specific file: http://wordpress.org/plugins/simple-custom-css/)
And add this to your custom css:
.outOfStockPrice {
visibility: hidden;
}
For more info on hiding elements using CSS: http://www.kirupa.com/html5/hiding_things_using_css.htm

very simple, woocommerce still add outofstock class to the whole page so use that and than price
this will work
.outofstock .price{display:none}

Related

How can I display all posts from one custom post type into a select meta box in another?

So, I'm trying to do something a little complicated that's a bit above my current technical level when it comes to WordPress!
I have a custom post type called "Products". I want to be able to pull through every "post" from the Products post type into a select meta box on every other post type on the site, standard and custom alike.
Then, I want to be able to use the selection to pull through information onto the page in order to display it to the user.
I have no idea how I'd even begin doing this. I can make meta boxes and custom post types, I just don't know how to glue them all together!
I have created the same and here is the code which pulls out the post from one post-type and displays it in select tag of the post types.
You have to create a meta box and after that alone you need to pull out the posts from another post-type so that it will populate the post names in the select tag of the meta box created as follows.
I have created it for the destination_category. you can change the post_type and display place in-order to get your codes success.
<?php
//Creation of Meta Box for post type "destination_category" (Start)
add_action( 'admin_init', 'my_destination_category' );
//destination_sub_category_admin - is the required HTML id attribute
//Select Destination Sub Category - is the text visible in the heading of the meta box section
//display_destination_subcategory_meta_box - is the callback which renders the contents of the meta box
//destination_category - is the name of the custom post type where the meta box will be displayed
// normal - defines the part of the page where the edit screen section should be shown
// high - defines the priority within the context where the boxes should show
function my_destination_category() {
add_meta_box( 'destination_sub_category_admin','Select Destination Sub Category','display_destination_subcategory_meta_box', 'destination_category', 'normal', 'high');
function display_destination_subcategory_meta_box( $select_category ) {
// Retrieve Current Selected Category ID based on the Category Created
global $wpdb;
$selectcat="SELECT * FROM ".$wpdb->prefix."posts WHERE `post_type`='destination_category' AND `post_status`='publish' ORDER BY `ID` DESC";
$resultant = $wpdb->get_results($selectcat);
$rescount=count($resultant);
$category_selected_id = intval( get_post_meta( $select_category->ID, 'destination_category_id', true ) );
?>
<link rel="stylesheet" type="text/css" href="<?php echo plugins_url('css/metabox.css',__FILE__ ) ?>" />
<table>
<tr>
<td style="width: 150px">Select Category</td>
<td>
<select style="width: 100px" name="category_selection" id="meta_box_category" style="float:left; width:50%; !important">
<?php
if($rescount==0)
{?>
<option value="null">No Posts have been created</option>
<?php
}
else
{
// Generate all items of drop-down list
foreach($resultant as $singleresultant)
{
?>
<option value="<?php echo $singleresultant->ID; ?>" <?php echo selected( $singleresultant->ID, $category_selected_id ); ?>>
<?php echo $singleresultant->post_title; ?>
</option>
<?php
}
}
?>
</select>
</td>
</tr>
</table>
<?php
}
// Registering a Save Post Function
add_action( 'save_post', 'destination_admin_sub_category', 10, 2 );
function destination_admin_sub_category( $select_category_id, $select_category ) {
// Check post type for movie reviews
if ( $select_category->post_type == 'destination_category' ) {
// Store data in post meta table if present in post data
if ( isset( $_POST['category_selection'] ) && $_POST['category_selection'] != '' ) {
echo update_post_meta( $select_category_id, 'destination_category_id', $_POST['category_selection'] );
}
}
}
}
?>
I have provided here with a detailed Explanation of the code so that it will be useful for you to develop the code very easy.
hop so this will help you a lot in order to solve your problems.

Custom Woocommerce Search Result Page should look like shop layout

I am creating custom search feature in a theme. I have created a form on the homepage and with action set to a custom php search page. Form looks like this somewhat. I AM GETTING Class WP_Query not found Error. How to make it work?
The woo-search.php looks like this. I am using this as a template file. forms action attribute links to the page which has this template applied
<?php
/**
* The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
* Template Name: Search Page
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php
global $wp_query;
global $wpdb;
if (isset($_POST['submit'])) {
// echo "Submitted <br>";
$product_kinds = $_POST['product-kinds'];
echo $product_kinds;
$the_query = new WP_Query(array('category_name' => '2-wheeler-batteries'));
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
}else{
echo "Get Away";
}
?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
For demonstration purpose the query inside wp_query is static. I want to display the search result just like the shop page looks like, I mean with product image, then title, then price and add to cart button. With above code I will need to fetch all seperately and code it to look like the shop page. How do I fetch Woocommerce layout in this search results. What changes should I make in the while loop.

Display product variations on category page

How do I show all product variations on WooCommerce category pages?
I have set up a variable product with three variations. I want each variation (not the variable product itself) to be displayed as a separate product on the product category page. The user should be able to open a variable product and add it to the cart.
Is this possible?
Example : http://bennyhendrikx.be/product-categorie/clothing/page/2/
Add this code in youre functions.php
if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
function woocommerce_template_loop_add_to_cart() {
global $product;
if ($product->product_type == "variable" && (is_product() || is_product_category() || is_product_tag())) {
woocommerce_variable_add_to_cart();
}
else {
woocommerce_get_template( 'loop/add-to-cart.php' );
}
}
}
add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_single_variation', 5);
function woocommerce_template_single_variation() {
global $product;
if ($product->product_type == "variable" && (is_product_category() || is_product_tag())) {
echo woocommerce_variable_add_to_cart();
}
}
Hi You can just paste this code in your theme functions.php
here is code
/**
* Replace add to cart button in the loop.
*/
function iconic_change_loop_add_to_cart() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
add_action( 'woocommerce_after_shop_loop_item', 'iconic_template_loop_add_to_cart', 10 );
}
add_action( 'init', 'iconic_change_loop_add_to_cart', 10 );
/**
* Use single add to cart button for variable products.
*/
function iconic_template_loop_add_to_cart() {
global $product;
if ( ! $product->is_type( 'variable' ) ) {
woocommerce_template_loop_add_to_cart();
return;
}
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'iconic_loop_variation_add_to_cart_button', 20 );
woocommerce_template_single_add_to_cart();
}
/**
* Customise variable add to cart button for loop.
*
* Remove qty selector and simplify.
*/
function iconic_loop_variation_add_to_cart_button() {
global $product;
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<button type="submit" class="single_add_to_cart_button button"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>
<?php
}
Even after so many years this one is not so simple for woocommerce.
The above examples shows the parent products with available variations below.
You maybe want to show the variations as simple products.
It's better to use a plugin for this. The Woocommerce single variations by weLaunch https://www.welaunch.io/en/product/woocommerce-single-variations/
it's the best i found so far. It gives you the opportunity to even exclude one variation from the category page (Most of the times, your products have 2 variations color/size. You simple want to show only the color variation).
If you want to go with custom code. You have to keep in mind, the images, pagination, filters, product links with variation parameters.
Woocommerce works with parent products on its core.

WooCommerce: How to display fields from the "Product Add-ons" extension?

I'm using the [Product Add-ons][1] extension for WooCommerce, which allows custom fields for products. This automatically displays on the single product template.
By some trial and error with the single product template, it appears to hook in somewhere in woocommerce_single_product_summary (single_meta?).
I need to get this same set of form fields to display on the archive-product template (the list of products). In my case, this is a field for a card request and one for delivery date. Both are needed before the product can be added to the cart from the product archive. I'm not sure if there is a function I can call for this, if it involves some more advanced coding.
Per Pelmered's answer, I was able to get the add-on fields to show up with adding this to functions.php:
add_action( 'woocommerce_after_shop_loop_item_title', array($GLOBALS['Product_Addon_Display'], 'display'), 10);
1ST ATTEMPT
Then, the problem was that the product archive was not creating a form element, only an anchor link for the add to cart button. So I've tried manually putting in a form. Code from content-product.php:
<?php do_action( 'woocommerce_before_shop_loop_item' ); ?>
<h2><?php the_title(); ?></h2>
<?php // MANUALLY PUT IN FORM, BECAUSE HOOKS ONLY PUT IN A BUTTON AND NO FORM. NEEDED FOR ADD-ON FIELDS ?>
<form class="cart" method="post" enctype='multipart/form-data'>
<?php
/**
* woocommerce_after_shop_loop_item_title hook
*
* #hooked woocommerce_template_loop_rating - 5
* #hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
?>
<div itemprop="description">
<?php the_content(); ?>
</div>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<button type="submit" class="single_add_to_cart_button button alt"><?php echo $product->single_add_to_cart_text(); ?></button>
</form>
<?php //do_action( 'woocommerce_after_shop_loop_item' ); ?>
This worked, but kind of messes up the AJAX submission and flash messages. After clicking the add to cart button, the page appears to refresh, and then it looks like nothing happened. But when you go to another page, it displays the messages (and multiple can stack).
2ND ATTEMPT
So I saw that the original AJAX cart button (not within a form) was using a query string to send the product ID. So I am now attempting to tack on additional parameters by modifying the add to cart JS. This worked. What I did added as the accepted answer.
I've found a solution that works, using jQuery to add the form field values to the AJAX request. This requires overriding add-to-cart.js with your own version, so future updates to that file may need to be merged.
In functions.php
// display add-on fields in woocommerce_after_shop_loop_item_title
add_action( 'woocommerce_after_shop_loop_item_title', array($GLOBALS['Product_Addon_Display'], 'display'), 10);
// Our custom cart JS to allow for product add-on fields on product archive
add_action( 'wp_enqueue_scripts', 'load_woo_scripts', 9 );
function load_woo_scripts() {
wp_enqueue_script( 'wc-add-to-cart', get_template_directory_uri() . '/js/add-to-cart.js', array( 'jquery' ), WC_VERSION, true );
}
Duplicate /assets/add-to-cart.js from the woocommerce plugin folder, and add to the folder referenced in load_woo_scripts. Then add the following after the var date = { statement (line 21).
// =========================================================================
// ADD PRODUCT ADD-ON FIELDS TO SUBMISSION (to allow from product archive)
// Store name and value of fields in object.
var addonfields = {};
$(this).parent().find(".after-shop-loop-item-title :input").each(function() {
addonfields[this.name] = $(this).val();
});
// Merge new fields and existing data
data = $.extend(data, addonfields);
// Testing: final value of data
console.log( data );
// =========================================================================
This adds the names and values of all input fields within the .after-shop-loop-item-title div. I've added this div/class to my theme's woocommerce/content-product.php template:
<?php do_action( 'woocommerce_before_shop_loop_item' ); ?>
<h2><?php the_title(); ?></h2>
<div itemprop="description">
<?php the_content(); ?>
</div>
<div class="after-shop-loop-item-title">
<?php
/**
* woocommerce_after_shop_loop_item_title hook
*
* #hooked woocommerce_template_loop_rating - 5
* #hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
?>
</div>
<?php do_action( 'woocommerce_after_shop_loop_item' ); ?>
Note that in my case, I am only using a textarea and a text input. Neither of which changes the price. More advanced usage of the extension may require more work.
I really hope that WooCommerce makes this easier in the future...
It's impossible to give you an exact copy-paste solution without access to the code, but it should be possible to attach the same function that is being hooked.
The hook that adds this to the single product page probably looks like this:
add_action( 'woocommerce_single_product_summary', array($this, 'woocommerce_product_add_ons_something'));
To attach this action to the achive template as well you should probably be able to do something like this:
add_action( 'woocommerce_after_shop_loop_item_title', array('WC_Product_Add_On', 'woocommerce_product_add_ons_something'));
WC_Product_Add_On is the class name of the plguin and needs to be changed to match the class that has the function/method in the action. Put this code in the functions.php file in your theme.
See if it works. If not, give me more information and I can expand my answer.
If anyone want to display woocommerce product addons fields everywhere, you can do this:
$custom_field = get_post_meta( $product_id, '_product_addons', false );
foreach( $custom_field as $field => $value) {
foreach( $value as $val => $v) {
echo $v["name"]; // or other field that you want!
}
}
Enjoy!

WooCommerce: List Variations Available

I'm trying to display the variations of a products in the category page, something like
"Product Image"
"Product Title"
from $107 - $ 200 (I got this done)
Available in: 5lb / 10lb / 20lb (I'm trying to achieve this)
I read online and found this function but i couldn't get it to work, keep saying function does not exist.
<?php
/**
* Loop Price
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product;
?>
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?>
<?php
**if ($product->get_variation_attributes(size)) {
echo $product->get_variation_attributes(size);
}**
?>
What i wish to achieve is fairly simple, I just need to let customers know that there are different bag size of my products available, however a simple "From $100" as per custom woocommerce template does not let novice online shopper know that means there are other variations.
I've added Size attribute under each of my variation products like 5KG, 6KG, 7Lb, and hope to display them in front page in red.
I do not need sophiscated solutions like drop down list or whatsoever, just plain display if variation is available.
Hope Someone can point me to some direction.
Update: This is my current solution, however, If i can list down the variation available it will be a bonus.
if ( $product->max_variation_price && $product->max_variation_price !== $product->min_variation_price ) {
echo "<span style='font-size:75%; color: red; display: block;'>Available in Various Sizes</span>";
}

Resources