I'm using WooCommerce on a WordPress site that I'm building and I need to be able to display a specific product's price throughout the site. Normally that wouldn't be an issue, but in this instance it's a product which has 2 variations, so I need to show both of them (e.g. £4.99 - £9.99). How can I retrieve these values and echo them out?
Put the following in your theme's functions.php file:
function so_28073705( $product_id ) {
$wc_product_variable = new WC_Product_Variable( $product_id );
$variation_price_html = $wc_product_variable->get_price_html( );
return $variation_price_html;
}
When you want to use it:
<?php echo so_28073705( <product_id> ); ?>
returns:
<span class="amount">$low-price</span>–<span class="amount">$high-price</span>
Related
I used the code here: Adding custom field to product category to add custom fields to my woocommerce category pages.
The used code includes usage to retrieve the data:
echo $productCatMetaTitle = get_term_meta($term_id, 'wh_meta_title', true);
echo $productCatMetaDesc = get_term_meta($term_id, 'wh_meta_desc', true);
Now, I'm not sure how I would pull the contents of each category's 'wh_meta_desc' into the 'woocommerce_before_main_content' hook space on the front-end of each relevant category page?
For example here: https://themirrorman.uk/category/wall-mirrors/
I have the equivalent code that would work to achieve this if I had a custom field named 'wh_meta_title' stored within a PRODUCT page. It would take the contents of 'wh_meta_desc' and place that content into the 'woocommerce_before_main_content' hook space on its product page:
function add_before_main_content() {
global $post;
$product_id = $post->ID;
$productCatMetaTitle = get_post_meta($product_id,'wh_meta_title',true);
if(!$productCatMetaTitle) return;
echo ''.$productCatMetaTitle.'';
}
add_action('woocommerce_before_main_content','add_before_main_content');
But how do I adapt this code so that it pulls through 'wh_meta_title' from its newly created CATEGORY page custom field? And then display its contents on the front-end within the 'woocommerce_before_main_content' hook space?
Would my code look something like this?
/** WooCommerce product category custom field - woocommerce_before_main_content - content **/
function add_before_main_content() {
global $post; //Is '$post' correct to apply this function to product category pages?
$term_id = $term->term_id;
$productCatMetaTitle = get_term_meta($term_id, 'wh_meta_title', true);
if(!$productCatMetaTitle) return;
echo ''.$productCatMetaTitle.'';
}
}
add_action('woocommerce_before_main_content','add_before_main_content');
Thank you.
UPDATE:
I've now found a suitable solution here: http://www.wpmusketeer.com/add-a-wysiwyg-field-to-woocommerce-product-category-page/
Works a treat:
Or visit to see it in action now: https://themirrorman.uk/category/wall-mirrors/
I'm building out custom landing pages for products in WooCommerce and I'd like to get the product price amongst other things in order to display them on the landing page.
Each landing page has some custom fields which allow the WP Admin to add in content, for the landing page as well as the product ID, which will then be used to generate the product price, checkout URL etc..
I can't get the wc_get_product(); to work with my custom field or a variable built off that. It only works when I use a straight ID. I think there's something I'm not understanding about how variables work within PHP. Here's my code.
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// This line is where the problem is...
$_product = wc_get_product('$courseID');
// If I replace the line above with this line
// $_product = wc_get_product('7217');
// everything works great, but that does not let
// each landing page function based on the custom fields where the user determines
// the product ID they are selling on that landing page.
// Get's the price of the product
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
Update
I get the following error using wc_get_product( $courseID ); or get_product( $courseID );:
Fatal error: Call to a member function get_regular_price() on a non-object in ...
Update related to your recent comment. The 2 ways to explore:
1) Instead of you should try to use to get the product object (avoiding the error):
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Get an instance of the product object
$_product = new WC_Product($courseID);
2) Alternatively if this doesn't work, you should try to use get_post_meta() function to get the product price (or any product meta data) this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Get the product price (from this course ID):
$course_price = get_post_meta($courseID, '_regular_price', true);
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This time you should get displayed the price with one or the other solutions.
Update: May be Also you need to convert $courseID to an integer variable.
Because you need to use your variable $courseID inside wc_get_product() (without the 2 ') function this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Here
$_product = wc_get_product( $courseID );
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This should work now.
You can try this out :
$courseID = the_field('course_id');
$product = get_product( $courseID );
I figured out the answer after running through the possible solution routes that #LoicTheAztec supplied in his response. None of these worked and so I assumed something else was up.
I use Advanced Custom Fields to add custom fields in the back end and I was using ACF's the_field() in order to create my variable. That is incorrect usage of that function as it's designed to display the field, (it's basically using php's echo). To work with these custom field's you need to use ACf's get_field() which is to use it to store a value, echo a value and interact with a value.
Once I switched to setting my $courseID to this..
$courseID = get_field('course_id');
Everything worked. My code worked, and all #LoicTheAztec's code approaches also worked.
Trying to achieve something that should be simple, but I've tried 3 approaches with multiple code variations and I just can't make it work. I'm trying to create a button that will appear in place of the "ADD TO CART" button on single product pages when the item is out of stock. Clicking the button will fire a popup contact form.
Is creating an add action in functions the right way to go, or should I replace the normal button with an if statement? I've tried both, so help with coding either would be greatly appreciated.
You can either hook into woocommerce_loop_add_to_cart_args using a filter in your functions.php or edit the template file directly by pulling it into your theme. Either way will require a bit of PHP.
If doing it in your functions.php, it would look something like this (untested but should send you down the right path):
<?php
add_filter( 'woocommerce_loop_add_to_cart_link', 'my_out_of_stock_button' );
function my_out_of_stock_button( $args ){
global $product;
if( $product && !$product->is_in_stock() ){
return 'Contact us';
}
return $args;
}
I don't know what your button code should actually look like or what other information you need to capture, but this is how you could override the "Add to Cart" button and replace it if out of stock.
UPDATE
LoicTheAztec brought up a great point - the filter provided only affects the button on the archive, category, tag overview pages - not the individual product pages. There are no hooks for the individual product page buttons BUT you can copy the templates to your theme and override them.
You'll want to look at the files in templates/single-product/add-to-cart. Use a similar if statement as above:
#simple.php
<?php if ( $product->is_in_stock() ) : ?>
// Standard WooCommerce code
<?php else: ?>
// Your button code
<?php endif; ?>
Just add below code in functions.php file of your enabled theme reference
add_action('woocommerce_after_shop_loop_item', 'themelocation_change_outofstock_to_contact_us', 1);
// for shop page
function themelocation_change_outofstock_to_contact_us() {
global $product;
if (!$product->is_in_stock()) {
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
//change the link to your contact us page
echo ' Contact Us ';
}
}
// for single page
add_filter('woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability($availability, $_product) {
// Change In Stock Text
if ($_product->is_in_stock()) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change Out of Stock Text
if (!$_product->is_in_stock()) {
$availability['availability'] = __(' Contact Us ', 'woocommerce');
}
return $availability;
}
I was looking for a way to show a contact button on bespoke products and
#ahwychkchih solution works great. One issue I had though is that schema markup will show as out of stock for those products which is not the case for beskpoke products is just they can't be purchased straight away so I've added this to force in_stock markup for my products. I'm aware that this solution would affect all products so you can always add a product id filter if needed
// Force In Stock schema markup
function fix_my_product_offers_schema ($markup_offer, $product) {
if (!$product->is_in_stock()) {
$markup_offer['availability'] = 'https://schema.org/InStock';
}
return $markup_offer;
}
add_filter('woocommerce_structured_data_product_offer', 'fix_my_product_offers_schema', 1, 2);
I am using Woo Commerce plugin and I want to display extra text for a specific product page.
The product ID seen in my body is:
single single-product postid-2624
So I tried the following code but it didn't work:
<?php
function ip_more_content() {
if ( is_single('2624'); ) {
echo 'show something';
}
else {
echo '';
}
}
add_action( 'woocommerce_product_thumbnails' , 'ip_more_content', 9 );
?>
How can I make WP do something based on a specific product id?
I guess that 'woocommerce_product_thumbnails' is running inside the loop, so you cab grab item id by simply using get_the_ID() function. So, edit your conditional like this:
if ( get_the_ID() == 2624 ) {
And it should work it out.
The code bellow, is to show a custom field I created to customize Woocommerce product category pages.. This code makes the custom field appear "after" the list of products. I need to make this code appear BEFORE the list of products... any hint on what I have to change in this bit of php code to make the custom field show before?
<?php
// Display details on product category archive pages
add_action( 'woocommerce_after_shop_loop', 'wpm_product_cat_archive_add_meta' );
function wpm_product_cat_archive_add_meta() {
$t_id = get_queried_object()->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$term_meta_content = $term_meta['custom_term_meta'];
if ( $term_meta_content != '' ) {
echo '<div class="woo-sc-box normal rounded full">';
echo apply_filters( 'the_content', $term_meta_content );
echo '</div>';
}
}
Thank you, I would really like to understand what makes the code appear after and not before, is the filter? in the last lines?
I found this bit of code at http://www.wpmusketeer.com/add-a-wysiwyg-field-to-woocommerce-product-category-page/
use below action to display custom fields before list of products
add_action('woocommerce_before_shop_loop','wpm_product_cat_archive_add_meta');