skip to Main Content

I’ve created a do_action('after_product_tabs) hook that I hope to programmatically echo an image under specific product categories, but I’m running into a bug. I’m able to echo out a div as a test for the action hook, but my conditional logic is breaking. Any help would be very much appreciated:

Here’s the function:


function after_product_tabs() {
    do_action('after_product_tabs'); //creates the action hook
};

function display_prewired_image() {
    
    echo '<div class="test" style="clear: both;">'.$home_url.'</div>'; // This prints!
    
    if( has_term( 'strat_pickups', 'product_cat' ) ) {
        echo' <p>DEBUG ME</p>'; // This breaks
    }
}

add_action('after_product_tabs', 'display_prewired_image');

I’ve also Tried:

1.) Passing in Global Variable:

function display_prewired_image() {
    global $product;
    $prod_id = $product->get_id();

    if( has_term( 'strat_pickups', 'product_cat', $prod_id ) ) {
        echo' <p>DEBUG ME</p>';
    }
}

2.) Changing the conditional logic from product categories to is_single(), which works, so I know my problem is within the logic of strat_pickups:

function display_prewired_image() {
    global $product;
    
    if ( is_a( $product, 'WC_Product' ) ) {
        // Get product ID
        $prod_id = $product->get_id();
        
        if( is_single('354') ) {
            echo '<h1 style="red!important; clear: both;">DEBUG ME</h1>';
        }
    } else {
        echo 'No product found!';
    }
}
add_action( 'after_product_tabs', 'display_prewired_image' );

For more info, I’m customizing the tabs.php template in Woocommerce. Would that not have access to the $product variable?

2

Answers


  1. Chosen as BEST ANSWER

    thank you for your help. This was a bang your head against the wall moment. The product category was not strat_pickups, it was strat-pickups.


  2. Try this instead, global $product is missing

    function display_prewired_image() {
        global $product;
        
        if ( is_a( $product, 'WC_Product' ) ) {
            // Get product ID
            $prod_id = $product->get_id();
            
            if ( has_term( 'strat_pickups', 'product_cat', $prod_id ) ) {
                echo 'DEBUG ME';
            } else {
                echo 'Category not found!';
            }
        } else {
            echo 'No product found!';
        }
    }
    //add_action( 'after_product_tabs', 'display_prewired_image' );
    add_action( 'woocommerce_before_add_to_cart_form', 'display_prewired_image' );
    add_action( 'woocommerce_after_single_product', 'display_prewired_image' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search