Woocommerce how to get specific product quantity in php code - php

I am new to developing with woocommerce, and I recently found a way to display all of the names of my products and also the quantity. However, I was only able to display ALL of my products, and not just each one individually. If I try and remove the foreach loop, I get an error stating:
Undefined index: data in /filepath of folder.
If anyone has ever been able to display a single quantity of a product in woocommerce, please let me know!
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
echo " ";
$_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>";
}`
I was able to display each of the name and quantity of each product. However, when I tried to reverse engineer the foreach loop, I recieved an error:
Undefined index: data in /filepath of folder.

Related

Woocommerce update stock by ID after payment

I created a custom product type for WooCommerce. With this product type it is possible to connect an other product that is exist by ID.
WooCommerce reduce the stock quantity automatically if an order is placed and the payment is successful.
For example I added a product with ID 4082 to the cart with a quantity from 3.
After place this order WooCommerce updated the stock from product 4082 with -3.
Ok back to my custom product type. As I said it is possible to connect another product by ID.
For example I connect product 4082 with product ID 10988.
If a customer add product 4082 to the cart and placed the order I want reduce the stock quantity from product ID 10988 and not from 4082.
<?php
add_action('woocommerce_checkout_order_processed', 'stocktest');
function stocktest($order_id){
$order = wc_get_order( $order_id );
$order_item = $order->get_items();
foreach( $order_item as $product ) {
//for the topic I programmed the IDs hardcoded
if($product->ID == '4082'){
wc_update_product_stock( 10998, $product['qty'], 'decrease', '' );
}
}
}
?>
I tried the code above and the stock from ID 10998 is correctly decreased but also the stock from ID 4082 is decreased.
Do I use the wrong hook? And how can I make the function correctly?
Hope somebody can help me with this.
Thanks a lot
Does wc_update_product_stock( 4082, $product['qty'], 'increase', '' ); not work?
That way, the stock should be adjusted for the 4082 product.
Also, there might be a potential issue with your snippet. The $product variable refers to the product object of 4082 product, not the 10998 product.
Maybe try something like this?
<?php
add_action('woocommerce_checkout_order_processed', 'stocktest');
function stocktest($order_id){
$order = wc_get_order( $order_id );
$order_item = $order->get_items();
foreach( $order_item as $product ) {
//for the topic I programmed the IDs hardcoded
if($product->ID == '4082'){
$connected_qty = get_post_meta( 10988, '_stock', true );
wc_update_product_stock( 10998, $connected_qty, 'decrease', '' );
wc_update_product_stock( 4082, $product['qty'], 'increase', '' );
}
}
}
?>
Instead of using the custom field query, you could use the WC_Product class:
$connected_product = wc_get_product(10998);
$connected_qty = $connected_product->get_stock_quantity();
I did some research on your question. WooCommerce called a lot of functions to update the stock af order / payment. If an order is placed the items become an status reduce_stock yes or no.
If is yes go further and update the stock of this item. After this WooCommerce created the e-mails and ordernotes.
All this functions worked together please be careful with changing the code in the file wc-stock-functions.php
If you want change and try something do this for example on a local testserver.
This are some functions in the file
<?php
wc_maybe_reduce_stock_levels();
wc_maybe_increase_stock_levels();
wc_reduce_stock_levels();
wc_trigger_stock_change_notifications();
?>

Display the product title by order number only in email notifications

I'm customizing my Woocommerce email templates now and right now I only have acess to the order number, I'd like to get the price and quantity for my order but I haven't managed to figure out how.
<?php
// Getting the order object "$order"
$order = wc_get_order( $order_id );
// Getting the items in the order
$order_items = $order->get_items();
// Iterating through each item in the order
foreach ($order_items as $item_id => $item_data) {
// Get the item quantity, something is wrong here..
$item_quantity = $order->get_item_meta($item_id, '_qty', true);
echo $item_quantity;
// Get the price, doesn't work either..
$item_total = $order->get_item_meta($item_id, '_line_total', true)
}
?>
The issue is that I'm unable to get the quantity and price which I can display in my order confirmation email that I'm customizing, I'm currently running woocommerce 3.2.5
As you have the $order object, you can get the product title this way:
<?php
// Loop through order items
foreach($order->get_items() as $items){
$product = $items->get_product(); // The product object
$product_name = $product->get_name(); // The product Name
$quantity = $items->get_quantity(); // The product Quantity
$line_total = $items->get_total(); // The line item total
// Display the product name
echo '<p>'.$product_name.'</p>';
// Display the product quantity
echo '<p>'.$quantity.'</p>';
// Display the product name
echo '<p>'.$line_total.'</p>';
// Get the raw output to check
echo '<pre>'; print_r(echo $items->get_data() ); '</pre>';
}
?>
Remember that an order can have many items (so different product names).
Related trhread: Get Order items and WC_Order_Item_Product in Woocommerce 3

woocommerce cart product names into dropdown

I'm trying to create a snippet at the checkout of woocommerce to take a dropdown list with the label course1 and add all product titles as selections.
as im new to php in general, im struggling to figure out how I would pull the names of the products in the cart.
any tips would be very much appreciated.
Thanks
You can get cart items from global $woocommerce object
global $woocommerce;
$items = $woocommerce->cart->get_cart();
echo "<select name='products-list'">;
foreach($items as $item => $values) {
$_product = $values['data']->post;
$price = get_post_meta($values['product_id'] , '_price', true);
echo "<option value=".$values['product_id'].">".$_product->post_title."-".$price."</option>";
}
echo "</select>";
Use this PHP snippet where you want to show dropdown list

How to pass first item unit price to dataLayer in cart?

I'm setting up a dataLayer in the WooCommerce cart trying to pass the first item value to it. The code below passes both the values as one variable and that's not what I want - I want an array to be built for each item in the cart.
One array for the prices, and one for the quantity.
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'firstItemUnitPrice': '<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$price = get_post_meta($values['product_id'] , '_price', true);
echo $price;
}?>'
});
</script>
The output from the code above is:
firstItemunitprice: 1144
There are two products in cart, one worth $11 and one $44.
I know how to separate them but I want them to be added as an array to the dataLayer. Help please?
I understand I need to loop through the cart, but I want each item price and each item quantity to be placed in a dataLayer.

Magento: Can't get attribute value for last item in cart

I'm running into a problem with getting attribute values for products that have been placed in a users cart.
I have the following code:
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('checkout/session');
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
foreach ($session->getQuote()->getAllItems() as $item) {
$_product = Mage::getModel('catalog/product')->load($item->getId());
$attributeValue = $_product->getAttributeText('availability');
echo $attributeValue;
}
And it works fine for all products in the cart except the very last one. For example, I'm trying to get the value of an "availability" attribute I've created that can have only 1 of the following values "Backorder", "Preorder", "Out of Stock". If I have 3 items in my cart I can get the correct values for the first 2, however for the last item it just displays "No".
I have double checked each item to make sure all attributes are set correctly and it happens with any number of items in the cart.
Hopefully it's just a stupid mistake on my part.
Any help would be appreciated.
Thanks.
SOLVED:
Changing:
$_product = Mage::getModel('catalog/product')->load($item->getId());
to
$_product = Mage::getModel('catalog/product')->load($item->getProductId());
Fixes the issue.
The problem is in that line:
$_product = Mage::getModel('catalog/product')->load($item->getId());
With $item->getId() you are getting ID of Mage_Sales_Model_Quote_Item and not Mage_Catalog_Model_Product. You have to use $item->getProductId() instead to get ID of actual product associated with current quote item.

Categories