WooCommerce - Conditional has_term doesn't work anymore after updating - php

So I'm using the conditional of tho code Snippet from this example, with this thread code with success:
BUT It's no longer working after updating my WordPress Core and WooCommerce Plugin.
if ( is_product() && has_term( 'sample-category', 'product_cat' ) ){
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $products;
$product_link = get_permalink( $products->id );
$sample_link = substr($product_link, 0, -1) . '-swatch-card/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Order a Sample", "my_theme_slug" ) . '</a>';
}
}
Child Plugin still has the proper code in the function.php file.
How can I solve this issue please?
Thanks

Try this way may be, using $post global object and embedding the conditional inside your function:
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $post;
if ( has_term( 'collection', 'product_cat', $post->ID ) ) {
$product_link = get_permalink( $post->ID );
$sample_link = substr($product_link, 0, -1) . '-swatch-card/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Order a Sample", "my_theme_slug" ) . '</a>';
}
};
The conditional has_term() needs sometimes it's third argument to work… In function.php it can't find the current post So in this case is better to embed it inside the function after $post or $product global object.

Related

Change add to cart button and text based on WooCommerce product type

How to change a WooCommerce add to cart button in Product List loop but depending on the product type, like for example:
For products with Variations I want a text in add to cart button to: "Show product"
For Simple prodcuts "Show product"
For products Out of stock: "Unavailable"
I tried with below code but doesn't work:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
$button_text = __( "Out of stock", "woocommerce" );
return '<a class="view-product" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
if( ! $product->managing_stock() && ! $product->is_in_stock() ) {
return $button;
}
if( $product->is_type( 'variable' ) ) return $button;
}
Try the following instead:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Out of stock products
if( ! $product->is_in_stock() ) {
$button_text = __( "Unavailable", "woocommerce" );
}
// Simple and Variable products
elseif( $product->is_type( 'simple' ) || $product->is_type( 'variable' ) ) {
$button_text = __( "Show product", "woocommerce" );
}
// Other product types
else {
$button_text = add_to_cart_text();
}
return '<a class="view-product button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
Code goes in functions.php file of the active child theme (or active theme). It should work
To change woocommerce Book Now button I installed this plugin https://wordpress.org/plugins/button-customizer-for-woocommerce/ and added 2 filters to functions.php and finally, it works
add_filter( 'woocommerce_booking_single_check_availability_text', 'wooninja_booking_check_availability_text' );
function wooninja_booking_check_availability_text() {
return "your text";
}
add_filter( 'woocommerce_booking_single_add_to_cart_text', 'wooninja_woocommerce_booking_single_add_to_cart_text' );
function wooninja_woocommerce_booking_single_add_to_cart_text() {
return "your text";
}

Display shipping class name on WooCommerce single product pages

Need to display the product shipping class to product page! any ideas why this is not working?
add_action('woocommerce_single_product_summary', 'display_product_shipping_class', 15 );
function display_product_shipping_class(){
global $product;
$term = get_term_by( 'slug', $product->get_product_shipping_class(), 'product_shipping_class' );
if( is_a($term, 'WP_Term') && $term->name == $product_shipping_class ){
echo '<p class="product-shipping-class">' . $term->name . '</p>';
}
}
There are multiple mistakes in your code. Try the following instead:
add_action('woocommerce_single_product_summary', 'display_product_shipping_class', 15 );
function display_product_shipping_class(){
global $product;
$shipping_class = $product->get_shipping_class();
if( ! empty($shipping_class) ) {
$term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' );
if( is_a($term, 'WP_Term') ){
echo '<p class="product-shipping-class">' . $term->name . '</p>';
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Order Wordpress-attributes by global/backend order (orderby)

I'm trying to create a function, where all available colors (attributes/terms) gets attached to each product - on the category-page (product-listing). The code below works, however, the frontend-order is not identical with the backend-order. I really can't figure out, how I'm suppose to get the attributes sorted accordingly. Have tried changing menu_order to term_order aswell. This dosen't work either.
function ea_display_color_swatches_on_category() {
$html_swatch = '<div class="swatches-wrapper">';
$terms = wp_get_object_terms( get_the_ID(), 'pa_farve', array( 'orderby' => 'menu_order' ) );
foreach ( $terms as $term ) {
$hex_color = get_woocommerce_term_meta( $term->term_id, 'pa_farve_swatches_id_color', true );
$html_swatch .= '<a class="swatch-color-category tooltips"
title="' . $term->name . '"
style="background:' . $hex_color . '" ></a>';
}
// end color-swatches-wrapper
$html_swatch .= '</div>';
echo $html_swatch;
}
add_action( 'woocommerce_before_shop_loop_item', 'ea_display_color_swatches_on_category' );
Source
Have you tried with term_id? WordPress use to sort by creation date.

Override themes functions

In my theme there is a functions-template.php file with a lot of functions. One of them echoes the category description on the site.
function woocommerce_taxonomy_archive_description() {
if ( is_tax( array( 'product_cat', 'product_tag' ) ) && get_query_var( 'paged' ) == 0 ) {
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_image_src( $thumbnail_id, 'full' );
$description = apply_filters( 'the_content', term_description() );
if ( $image && yit_get_option( 'shop-category-image' ) == 1 ) {
echo '<div class="term-header-image"><img src="' . $image[0] . '" width="' . $image[1] . '" height="' . $image[1] . '" alt="' . $cat->name . '" /></div>';
}
if ( $description ) {
echo '<div class="term-description">' . $description . '</div>';
}
}
}
I want to echo another variable instead without messing with the files. Is there a way to "override" an existing function?
I've been fiddling a bit with mu-plugins etc but with no success.
I always get the Fatal error: Cannot redeclare woocommerce_taxonomy_archive_description() (previously declared in error when adding the same function in my custom functions file..
Yes it can be overridden from a child theme. You can override the function from your child themes functions.php file.
See more about child theme https://codex.wordpress.org/Child_Themes
WordPress loads child theme first and then the parent theme. So if you create a function with same name in your child theme, then the if condition with ! function_exists will will be false and hence there this function won't be declared.
If you want to override from a plugin then you've to declare the function in earlier execution. Try declaring it in a init hook.
add_action('init', 'theme_func_override', 5);
function theme_func_override(){
function override_func(){
//code goes here
}
}
Update
If the function isn't declared with function_exists() check within a if condition then you can't override it!

Add WP permalink to PHP function

The code below adds an image into my wordpress RSS feed. I am wanting to make it so that the image is automatically hyperlinked back to the corresponding post. The code is in my theme's functions.php
function wcs_post_thumbnails_in_feeds( $content ) {
global $post;
if( has_post_thumbnail( $post->ID ) ) {
$content = get_the_post_thumbnail( $post->ID ) . '<span class="text">' . $content . '</span>';
}
return $content;
}
add_filter( 'the_excerpt_rss', 'wcs_post_thumbnails_in_feeds' );
add_filter( 'the_content_feed', 'wcs_post_thumbnails_in_feeds' );
Can I change this so that the post_thumbnail is automatically wrapped with a link to the post?
How can I wrap the get_the_post_thumbnail( $post->ID ) part of the code with a link? Thanks
You can use the get_permalink function and pass it the post ID.
function wcs_post_thumbnails_in_feeds( $content ) {
global $post;
if( has_post_thumbnail( $post->ID ) ) {
$content = '' . get_the_post_thumbnail( $post->ID ) . '<span class="text">' . $content . '</span>';
}
return $content;
}

Categories