0

I am using WooCommerce and a plugin called WooCommerce Store PDF Catalog.

In the frontend.php file, there is 2 actions. 1 is to create a downloadable pdf button on the category pages and downloadable pdf on the single product pages.

I want to remove the action on the single product pages and only apply it to a specific category single page.

So for example, I have 5 categories but I only want to apply that button to category #3.

This is what the code looks like

public function display_button_on_single() {
    global $wp_query;

    $post_ids = array();

    // gather all ids
    foreach( $wp_query->posts as $post ) {
        $post_ids[] = $post->ID;
    }

    $output = '';
    $output .= '<p class="wc-store-catalog-pdf-download">' . PHP_EOL;

    $output .= '<a href="#" class="wc-store-catalog-pdf-download-link button" target="_blank" download=""><i class="icon-file-pdf" aria-hidden="true"></i> ' . $this->link_label . '</a>' . PHP_EOL;

    $output .= '<input type="hidden" value="true" name="is_single" />' . PHP_EOL;

    $output .= '<input type="hidden" value="' . esc_attr( json_encode( $post_ids ) ) . '" name="posts" />' . PHP_EOL;

    $output .= '</p>' . PHP_EOL;

    echo $output;
}

The button gets generated in the woocommerce_single_product_summary hook.

1 Answer 1

0

You can use has_term to check if a post has a 'category' then you can return your function early if it does not or ideally you would only add this function to posts of your chosen 'category' however you have not shown code where you add this action so I won't comment on that.

Try something like:

public function display_button_on_single() {
    global $wp_query;

    if ( ! has_term('category_3', 'taxonomy_name')) {
        return;
    }

    $post_ids = array();
    // the rest of your function...

https://developer.wordpress.org/reference/functions/has_term/

1
  • It's a plugin from WooCommerce and it auto adds those button via the function they wrote above which is why I'm having a difficult time getting it to only display on a certain category. I can't overwrite the file as it will be overwritten in future updates and I can't exactly copy the file to my themes folder. Does that information help?
    – Amy Ling
    Oct 10, 2017 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.