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.
Related
I have this php code and it displays a button in the single product page. Everything works fine but the button generated from the shortcode is displayed in EVERY product category.
I want to display the button from shortcode ONLY in a specific product category.
<?php
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote()
{
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
?>
I tried this but the site crashed
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote()
if ( in_category( $cat 'category-slug' ) ) {
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
Let's see if I can help you!
First of all you're missing two curly brackets in your function and that is why your site crashed. You need to wrap your function with the curly brackets to avoid this.
What I think you want to do is to only execute the shortcode if it's a certain product category archive page. You can do this by using if( is_product_category() ) You need to define the category either by slug, id or title.
Try this function and change "example" to the title of your category. Let me know how it works out!
add_action ('woocommerce_after_add_to_cart_button', 'quote'); //action
function quote() {
if( is_product_category('example') ) { //change "example" to category title, id or slug
echo do_shortcode( '[wpb-quote-button id="14"]' ); //shortcode
}
}
I'm using WooCommerce on my Wordpress site as a catalogue (i.e. with the cart disabled) and I want to add widgets to the single product page sidebar based on if the product has a certain tag or not. I tried using the "Widget Logic" plugin and conditional tags, but even if I disabled all other plugins I could not get this to work anywhere on the site (i.e. including with a tagged post and the main sidebar - so not just with a tagged product in WooCommerce).
I then added the "PHP Code Widget" and added the code below in it:
<?php
if (is_product_tag( 'adexample1' )) {
echo "test1";
} elseif (is_product_tag( 'adexample2' )){
echo "test2";
} elseif (is_product_tag( 'adexample3' )){
echo "test3";
} elseif (is_product_tag( 'adexample4' )){
echo "test4";
} elseif (is_product_tag( 'adexample5' )){
echo "test5";
} else {
echo "<p>test6</p>";
}
?>
I've tested it with the tagged products and the other products, but they all return "test6" in the widget.
Can anyone see what I am doing wrong or is it possible that the product tags are not working/being recognised for some reason (permissions perhaps?). The issue is replicated in the default 2017 and basic WooCommerce themes, so it doesn't appear to be a theme issue?
Suggestions for another way of achieving this would also be appreciated?
Thanks
The conditional is_product_tag() will not work to get a product that has a product tag, as it's used to return true when viewing a product tag archive page.
To do achieve what you want to do you need to use WordPress has_term() conditional function with defined $taxonomy argument to 'product_tag', this way:
// Define BELOW your product tag ID, slug or name (or an array of values)
$term = 'adexample1';
$term2 = 'adexample2'; // ...
// HERE are your conditions with your code
if( has_term( $term, 'product_tag' ) ){
echo "test1";
} elseif( has_term( $term2, 'product_tag' ) ){
echo "test1";
} else {
echo "<p>test6</p>";
}
This should work as it works for product categories with defined $taxonomy argument to 'product_cat'…
In WooCommerce, My Category Listing page and product listing page are rendered from archieve-product.php ( By Default) . How to check if page is_shop() in functions.php? As is_shop function does not work in functions.php. I simply want to remove my sidebar from Category listing page not from product listing page.
When placed inside a hook, is_shop will work in functions.php
add_action( 'template_redirect', 'custom_template_redirect' );
function custom_template_redirect() {
if( is_shop() ) :
// code logic here
endif;
}
Here is a list of all WooCommerce conditionals
You can write a condition into "archive-product.php" for category page like,
$cate = get_queried_object();
if(is_product_category() && $cate->parent != 0 ){
// Write code here
//include sidebar here
}
By using this code this will check the page for product_category and also check for a parent.
call it using WordPress Hook pre get posts
add_action('pre_get_posts','nameTheFunction');
function nameTheFunction(){
if(is_shop()){
// your code here
}
}// function end here
Read more about pre get posts Hook
https://developer.wordpress.org/reference/hooks/pre_get_posts/
You can use function_exists
if( function_exists("is_shop") ) {
// call it or do something else
}
else {
// load it from somewhere
}
Official docs: https://secure.php.net/function_exists
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>
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');