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.
Related
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'];
}
}
Hy guys.
I have three product in my cart. Let's try to imagine that the image is the cart and i have modify the quantity of product and then press update cart.
When i press update cart, my function runs, than prints all my item after modify, into ITEMS.json. There are all my items and with the quantity modify of the Shoes, but into my file there is also Hoodie with quantity = 3. How is it possible that i delete it? it and i print items after update cart.
add_action('woocommerce_after_cart_item_quantity_update','my_function');
function my_function()
{
global $woocommerce;
$items = $woocommerce->cart->get_cart();
file_put_contents('ITEMS.json', print_r($items, true));
}
Is there something to distinguish the products delete from product modify?
Is there a hook which returns the list of products deleted ?
According to the documentation, you can retrieve all needed information from the parameters of the hooked function:
function my_function( $cart_item_key, $quantity, $old_quantity ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
// Check that quantity is not equal to 0 and that the item exists in the cart
if($quantity && isset($items[$cart_item_key])) {
$content = '';
// TODO: Here you must fill $content with needed information from $items[$cart_item_key]
// Below, I add the new quatity to the $content string.
$content .= $quantity;
// Write in JSON file
file_put_contents('ITEMS.json', $content);
}
};
add_action( 'woocommerce_after_cart_item_quantity_update', 'my_function', 10, 3 );
I have setup a local Wordpress environment with WooCommerce. The goal is a buy-one-get-one of equal or lesser value scenario.
I have the cart displaying a single item per line. I have isolated the cart products by 'key' not 'product_id' which is necessary for the equal or lesser portion of the requirements. I have created an array of necessary 'key' and am matching that against the cart contents by 'key'. If the keys match, 'set_price('0.00');'
This works on the cart page and checkout page. It all works beautifully until I process the order (currently using COD option only). Then all of the prices reset to the regular price as it appears on the product page (the "regular price"). So the orders do not reflect the actual cost.
Below is the code I have:
function resetPrice( $cart_obj ) {
global $allFreeItems; //all items in cart (and relevant data) that need to be free
$comparison_ids = array(); //isolate keys for comparison
foreach ($allFreeItems as $key => $value) {
$comparison_ids[] = $allFreeItems[$key][1]; //get unique key for each line item
}
foreach ( $cart_obj->get_cart() as $cart_item ) {
// The corresponding product key of line item
$product_key = $cart_item['key'];
// Compare value of above to array of key's and change value
if (in_array($product_key, $comparison_ids)) {
$cart_item['data']->set_price( 0.00 );
}
// else
// {
// $cart_item['data']->get_price();
// }
}
add_action( 'woocommerce_after_checkout_form', 'resetPrice');
add_action( 'woocommerce_before_calculate_totals', 'resetPrice');
I thought perhaps I wasn't using the correct hook, but all examples point to woocommerce_before_calculate_totals. The only difference I see is the everyone keeps using ['product_id'] rather than how I am doing it by [key] Is there something that happens when I click the "place order" button that I am not seeing? Anyone know a way around this?
Wondering if anyone can help me resolve my problem here. I am looking for a way to apply a coupon code to a Single (least expensive) product item in a Cart. e.g. customer add 2 SG bats at price of 100 and 200 respectively.
I want to apply 30% coupon code to my first bat (rs 100 X .70 = 70 price) for the first bat since its a cheapest item in the cart. So customer will pay 270 rs for total purchase.
I have tried to add this coupon on the Woo commerce by product but it applies to all the products (300 X .70 = 210 price).
I tried different functions of Cart object to retrieve the each item pricing without any success.
global $woocommerce;
$lowestPrice=1000;
$myproduct_price=0;
foreach ( $cart_object->cart_contents as $key => $value ) {
$myproduct_price=$value['data']->price;
if($myproduct_price < $lowerPrice) $lowestprice=$myproduct_price;
}
this code is giving me error.
Basically there are 2 steps:
Get all cart product's price and store in one array
find minimum value from array
Complete Code: (Tested)
global $woocommerce;
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$product_price[] = get_option('woocommerce_tax_display_cart') == 'excl' ? $_product->get_price_excluding_tax() : $_product->get_price_including_tax(); /*Store all product price from cart items in Array */
}
}
$lowestprice = min($product_price); // Lowest Price from array
Hope it will help you :)
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.