Home > Net >  Woocommerce | display different text on product page if product has tag "xyz"
Woocommerce | display different text on product page if product has tag "xyz"

Time:05-29

I want to put a text or html under the short product description
Normally I can put any text in the short-description.php file But I want this text to be displayed only when my product has a Special tag and also the user is not registered

For example, when my product has a "Free" tag, show the user the following text in short description

Please register to purchase the product for free
And when the user is registered, do not show the text to him

Thanks in advance for your help

CodePudding user response:

function add_text_after_excerpt_single_product( $short_description ) {
    global $product;
    $terms = get_the_terms( $product->get_id(), 'product_tag' );

    foreach ( $terms as $term ) {

        if ( $term->name == 'Free' && ! is_user_logged_in() ) {
            $short_description .= 'Please register to purchase the product for free';
        }

    }

    return $short_description;
}
add_filter('woocommerce_short_description','add_text_after_excerpt_single_product', 20, 1);

CodePudding user response:

Thanx For help
its worked
But I have another problem
That is, I want this code to be at the end of the description short See the image below

enter image description here

Items are displayed at the top now, but I want to be at the bottom The code I put in the short file may be wrong, please see the code in my short-description.php file below (woocommerce/single-product/short-description.php)
The code that shows the highlighted table information is inside the table

    <?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

global $post;

$short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
if ( ! $short_description ) {
    return;
}

?>
<div >
    <?php echo $short_description; // WPCS: XSS ok. ?>
</div>

<table >
<tbody >
    <?php if ( get_field( 'platform' ) ): ?>
        <tr>
            <td >Platform:</td>
            <td >
            <?php $terms = get_field('platform');
    if( $terms ): ?>
    <?php foreach( $terms as $term ): ?>
            <a  href="<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo esc_html( $term->name ); ?></a>
    <?php endforeach; ?>
    <?php endif; ?>
            </td>
        </tr>
    <?php endif; ?>
        <?php if ( get_field( 'version' ) ): ?>
        <tr>
            <td >3DsMax Version:</td>
            <td >
            <?php $terms = get_field('version');
    if( $terms ): ?>
    <?php foreach( $terms as $term ): ?>
            <a  href="<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo esc_html( $term->name ); ?></a>
    <?php endforeach; ?>
    <?php endif; ?>
            </td>
        </tr>
    <?php endif; ?>
            <?php if ( get_field( 'render' ) ): ?>
        <tr>
            <td >Render:</td>
            <td ><?php $terms = get_field('render');
    if( $terms ): ?>
    <?php foreach( $terms as $term ): ?>
            <a  href="<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo esc_html( $term->name ); ?></a>
    <?php endforeach; ?>
    <?php endif; ?>
            </td>
                    </tr>
    <?php endif; ?>
            <?php if ( get_field( 'format' ) ): ?>
        <tr>
            <td >Format:</td>
            <td >
            <?php $terms = get_field('format');
    if( $terms ): ?>
    <?php foreach( $terms as $term ): ?>
            <a  href="<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo esc_html( $term->name ); ?></a>
    <?php endforeach; ?>
    <?php endif; ?>
            </td>
        </tr>
    <?php endif; ?>
    <?php if ( get_field( 'size' ) ): ?>
        <tr>
            <td >Size:</td>
            <td ><span ><?php the_field('size') ?></span> <span >MB</span></td>
        </tr>
    <?php endif; ?>
    <?php if ( get_field( 'date' ) ): ?>
        <tr>
            <td >Date:</td>
            <td ><span ><?php the_field('date') ?></span></td>
        </tr>
    <?php endif; ?>

    <?php if ( get_field( 'material' ) ): ?>
        <tr>
            <td >Material:</td>
            <td >
            <?php $terms = get_field('material');
    if( $terms ): ?>
    <?php foreach( $terms as $term ): ?>
            <a  href="<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo esc_html( $term->name ); ?></a>
    <?php endforeach; ?>
    <?php endif; ?>
            </td>
        </tr>
    <?php endif; ?>
        <?php if ( get_field( 'style' ) ): ?>
        <tr>
            <td >Style:</td>
            <td >
            <?php $terms = get_field('style');
    if( $terms ): ?>
    <?php foreach( $terms as $term ): ?>
            <a  href="<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo esc_html( $term->name ); ?></a>
    <?php endforeach; ?>
    <?php endif; ?>
            </td>
        </tr>
    <?php endif; ?>
    <?php if ( get_field( 'polygons' ) ): ?>
        <tr>
            <td >Polygons:</td>
            <td ><span ><?php the_field('polygons') ?></span></td>
        </tr>
    <?php endif; ?>
</tbody>
</table> 

I want my show to look like the image below.

enter image description here

  • Related