H
H
HAtan2020-09-17 00:36:16
WordPress
HAtan, 2020-09-17 00:36:16

What is the best way to display text in the shopping cart depending on the product or shipping class?

I want to check the availability of goods with a certain delivery class in the cart and, depending on whether it is or not, display text. If there is this delivery class, output one text, if it is not there, then another.

add_action('woocommerce_cart_display_variable_text' , 'print_text_based_by_cart');

function print_text_based_by_cartt(){

    $class_slug = 'food-stock';
    foreach( WC()->cart->get_cart() as $cart_item ){
        if( $cart_item['data']->get_shipping_class() == $class_slug ){
            echo 'span>Текст 1 </span>';
        }
        else{
            echo 'span>Текст 2 </span>
        <span>Текст 3 </span>';
        }
    }
}


And call already in the trash to the block
<div class="cart_totals__item">
            <?php
                do_action('woocommerce_cart_display_variable_text');
            ?>
</div>


But I get:
Text 2
Text 3
Text 1
But I need something one thing, I hope tell me where the cant or some other way

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question