woocommerce if (is_product_category) - php

Need some help understanding why the code below isn't working. Copied from the woocommerce docs page https://docs.woocommerce.com/document/conditional-tags/
and inserted into the theme (avada child theme) functions.php
I've modified the original code only in the category names - changing 'shirts' to 'merch' etc - to suit the categories descriptors I have used, and added the action and function lines.
add_action('woocommerce_before_add_to_cart_form', 'print_below_desc');
function print_below_desc()
{
if ( is_product_category() )
{
if ( is_product_category( 'merch' ) )
{
echo 'Hi! Take a look at our sweet tshirts below.';
}
elseif ( is_product_category( 'parts' ) )
{
echo 'Hi! Hungry for some gaming?';
}
else
{
echo 'Check out our products below.';
}
}
When used as shown above, I get no text added to the product, however if I remove the if statements and just echo a text string it shows up. i.e. this works:
add_action('woocommerce_before_add_to_cart_form', 'print_below_desc');
function print_below_desc() {
echo 'Text string.';
}
Given I have just copy/pasted the woo code, I'm not sure why it wont work for me. It's possible that there is something with avada that interferes, but I don't know what to look for.
Thanks

Related

How to add HTML in "No products were found matching your selection" WooCommerce message

In WooCommerce, The "No products were found matching your selection." text appears when no products are found in the loop.
However, I want to add HTML in the text, so I use the gettext filter hook. Via str_ireplace I can easily overwrite the text:
function change_empty_category_text($translated) {
$translated = str_ireplace ('No products were found matching your selection.', 'sorry! we are not delivering products from this category to you pin code at the moment.', $translated);
return $translated;
}
add_filter( 'gettext', 'change_empty_category_text' );
However, when I want to add HTML in this text:
function change_empty_category_text($translated) {
$translated = str_ireplace ('No products were found matching your selection.', 'sorry! we are not delivering products from this category to you pin code at the moment.\r\nplease visit the following categories instead\r\n<a href = 'www.example.com/category1'>category 1</a>\r\n<a href = 'www.example.com/category2'>category 2</a>\r\n<a href = 'www.example.com/category3'>category 3</a>', $translated);
return $translated;
}
add_filter( 'gettext', 'change_empty_category_text' );
It has no result. Instead, I am getting the same exact text as written in the replacement.
Any advice?
The text you want to adjust can be found in templates/loop/no-products-found.php, so you could overwrite the template file
versus using the gettext filter hook, since this hook is executed multiple times on every page load.
However, there is another option. Since wc_no_products_found uses function_exits, see: /includes/wc-template-functions.php on line 3254-3262 #version 2.5.0
if ( ! function_exists( 'wc_no_products_found' ) ) {
/**
* Handles the loop when no products were found/no product exist.
*/
function wc_no_products_found() {
wc_get_template( 'loop/no-products-found.php' );
}
}
More info: What's "function_exists" in Wordpress
So instead of using the existing template file, which is called via wc_get_template() in the function, we are going to rewrite the function with our own function.
So you get:
function wc_no_products_found() {
echo '<p class="woocommerce-info" style="display:block;">' .
__( 'Sorry! we are not delivering products from this category to you pin code at the moment.<br>Please visit the following categories instead', 'woocommerce' )
. '<br>category 2<br>category 3</p>';
}
As you can see the output is fully customizable to your needs. CSS adjustments may be required, since this is theme dependent.
Code goes in functions.php file of your active child theme (or active theme).

Woocommerce | If page_id= .. { }

I am trying to apply specific functions to a certain page ID in Woocommerce.
However, it does not seem to apply. I have looked at both Wordpress and Woommerce conditional tags (https://docs.woocommerce.com/document/conditional-tags/).
The simple code I am testing is shown below, but even that is not working.
if ( is_product()) {
echo ' HALLO sdfdsfsdfsdfsdf ';
}
I have also tried with page_id and post_id but cannot get this to work.
I have added this code to my functions.php in my child theme.
Does anybody know to to write a specific function only for a specific product page (I know the value).
Method - 1
is_page() relies on a complete global $wp_query object. If you call it before the action template_redirect has been fired, it might be impossible to get that data.
add_filter( 'template_include', function( $template ) {
if ( is_product() )
echo 'HELLO';
return $template;
});
Method - 2
The WordPress/WooCommerce conditional tags won’t work until after the wp action is fired, making it the earliest point at which you can use the tags. You can view the entire list, and the order of the core WordPress actions at the Action Reference page.
add_action( 'wp', 'init' );
function init() {
if ( is_shop() ) {
echo 'HELLO, this works!';
}
}

Removing some text in main title on product category archives pages

On my WooCommerce web shop, I would like to remove Archive of : from main title on product category archives pages.
Here is a screenshot:
I have tried to use this code based on this answer,
But it doesn’t work:
function changing_category_archive_page_title( $page_title ) {
if(is_product_category()) {
$title_arr = explode(' :', $page_title);
$new_title = $title_arr[2];
if(!empty($new_title)) echo $new_title;
else echo $page_title;
}
}
add_filter( 'woocommerce_page_title', 'changing_category_archive_page_title', 10, 1 );
How can I do to remove Archive of : from title?
Thanks.
It seem that this 'Archive of : ' text is a customization of your theme, as it doesn't exist in classic WooCommerce. So in this case, it's normal that the code you are using doesn't work.
Without any guaranty, as I can't test it my self, you should try to use the WordPress gettex() function, as I think that this is an addition to main title, as some themes use to do:
add_filter( 'gettext', 'removing_specific_text_in_categories_page_titles', 10, 2 );
function removing_specific_text_in_categories_page_titles( $translated_text, $untranslated_text )
{
if ( 'Archive of :' == $untranslated_text ) {
$translated_text = '';
}
return $translated_text;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Add login form shortcode programatically to every published product page

I am running a wholesale shop on Woocommerce. Login is required to see the prices. This is set up and working properly. Now I wish to add a logon form on every product page to only show to visitors (not logged on users).
I am using the WooCommerce Catalog Visibility plugin. This plugin offers the functionality I described above, but my theme is somehow messing it up. The plugin author says to talk to the theme developer and the theme developer says to talk to the plugin author. So now I am trying to find a workaround.
First issue: The plugin comes with a shortcode [woocommerce_logon_form] that will display a logon form. I don't want to manually add this to every existing product since I have thousands of products on my site. I am looking for a way to get it in through the code for the product page layout.
I found this code (to be added to the functions.php) to work well:
// adds notice at single product page above add to cart
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo '<p id="rtrn">30-day return policy offered. See Terms and Conditions for details.</p>';
}
However, it will only show text. The short code won't work when added instead of the sample text.
Second issue: The short code shows the form even when the customer is already logged in.
I am currently using this nice code that shows or hides content depending on whether the user is logged in or not:
add_shortcode( 'access', 'access_check_shortcode' );
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return '';
}
That shortcode works perfectly for text, but not with other shortcodes.
So the combination of these short codes: [visitor][woocommerce_logon_form][/visitor] will not show the logon form to visitors. Instead it will only show them this as text [woocommerce_logon_form].
Please help! I am sure this is probably easily fixed by someone with coding skills.
I appreciate your effort to answer to this question. Keep in mind that my understanding of code is very limited and it would be great if you can also point out in which file to add or modify code.
To make your shortcode working in php code or in php/html code you need to use a native WordPress function do_shortcode() … You can use it with your shortcode for example in your 1st function this way:
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo do_shortcode('[woocommerce_logon_form]');
}
And this will work…
To see all the different hooks you can use instead of woocommerce_single_product_summary, please see this 2 templates code to chose in case a more convenient hook:
WooCommerce single-product.php template
WooCommerce content-single-product.php template
You can also add it the same way in one of your existing short codes, this way:
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return do_shortcode('[woocommerce_logon_form]');
return '';
}
And this will work too.
See as reference this answer: Change markup in WooCommerce shortcode output
So as you can see your problem is solved on both issues

Conditional if statement for a particular WP page

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.

Categories