20i Domains

Woocommerce issue

Discussion in 'Scripts and Coding' started by bensd, Mar 28, 2021.

  1. bensd United Kingdom

    bensd Well-Known Member

    Joined:
    Jan 2007
    Posts:
    5,142
    Likes Received:
    73
  2. Domain Forum

    Acorn Domains Elite Member

    Joined:
    1999
    Messages:
    Many
    Likes Received:
    Lots
    articles.co.uk
     
  3. seemly

    seemly Well-Known Member

    Joined:
    Feb 2011
    Posts:
    1,381
    Likes Received:
    289
    what PHP version are you using, Ben?
     
  4. bensd United Kingdom

    bensd Well-Known Member

    Joined:
    Jan 2007
    Posts:
    5,142
    Likes Received:
    73
    I'm using 7.2
     
  5. seemly

    seemly Well-Known Member

    Joined:
    Feb 2011
    Posts:
    1,381
    Likes Received:
    289
    OK, cool.

    The code snippet that appears in your opening post, is that literally copy and pasted? As there is currently a syntax issue with it if that is the case?
     
  6. bensd United Kingdom

    bensd Well-Known Member

    Joined:
    Jan 2007
    Posts:
    5,142
    Likes Received:
    73
    I updated it earlier as I noticed an issue. I'm not sure whether you're seeing the revised version so this is the code:

    Code:
    add_action( 'woocommerce_before_main_content', 'remove_test_category_thumbnails', 10 );
    
    function remove_test_category_thumbnails() {
        if (is_product_category('test-category')) {
            remove_action('woocommerce_before_shop_loop_item_title' , 'woocommerce_template_loop_product_thumbnail' , 10);
        }
    }
    
    add_action( 'woocommerce_before_shop_loop_item_title', 'add_test_category_thumbnails', 10 );
    
    function add_test_category_thumbnails() {
        global $post;
        $testCatThumb = get_post_meta( $post->ID, 'step_dad_mug', true);
        if (is_product_category('test-category') && (isset($testCatThumb)) ) {
            echo wp_get_attachment_image( $testCatThumb );
        }
        else
            echo woocommerce_get_product_thumbnail();
        }
    }
     
  7. seemly

    seemly Well-Known Member

    Joined:
    Feb 2011
    Posts:
    1,381
    Likes Received:
    289
    The following function expects a post id. is that what you are passing it?
    Code:
    wp_get_attachment_image();
    as part of your if condition, you could check:
    Code:
    is_numeric($testCatThumb);
    I tend to create readable variables, rather than having horrible conditions you have to parse with your brain every time you read it:

    Code:
    function add_test_category_thumbnails() {
        global $post;
    
        $testCatThumb = get_post_meta( $post->ID, 'step_dad_mug', true);
        $isPostId = !empty($testCatThumb) && is_numeric($testCatThumb);
        $isValidAttachmentId = is_product_category('test-category') && $isPostId;
    
        $image = $isValidAttachmentId ? wp_get_attachment_image($testCatThumb) : '';
    
        echo !empty($image) ? $image : woocommerce_get_product_thumbnail();
    }
    
     
    • Like Like x 1
    • Friendly Friendly x 1
  8. bensd United Kingdom

    bensd Well-Known Member

    Joined:
    Jan 2007
    Posts:
    5,142
    Likes Received:
    73
    That seems to have worked, thank you very much for your help.
     
    • Winner Winner x 1
    • Friendly Friendly x 1
  9. seemly

    seemly Well-Known Member

    Joined:
    Feb 2011
    Posts:
    1,381
    Likes Received:
    289
    I'm glad it worked, and happy to have helped!
     
    • Like Like x 4