【问题标题】:Get cart item name, quantity all details woocommerce获取购物车商品名称、数量所有详细信息 woocommerce
【发布时间】:2015-04-19 01:03:30
【问题描述】:

我正在尝试将 woocommerce 购物车商品发送到第三方运输工具。我需要将商品名称、数量和单个价格发送给第三方。如何实现?

$items = $woocommerce->cart->get_cart();
  foreach($items as $item => $values) { 

   $_product = $values['data']->post; 
     echo $_product->post_title; 
} 

如何获取商品名称、数量和价格?

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    试试这个:

    <?php
        global $woocommerce;
        $items = $woocommerce->cart->get_cart();
    
            foreach($items as $item => $values) { 
                $_product =  wc_get_product( $values['data']->get_id()); 
                echo "<b>".$_product->get_title().'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
                $price = get_post_meta($values['product_id'] , '_price', true);
                echo "  Price: ".$price."<br>";
            } 
    ?>
    

    获取产品图片和常规及销售价格:

    <?php
        global $woocommerce;
        $items = $woocommerce->cart->get_cart();
    
            foreach($items as $item => $values) { 
                $_product =  wc_get_product( $values['data']->get_id() );
                //product image
                $getProductDetail = wc_get_product( $values['product_id'] );
                echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
    
                echo "<b>".$_product->get_title() .'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
                $price = get_post_meta($values['product_id'] , '_price', true);
                echo "  Price: ".$price."<br>";
                /*Regular Price and Sale Price*/
                echo "Regular Price: ".get_post_meta($values['product_id'] , '_regular_price', true)."<br>";
                echo "Sale Price: ".get_post_meta($values['product_id'] , '_sale_price', true)."<br>";
            }
    ?>
    

    【讨论】:

    • 嘿@Rohil_PHPBeginner,我怎样才能获得购物车中产品的所有属性。使用您的脚本,我得到了标题、数量和价格,但我也想要图像和实际价格。提前致谢
    • 你好,如果我想在我的联系表格 7 中发送它怎么办?
    • 我怎样才能得到每个数量的产品总价?我正在使用 get_item_total 来获取单价。那么有没有办法获得总价@Rohil_PHPBeginner?谢谢
    • 如何获取产品变化数据?
    • 在 WooCommerce 3 中,如果您直接访问这些属性,您会收到通知。 @JesúsCarrera 建议在下面的回答中使用get_title(),这是一个更好的选择。
    【解决方案2】:

    你可以这样获取产品名称

    foreach ( $cart_object->cart_contents as  $value ) {
            $_product     = apply_filters( 'woocommerce_cart_item_product', $value['data'] );
    
            if ( ! $_product->is_visible() ) {
                    echo $_product->get_title();
            } else {
                    echo $_product->get_title();
            }
    
    
         }
    

    【讨论】:

    • 像这样的过滤器旨在用于内部或辅助功能。获取产品的首选方法应该是 wc_get_product()。
    【解决方案3】:

    从 WooCommerce 2.1 (2014) 开始,您应该使用 WC 函数而不是全局函数。也可以调用更合适的函数:

    foreach ( WC()->cart->get_cart() as $cart_item ) {
       $item_name = $cart_item['data']->get_title();
       $quantity = $cart_item['quantity'];
       $price = $cart_item['data']->get_price();
       ...
    

    这不仅是干净的代码,而且比直接访问 post_meta 更好,因为它会在必要时应用过滤器。

    【讨论】:

    • 这是更好的解决方案,因为它遵循'WC方式'做事并且不直接访问post_meta。
    • 这也比公认的解决方案更好,因为它获得了客户支付的产品的实际价格,包括任何基于角色或数量的折扣等。
    【解决方案4】:

    这将仅显示购物车项目数。

     global $woocommerce; 
        echo $woocommerce->cart->cart_contents_count;
    

    【讨论】:

    • 这应该是现在(2018):WC()-&gt;cart-&gt;get_cart_contents_count();
    【解决方案5】:

    大多数情况下,您希望获取购物车中产品的 ID,以便您可以与其他一些逻辑进行比较 - 后端中的示例设置。

    在这种情况下,您可以从@Rohil_PHPBeginner 扩展答案并返回数组中的 ID,如下所示:

    <?php 
    
        function njengah_get_ids_of_products_in_cart(){
        
                global $woocommerce;
        
                $productsInCart = array(); 
                
                $items = $woocommerce->cart->get_cart(); 
                
                foreach($items as $item => $values) { 
                
                    $_product =  wc_get_product( $values['data']->get_id()); 
                    
                   /* Display Cart Items Content  */ 
                   
                    echo "<b>".$_product->get_title().'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
                    $price = get_post_meta($values['product_id'] , '_price', true);
                    echo "  Price: ".$price."<br>";
                    
                    /**Get IDs and in put them in an  Array**/ 
                    
                    $productsInCart_Ids[] =  $_product->get_id();
                }
               
               /** To Display **/ 
                  
               print_r($productsInCart_Ids);
               
               /**To Return for Comparision with some Other Logic**/ 
               
               return $productsInCart_Ids; 
        
            }
    

    【讨论】:

      【解决方案6】:

      产品价格说明

      购物车商品的价格可能与购物车商品的价格不同 产品(作为后元存储在数据库中)

      某些插件自定义功能 (添加到活动主题的functions.php)可以更改购物车商品的价格。

      如果您想确保将产品的价格添加到购物车中,您必须这样获取:

      foreach ( WC()->cart->get_cart() as $cart_item ) {
          // gets the cart item quantity
          $quantity           = $cart_item['quantity'];
          // gets the cart item subtotal
          $line_subtotal      = $cart_item['line_subtotal']; 
          $line_subtotal_tax  = $cart_item['line_subtotal_tax'];
          // gets the cart item total
          $line_total         = $cart_item['line_total'];
          $line_tax           = $cart_item['line_tax'];
          // unit price of the product
          $item_price         = $line_subtotal / $quantity;
          $item_tax           = $line_subtotal_tax / $quantity;
      
      }
      

      代替:

      foreach ( WC()->cart->get_cart() as $cart_item ) {
          // gets the product object
          $product            = $cart_item['data'];
          // gets the product prices
          $regular_price      = $product->get_regular_price();
          $sale_price         = $product->get_sale_price();
          $price              = $product->get_price();
      }
      

      您可以获得的其他数据:

      foreach ( WC()->cart->get_cart() as $cart_item ) {
      
          // get the data of the cart item
          $product_id         = $cart_item['product_id'];
          $variation_id       = $cart_item['variation_id'];
      
          // gets the cart item quantity
          $quantity           = $cart_item['quantity'];
          // gets the cart item subtotal
          $line_subtotal      = $cart_item['line_subtotal']; 
          $line_subtotal_tax  = $cart_item['line_subtotal_tax'];
          // gets the cart item total
          $line_total         = $cart_item['line_total'];
          $line_tax           = $cart_item['line_tax'];
          // unit price of the product
          $item_price         = $line_subtotal / $quantity;
          $item_tax           = $line_subtotal_tax / $quantity;
      
          // gets the product object
          $product            = $cart_item['data'];
          // get the data of the product
          $sku                = $product->get_sku();
          $name               = $product->get_name();
          $regular_price      = $product->get_regular_price();
          $sale_price         = $product->get_sale_price();
          $price              = $product->get_price();
          $stock_qty          = $product->get_stock_quantity();
          // attributes
          $attributes         = $product->get_attributes();
          $attribute          = $product->get_attribute( 'pa_attribute-name' ); // // specific attribute eg. "pa_color"
          // custom meta
          $custom_meta        = $product->get_meta( '_custom_meta_key', true );
          // product categories
          $categories         = wc_get_product_category_list(  $product->get_id() ); // returns a string with all product categories separated by a comma
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-15
        • 1970-01-01
        • 2021-05-16
        • 2019-09-23
        • 2018-06-03
        • 1970-01-01
        • 2018-02-10
        • 2014-08-14
        相关资源
        最近更新 更多