douzhen1234 2014-02-17 21:44
浏览 59
已采纳

WooCommerce - 列出产品页面之外的购物车项目名称

I am trying to create a simple bulleted list of items that have already been added to the cart, which will later be passed through a gravity form field. Here is what I have come up with so far:

<?php global $woocommerce; 

      $items = $woocommerce->cart->get_cart();

      foreach($items as $item => $product_id) { ?>

            <li><?php echo $item; ?></li>

      <?php } ?>

I have 2 items added to the cart, and get a result of:

  • 747579165776fe1ba349237464ae0eed
  • f06a642b52d91adfa0168b737bd60db9

However, I am trying to grab the title of the products rather than the key.

  • 写回答

1条回答 默认 最新

  • doudu22272099831 2014-02-18 01:04
    关注

    You need to do this way:

    <?php global $woocommerce; 
    
      $items = $woocommerce->cart->get_cart();
      foreach($items as $item => $values) { ?>
            <? // print_r($item); ?>
            <? $_product = $values['data']->post; ?>
            <? echo $_product->post_title; ?>
      <?php } ?>
    

    You can access then to any post value, post_title, post_date, ID, etc. Uncomment the // print_r($item); and see what i mean. Also you can play with other data changing a bit the part with: $_product = $values['data']->post; For example to access the product ID, not the post ID, change the inside of foreach function to this:

    $_product = $values['data'];
    echo $_product->ID;
    

    Print $_product to see the rest of values available.

    In fact, always do a print_r to see what´s inside an array.

    That´s it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题