获取购物车项目名称,数量所有细节woocommerce

Phi*_*ath 42 wordpress woocommerce

我正在尝试将woocommerce购物车物品发送给第三方运输工具.我需要将项目名称,数量和个别价格发送给第三方.怎么能实现这一目标?

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

   $_product = $values['data']->post; 
     echo $_product->post_title; 
} 
Run Code Online (Sandbox Code Playgroud)

如何获取项目名称,数量和价格?

Roh*_*ner 87

试试这个 :

<?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>";
        } 
?>
Run Code Online (Sandbox Code Playgroud)

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

<?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>";
        }
?>
Run Code Online (Sandbox Code Playgroud)


Jes*_*era 29

自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();
   ...
Run Code Online (Sandbox Code Playgroud)

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

  • 这是更好的解决方案,因为它遵循“WC 方式”的做事方式并且不直接访问 post_meta。 (3认同)
  • 这也比已接受的解决方案更好,因为它获取客户支付的产品的实际价格,其中包括任何基于角色或数量的折扣等。 (2认同)

Vin*_*ano 25

产品价格备注

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

某些插件自定义函数 (添加到活动主题的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;

}
Run Code Online (Sandbox Code Playgroud)

代替:

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();
}
Run Code Online (Sandbox Code Playgroud)

您可以获得的其他数据:

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
}
Run Code Online (Sandbox Code Playgroud)


Ras*_*sim 6

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

 global $woocommerce; 
    echo $woocommerce->cart->cart_contents_count;
Run Code Online (Sandbox Code Playgroud)

  • 这应该是现在(2018 年):`WC()-&gt;cart-&gt;get_cart_contents_count();` (5认同)