如何使用 PHP 在 WooCommerce 中显示产品属性

发布于 2025-01-12 21:06:49 字数 1106 浏览 1 评论 0原文

我想以自定义方式显示整个购物车,因此我必须访问:

  • 产品图片
  • 产品标题
  • 产品价格
  • 产品属性

到目前为止,我设法显示除属性之外的所有内容。这是我到目前为止的工作代码。


<?php
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    

        foreach($items as $item => $values) { 
            $_product =  wc_get_product( $values['data']->get_id() );
           
            $getProductDetail = wc_get_product( $values['product_id'] );
            echo $getProductDetail->get_image(); 

            echo "<b>".$_product->get_title() .'</b>  <br> Anzahl: '.$values['quantity'].'<br>'; 
            $price = get_post_meta($values['product_id'] , '_price', true);
            echo "  Price: ".$price."<br>";
            /*attributes*/
            /* $available_variations = $_product->get_available_variations();
                foreach ($available_variations as $key => $value) {
                    echo "$key $value";
                }*/
                    
        }
?>

关于属性的最后一部分不起作用有人可以帮助我并告诉我如何访问购物车中每个产品的属性并将其显示在下面吗?

I want to display the entire shopping cart in a customized way, therefore I have to get access to the:

  • Product Image
  • Product Title
  • Product Price
  • Product Attributes

So far I managed to display everything but the attributes. This is my working code so far.


<?php
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    

        foreach($items as $item => $values) { 
            $_product =  wc_get_product( $values['data']->get_id() );
           
            $getProductDetail = wc_get_product( $values['product_id'] );
            echo $getProductDetail->get_image(); 

            echo "<b>".$_product->get_title() .'</b>  <br> Anzahl: '.$values['quantity'].'<br>'; 
            $price = get_post_meta($values['product_id'] , '_price', true);
            echo "  Price: ".$price."<br>";
            /*attributes*/
            /* $available_variations = $_product->get_available_variations();
                foreach ($available_variations as $key => $value) {
                    echo "$key $value";
                }*/
                    
        }
?>

The last part about the attributes is not working can someone assist me and tell me how to access the attributes of each product in the cart and display them underneath?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宫墨修音 2025-01-19 21:06:49

试试这个代码

global $product;
echo wc_display_product_attributes( $product );

Try this code

global $product;
echo wc_display_product_attributes( $product );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文