Get cart nombre del artículo, cantidad todos los detalles woocommerce


Estoy tratando de enviar los artículos del carrito de woocommerce a la herramienta de envío de terceros. Necesito que el nombre del artículo, la cantidad y el precio individual se envíen al tercero. ¿Cómo se puede lograr esto?

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

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

¿Cómo obtengo el nombre del artículo, la cantidad y el precio?

Author: Jan Zyka, 2015-02-18

4 answers

Prueba esto:

<?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>";
        } 
?>

Para obtener la Imagen del Producto y el Precio Regular y de Venta:

<?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>";
        }
?>
 73
Author: Rohil_PHPBeginner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-10 07:41:13

Desde WooCommerce 2.1 (2014) debe usar la función WC en lugar de la global. También puede llamar a funciones más apropiadas:

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();
   ...

Esto no solo será código limpio, sino que será mejor que acceder directamente a post_meta porque aplicará filtros si es necesario.

 15
Author: Jesús Carrera,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-10 12:15:26

Esto solo mostrará el Número de artículos del Carrito.

 global $woocommerce; 
    echo $woocommerce->cart->cart_contents_count;
 2
Author: Rashid Wasim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-23 08:00:45

Puedes obtener el nombre del producto así

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();
        }


     }
 1
Author: Yasssine ELALAOUI,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-28 12:32:35