Get order title(s) in Woocommerce - php

I'm trying to get the names of the ordered products through my functions.php file with a loop. Heres's my code:
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
}
And then i call the title like this:
$_product->post_title
This works, it returns me the name of the product I ordered. The thing is when i have 2 or more products it still returns me 1 name. How can i make it so it returns all the names in the cart.

The new syntax in woocommerce relative to cart is made with WC() without any need of calling global woocommerce;
So your code will be this:
$products_in_cart= array();
$products_post_title_in_cart = array();
$products_ids_in_cart= array();
foreach(WC()->cart->get_cart() as $cart_item) {
$products_in_cart[] = $cart_item['data']->post;
$products_post_title_in_cart[] = $cart_item['data']->post->post_title;
$products_ids_in_cart[] = $cart_item['product_id'];
}
// The first product (or item of the cart)
$_product = $products_in_cart[0]; // product post data
$product_id = $products_ids_in_cart[0]; // product ID
$products_post_title_in_cart[0] // product post title
// The Second product (or item of the cart)
$_product = $products_in_cart[1]; // product post data
$product_id = $products_ids_in_cart[1]; // product ID
$products_post_title_in_cart[1] // product post title
// etc … for all other products you increase the key of the arrays to get the correct values

<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$arr_product=array();
foreach($items as $item => $values) {
$arr_product[]= $_product->post_title;
}
print_r($arr_product,true); // echo print_r(); thats why get 1
?>

Try this code, Its returns all the names in the cart.
global $woocommerce;
$cart_item = $woocommerce->cart->get_cart();
echo "<pre>";
print_r($cart_item);
exit();

Related

echo product selected quantity woocommerce

I want to display added product quantity in cart in single product page as text image
This is currently what i'm using in functions.php, I want to display the quantity number between.
add_action('woocommerce_before_add_to_cart_form', 'gripsquantity', 5);
function gripsquantity(){
echo 'Choose ';
echo 'Grips';
}
Thank you
Get cart object
$items = WC()->cart->get_cart();
Loop through cart items
foreach( $items as $item => $values ) {
if( $values ['product_id'] === $yourProductId ){
// Get product qty in cart
$quantity_in_cart = $values['quantity'];
}
}

WooCommerce user email and product meta data issue [duplicate]

I'm currently working on a WooCommerce theme and attempting to add a sidebar to the product detail page.
I've been able to get the sidebar added (specifically, this one: http://woocommerce.wp-a2z.org/oik_file/templatescontent-widget-product-php/)
Now, I'm trying to figure out how to add a class of "active" to the currently selected product and can't seem to figure it out?
In other words, how do I do something along the lines of if the current product id is equal to the product id in the sidebar add class="active" to the li?
I've made numerous searches and haven't been able to come up with anything useful, so I'm turning here.
Any help greatly appreciated!!
2017 Update - since WooCommerce 3:
global $product;
$id = $product->get_id();
Woocommerce doesn't like you accessing those variables directly. This will get rid of any warnings from woocommerce if your wp_debug is true.
If the query hasn't been modified by a plugin for some reason, you should be able to get a single product page's "id" via
global $post;
$id = $post->ID
OR
global $product;
$id = $product->id;
EDIT: As of WooCommerce 3.0 this needs to be
global $product;
$id = $product->get_id();
Since WooCommerce 2.2 you are able to simply use the wc_get_product Method. As an argument you can pass the ID or simply leave it empty if you're already in the loop.
wc_get_product()->get_id();
OR with 2 lines
$product = wc_get_product();
$id = $product->get_id();
Retrieve the ID of the current item in the WordPress Loop.
echo get_the_ID();
hence works for the product id too. #tested #woo-commerce
The correct method is:
global $product;
$id = $product->get_id();
Save the current product id before entering your loop:
$current_product = $product->id;
Then in your loop for your sidebar, use $product->id again to compare:
<li><a <? if ($product->id == $current_product) { echo "class='on'"; }?> href="<?=get_permalink();?>"><?=the_title();?></a></li>
your can query woocommerce programatically
you can even add a product to your shopping cart.
I'm sure you can figure out how to interact with woocommerce cart once you read the code.
how to interact with woocommerce cart programatically
====================================
<?php
add_action('wp_loaded', 'add_product_to_cart');
function add_product_to_cart()
{
global $wpdb;
if (!is_admin()) {
$product_id = wc_get_product_id_by_sku('L3-670115');
$found = false;
if (is_user_logged_in()) {
if (sizeof(WC()->cart->get_cart()) > 0) {
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->get_id() == $product_id)
WC()->cart->remove_cart_item($cart_item_key);
}
}
} else {
if (sizeof(WC()->cart->get_cart()) > 0) {
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->id == $product_id)
$found = true;
}
// if product not found, add it
if (!$found)
WC()->cart->add_to_cart($product_id);
} else {
// if no products in cart, add it
WC()->cart->add_to_cart($product_id);
}
}
}
}

Get product category for every item of a WooCommerce order

I can retrieve almost every metadata of the order items but I want to retrieve the category of the items too.
My code now has this:
foreach ($order->get_items() as $item_key => $item_values) {
## Using WC_Order_Item methods ##
// Item ID is directly accessible from the $item_key in the foreach loop or
$item_id = $item_values->get_id();
## Using WC_Order_Item_Product methods ##
$item_name = $item_values->get_name(); // Name of the product
$item_type = $item_values->get_type(); // Type of the order item ("line_item")
$product_id = $item_values->get_product_id(); // the Product id
$product = $item_values->get_product(); // the WC_Product object
## Access Order Items data properties (in an array of values) ##
$item_data = $item_values->get_data();
$product_name = $item_data['name'];
$item_totaal = $item_data['subtotal'];
// Get data from The WC_product object using methods (examples)
$product_type = $product->get_type();
$product_price = $product->get_price();
}
Thought this would work but it doesn't:
$product_category = $product->get_category();
What line do I need?
The WC_Product method get_category() doesn't exist and I remember you that you can have many product categories set for a product.
There is multiple ways to get the product categories set in a product:
1) You can use the method get_catogory_ids() to get the product categories Ids (an array of terms Ids) like:
foreach ($order->get_items() as $item ) {
$product = $item->get_product(); // the WC_Product Object
$product_category_ids = $product->get_category_ids(); // An array of terms Ids
}
2) Or to get the product category names (an array of term names) you can use wp_get_post_terms() like:
foreach ($order->get_items() as $item ) {
$term_names = wp_get_post_terms( $item->get_product_id(), 'product_cat', ['fields' => 'names'] );
// Output as a coma separated string
echo implode(', ', $term_names);
}

Get last added to cart product id in Woocommerce [duplicate]

This question already has an answer here:
Get in WooCommerce cart the product ID of a cart item
(1 answer)
Closed 4 years ago.
I try all of code from google, but none of them works.
such as:
$items = WC()->cart->get_cart();
$product_id = end($items)['data']->post->ID;
For example,
shoes have two different colors: black and gray. I added black shoes, then gray shoes, I order black shoes in the end.
It should return black shoes's ID, but the code on the top will shows gray shoes's id.
I want last added product, not the last position product.
You can put the products into an array and pick the last one using the php end() function as follows:
global $woocommerce;
//get cart items
$items = $woocommerce->cart->get_cart();
$ids = array();
foreach($items as $item => $values) {
$_product = $values['data']->post;
//push each id into array
$ids[] = $_product->ID;
}
//get last product id
$last_product_id = end($ids);
//get product variation details
$variations = get_variation_data_from_variation_id( $last_product_id );
Then define get_variation_data_from_variation_id($item_id) as follows:
//function to get product variation
function get_variation_data_from_variation_id( $item_id ) {
$_product = new WC_Product_Variation( $item_id );
$variation_data = $_product->get_variation_attributes();
//return variation detail
return woocommerce_get_formatted_variation( $variation_data, true );
}
That means you probably access the cart Object before It's created, You will need to place that code inside 'wp' hook for example.
add_action('wp', function(){
$product_id = end( WC()->cart->cart_contents)['product_id'];
});
Edited
I think you were looking for product attribute IDs? Like green or blue etc.
$cart = WC()->cart->get_cart();
$variation = end($cart)['variation'];
// This will have ID, slug, count, etc.
echo json_encode( get_term_by( 'name', end($variation) , str_replace( "attribute_", "", key($variation))) );
If you are specific about you attribute like just size change it like this based on your ta
$cart = WC()->cart->get_cart();
$variation = end($cart)['variation']['attribute_pa_size'];
// This will have ID, slug, count, etc.
echo json_encode( get_term_by( 'name', $variation , 'pa_size') );

WooCommerce get product ID [duplicate]

I'm currently working on a WooCommerce theme and attempting to add a sidebar to the product detail page.
I've been able to get the sidebar added (specifically, this one: http://woocommerce.wp-a2z.org/oik_file/templatescontent-widget-product-php/)
Now, I'm trying to figure out how to add a class of "active" to the currently selected product and can't seem to figure it out?
In other words, how do I do something along the lines of if the current product id is equal to the product id in the sidebar add class="active" to the li?
I've made numerous searches and haven't been able to come up with anything useful, so I'm turning here.
Any help greatly appreciated!!
2017 Update - since WooCommerce 3:
global $product;
$id = $product->get_id();
Woocommerce doesn't like you accessing those variables directly. This will get rid of any warnings from woocommerce if your wp_debug is true.
If the query hasn't been modified by a plugin for some reason, you should be able to get a single product page's "id" via
global $post;
$id = $post->ID
OR
global $product;
$id = $product->id;
EDIT: As of WooCommerce 3.0 this needs to be
global $product;
$id = $product->get_id();
Since WooCommerce 2.2 you are able to simply use the wc_get_product Method. As an argument you can pass the ID or simply leave it empty if you're already in the loop.
wc_get_product()->get_id();
OR with 2 lines
$product = wc_get_product();
$id = $product->get_id();
Retrieve the ID of the current item in the WordPress Loop.
echo get_the_ID();
hence works for the product id too. #tested #woo-commerce
The correct method is:
global $product;
$id = $product->get_id();
Save the current product id before entering your loop:
$current_product = $product->id;
Then in your loop for your sidebar, use $product->id again to compare:
<li><a <? if ($product->id == $current_product) { echo "class='on'"; }?> href="<?=get_permalink();?>"><?=the_title();?></a></li>
your can query woocommerce programatically
you can even add a product to your shopping cart.
I'm sure you can figure out how to interact with woocommerce cart once you read the code.
how to interact with woocommerce cart programatically
====================================
<?php
add_action('wp_loaded', 'add_product_to_cart');
function add_product_to_cart()
{
global $wpdb;
if (!is_admin()) {
$product_id = wc_get_product_id_by_sku('L3-670115');
$found = false;
if (is_user_logged_in()) {
if (sizeof(WC()->cart->get_cart()) > 0) {
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->get_id() == $product_id)
WC()->cart->remove_cart_item($cart_item_key);
}
}
} else {
if (sizeof(WC()->cart->get_cart()) > 0) {
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->id == $product_id)
$found = true;
}
// if product not found, add it
if (!$found)
WC()->cart->add_to_cart($product_id);
} else {
// if no products in cart, add it
WC()->cart->add_to_cart($product_id);
}
}
}
}

Categories