0

I am creating a website in WooCommerce, WordPress. Currently, I'm working on a feature which shows button named "LIVE TRANSMISSION" in the header when product custom field contains URL (only one product on the whole website can have this field not empty at a time), clicking button will redirect the user to the product page where live transmission is. I know how to do that on a product page and it shows correctly but how to show this button in a header for the whole website and redirect the user to the product which has unique transmission at the moment? What is the best method to "know" which product has this feature enabled? I can't figure it out.

<div class="top-buttons">
    <?php
         $transmissionURL = get_field('transmission_URL');
         if (!empty($transmissionURL)) {
            echo '<a href="URL-to-product-which-have-active-transmission" class="btn">LIVE</a>';
         }
     ?>
</div>
0

I would suggest two solutions.

Solution:1 Use WP_Query, if there are only few products, may be 10 or 20

Use WP_Query, loop through all the products, check if transmission_URL is not empty, if it is not empty, print the link and break from the loop.

The following code snippet goes inside header.php

$product_query = new WP_Query( array( 
  'post_type' => 'product' 
) );
if( $product_query->have_posts() ):
 while( $product_query->have_posts() ): $product_query->the_post();
   $transmissionURL = get_field('transmission_URL');
   if (! empty( $transmissionURL ) ) :
    echo '<a href="URL-to-product-which-have-active-transmission" class="btn">LIVE</a>';
    break; // come out of while loop entirely, cause we are done.
   else:
    continue; // skip to the next product, stay inside while loop.
   endif;
 endwhile;
endif;

Solution 2: Make use of categories.

If the product count is high or If you want to save some maintenance time,

Create and assign a new category active-transmission to product_cat taxonomy. Then you can just query for the product with category active-transmission. This also allows to track previously transmission-ed product easily.

$product_query = new WP_Query( array( 
  'post_type' => 'product',
  'posts_per_page' => 1, // pull out only one product from the category
  'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => 'active-transmission',
        ),
    ), 
) );
if( $product_query->have_posts() ):
 while( $product_query->have_posts() ): $product_query->the_post();
   $transmissionURL = get_field('transmission_URL');
   if (! empty( $transmissionURL ) ) :
    echo '<a href="URL-to-product-which-have-active-transmission" class="btn">LIVE</a>';
   endif;
 endwhile;
endif;

You can modify the If blocks based on your requirements.

1
  • Thanks. I used the second solution in order to optimise query. – Soothsayer92 Jul 7 '17 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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