最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Only show size attributes in product box woocommerce when available

programmeradmin0浏览0评论

First of all I want to say I have little coding experience. I want to show available sizes in the productbox on the shop page. I got this working after some searching around here. Now I want to only display the sizes when sizes are available. If there are no size attributes available I would like the text: No sizes available or no text at all.

I got the following code now:

add_action('woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5);


function display_size_attribute() {

    global $product;

    if ($product->is_type('external')) {

        $taxonomy = 'pa_maat';

        echo "Maten:";
        echo '<span class="attribute-size">' 
                . $product->get_attribute($taxonomy) 
        . '</span>';

    }

}

First of all I want to say I have little coding experience. I want to show available sizes in the productbox on the shop page. I got this working after some searching around here. Now I want to only display the sizes when sizes are available. If there are no size attributes available I would like the text: No sizes available or no text at all.

I got the following code now:

add_action('woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5);


function display_size_attribute() {

    global $product;

    if ($product->is_type('external')) {

        $taxonomy = 'pa_maat';

        echo "Maten:";
        echo '<span class="attribute-size">' 
                . $product->get_attribute($taxonomy) 
        . '</span>';

    }

}
Share Improve this question edited Nov 13, 2019 at 11:28 Kaperto 4783 silver badges10 bronze badges asked Nov 13, 2019 at 10:14 SalespottersSalespotters 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

to test is the text is not empty, try that :

function display_size_attribute() {

    global $product;

    if ($product->is_type('external')) {

        $taxonomy = 'pa_maat';

        $text = $product->get_attribute($taxonomy);

        if (!empty($text)) {
            echo "Maten:";
            echo '<span class="attribute-size">' 
                    . htmlspecialchars($text) 
            . '</span>';
        }

    } // END if ($product->is_type('external')) {


} // END function display_size_attribute() {
发布评论

评论列表(0)

  1. 暂无评论