Woocommerce - 无法在购物车项目的循环中显示图像

时间:2017-11-30 17:53:15

标签: php wordpress image woocommerce product

我在Woocommerce中有一个购物车项目循环,目前只显示名称,数量和价格。我也试图让图像缩略图显示,但它或者给我一个错误,页面根本没有加载,或者加载但它没有显示任何内容。

<?php
                global $woocommerce;
                $items = $woocommerce->cart->get_cart();
                $currency = get_woocommerce_currency_symbol();
                if (!$items) {
                    echo '<span class="no-items-mini">No items added</span>';
                }
                    foreach($items as $item => $values) { 
                        $_product = $values['data']->post; 
                        $link = get_permalink($_product);
                        echo "<span>";
                            echo $values['quantity']." x <a href='".$link."'>".$_product->post_title;
                        echo "<a/></span>";
                        $price = get_post_meta($values['product_id'] , '_price', true);
                        echo "  Price: ".$currency.$price."<br>";
                        $image = wp_get_attachment_image_src( $values['product_id'], 'single-post-thumbnail' );
                        echo "<img src=$image[0];/>";
                    }
                $total = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
                echo "<span class='mini-total'><b>Total: </b>".$currency.$total."</span>";
            ?>

1 个答案:

答案 0 :(得分:1)

我已重新访问您的代码,请尝试一下:

<?php

    if ( WC()->cart->is_empty() ) {
        echo '<span class="no-items-mini">No items added</span>';
    } else {
        $currency = get_woocommerce_currency_symbol();
        $cart_total = WC()->cart->cart_contents_total;
        foreach(WC()->cart->get_cart() as $cart_item ) {
            $_product = $cart_item['data'];
            $link = $_product->get_permalink();
            echo '<span>';
            echo $cart_item['quantity'].' x <a href="'.$link.'">'.$_product->get_title();
            echo '<a/></span>';
            $price = $_product->get_price();
            echo "  Price: " . $currency . $price . "<br>";
            echo $_product->get_image();
        }
    echo "<span class='mini-total'><b>Total: </b>". $currency . $cart_total ."</span>";
    }

?>

这次应该可以工作。