A
A
Andrey Plyuta2022-01-10 16:21:23
WordPress
Andrey Plyuta, 2022-01-10 16:21:23

How to display the badge in the product card on the store page?

Hello, it is necessary to display such a badge on the store pages, more precisely on the store and categories page.
61dc316ad47fc533237210.png

It is necessary to display only if the product is in the Exclusive category, I have not figured out how to do this.
I did it a little differently. I added a checkbox meta field, and massively assigned values ​​for it using PW WooCommerce Bulk Edit.

And I display the box by checking this meta field.
Here is the code

<?php

add_action( 'woocommerce_product_options_sku', 'exclusive_product_badge' );
function exclusive_product_badge() {
  global $product, $post;
  echo '<div class="options_group">';
woocommerce_wp_checkbox( array(
        'id'            => '_exclusive_product_badge',
        'value'   => get_post_meta( get_the_ID(), '_exclusive_product_badge', true ),
        'label'         => 'Відображати бейдж Ексклюзив?',
        'description'   => 'Так',
     ) );
    echo '</div>';
}


add_action( 'woocommerce_process_product_meta', 'exclusive_product_badge_save', 10 );
function exclusive_product_badge_save( $post_id ) {
    	$product = wc_get_product( $post_id );
        $exclusive_product_badge_val = isset( $_POST['_exclusive_product_badge'] ) ? 'yes' : 'no';
      $product->update_meta_data( '_exclusive_product_badge', $exclusive_product_badge_val);
        $product->save();
}


add_action( 'woocommerce_after_shop_loop_item', 'ruta_custom_product_card_details', 6 );

function ruta_custom_product_card_details() {
    global $post;
    $exclusive_product_badge_value = get_post_meta( $post->ID, '_exclusive_product_badge', true );
      if ( $exclusive_product_badge_value ) {
?> 
            <div class="product-card__badges_exclusive">
                Exclusive
                
               </div>

        
<?php
}

}


But I want to make sure that if the product was in the exclusive category, then such a badge would be displayed, how to do this, I unfortunately did not understand. Maybe someone will tell?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2022-01-10
@motor4ikkk

add_action( 'woocommerce_after_shop_loop_item', 'ruta_custom_product_card_details', 6 );

function ruta_custom_product_card_details() {
  global $post;

  if ( has_term( [ 'exclusive' ], 'product_cat', $post->ID ) ) {
    printf( '<div class="product-card__badges_exclusive">%s</div>', __( 'Exclusive', 'text-domain' ) );
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question