skip to Main Content

On my Woocommerce site, I have 2 different product attributes (artist and type). I want to execute some code when on a attribute archive page containing one of those attributes and their value. For example:

on site.com/artist/rembrandt/ showing all products made by Rembrandt (pa_rembrandt), I want to add the text "please see all painings by Rembrandt" etc. This is what I tried so fare:

<?php add_action( 'woocommerce_archive_description', 'artist_description' ); 
function artist_description() { 
if (is_product_taxonomy() && is_tax(pa_artist)) {

echo "Please see all paintings made by";
echo is_tax(pa_artist));
echo ".";
}
} ?>

But that clearly is not working. When I remove the && is_tax(pa_artist) the code does show the text "Please see all paintings made by." but without the actual painter and turns up on every archive page. Who can help me? Thanks in advance.

3

Answers


  1. Chosen as BEST ANSWER

    I found a solution by using (and stripping) the title and changing the code to this:

    function artist_description() { 
    if (is_product_taxonomy() & is_tax('pa_artist')) {
    
    echo "All paintings made by " ;
    $artist = sprintf(__('%1$s'), single_term_title('', false));
        echo $artist;
        echo ". </br>";
    }
    }
    

  2. add_action('woocommerce_archive_description', 'artist_description');
    
    function artist_description() {
        if (check_is_wc_attribute('Artist')) {
    
            echo "Please see all paintings made by";
        }
    }
    
    function check_is_wc_attribute($attribute_name) {
    
        /**
         * Attributes are proper taxonomies, therefore first thing is 
         * to check if we are on a taxonomy page using the is_tax(). 
         * Also, a further check if the taxonomy_is_product_attribute 
         * function exists is necessary, in order to ensure that this 
         * function does not produce fatal errors when the WooCommerce 
         * is not  activated
         */
        if (is_tax() && function_exists('taxonomy_is_product_attribute')) {
    
            return taxonomy_is_product_attribute($attribute_name);
        }
        return false;
    }
    
    Login or Signup to reply.
  3. Try this code below. It will add the text on every archive page/ listing page in products grid after product title. This text will show on specific products in which you have added this attribute.

    function artist_description() {
        global $product;
        $artist_val = $product->get_attribute('pa_artist');
        if ($artist_val) {
            echo "Please see all paintings made by ";
            echo $artist_val;
            echo ".";
        }
    }
    add_action( 'woocommerce_after_shop_loop_item_title', 'artist_description' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search