echo product selected quantity woocommerce - php

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'];
}
}

Related

Overwrite qty of simple product and variation product in the cart, if they are added in Woocommerce

I am the new developer to make the Woocommerce website. I found this useful code in another post. It works to overwrite the stock under product id.
If I have two variation products under the same product id, it will delete all the variation product under same product id in the cart. How can I amend the following code?
add_filter('woocommerce_add_to_cart_validation', 'remove_cart_item_before_add_to_cart', 1, 3);
function remove_cart_item_before_add_to_cart($passed, $product_id, $quantity) {
$already_in_cart = false;
foreach( WC()->cart->get_cart() as $key => $item ){
// Check if the item is already is in cart.
if( $item['product_id'] == $product_id ){
$already_in_cart = true;
$existing_product_key = $key;
break;
}
}
if( $already_in_cart ){
WC()->cart->remove_cart_item($existing_product_key);
}
return $passed;
}
For Example:
Cart
Product A (Blue) x 1 qty
Product A (Red) x 1 qty
What I want is if I try to add 2 qty of Product A (Blue), the cart will only overwrite the quantity of Product A (Blue) and keep the quantity of Product A (Red) remain unchanged.

Replace backorder notification with number of backorders for each cart item on WooCommerce cart page

I'm trying to edit the backorder notification on the cart page in WooCommerce so that the number of backorders show for each cart item (I'm using variations).
So if a cart item has a quantity of 7 but the stock is 5, the backorder should be 2. (This information is shown on order emails by default in Woocommerce but not on the cart page before the order is made).
The code below works as long as there is only 1 cart item in the cart. But if there is more than 1 cart item, the same number of backorders is returned for all cart items with backorders.
Does anyone know what I should change so that the backorder notification will display the correct number of backorders for each individual cart item that has backorders - no matter how many cart items with backorders there are in the cart?
I'm using the filter hook woocommerce_cart_item_backorder_notification
function backorder_info() {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Get an instance of the WC_Product Object
$product = $cart_item['data'];
// Get stock quantity
$stock_qty = $product->get_stock_quantity();
// get cart item quantity
$item_qty = $cart_item['quantity'];
// Calculate number of backorders
$backorder_qty = $item_qty - $stock_qty;
$html = '<p class="backorder_notification">' .__( " " .$backorder_qty. " i restordre","woocommerce").'</p>';
// return backorder quantity
return $html;
}
}
// Display backorder notification on cart page
add_filter( 'woocommerce_cart_item_backorder_notification', 'custom_cart_item_backorder_notification', 10, 2 );
function custom_cart_item_backorder_notification($html, $product_id) {
$html = backorder_info();
return $html;
}
As you can see the woocommerce_cart_item_backorder_notification filter hook contains the productID as the second argument.
Only this is the parentID, and therefore not so easy to determine for variable products which variant (variantID) is currently in cart.
That is why we are going to first ensure through the following code that not the parentID but the real productID is passed to the woocommerce_cart_item_backorder_notification filter hook
function filter_woocommerce_cart_item_product_id( $cart_item_product_id, $cart_item, $cart_item_key ) {
// Return real product ID instead of parent ID
$cart_item_product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
return $cart_item_product_id;
}
add_filter( 'woocommerce_cart_item_product_id', 'filter_woocommerce_cart_item_product_id', 10, 3 );
And then you get, to change the backorder_notification based on number of backorders
function custom_cart_item_backorder_notification( $html, $product_id ) {
// Get cart items quantities
$cart_item_quantities = WC()->cart->get_cart_item_quantities();
// Product quantity in cart
$product_qty_in_cart = isset( $cart_item_quantities[ $product_id ] ) ? $cart_item_quantities[ $product_id ] : 0;
// Get product
$product = wc_get_product( $product_id );
// Get stock quantity
$stock_qty = $product->get_stock_quantity();
// Still in stock
if ( $stock_qty >= 1 ) {
// Calculate number of backorders
$backorder_qty = $product_qty_in_cart - $stock_qty;
} else {
// Calculate number of backorders
$backorder_qty = $product_qty_in_cart;
}
// Output
$html = '<p class="backorder_notification">' . sprintf( __( '%s in backorder', 'woocommerce' ), $backorder_qty ) . '</p>';
return $html;
}
add_filter( 'woocommerce_cart_item_backorder_notification', 'custom_cart_item_backorder_notification', 10, 2 );

Changing WooCommerce coupon discount amount in cart

What I'm doing:
I am creating a coupon based promo. The coupon will discount 10% of the most expensive item in the cart that belongs to a specific product category.
The problem:
The code below successfully checks if the cart contains products from the specific category, and obtains 10% of the price of the most expensive product from that array to use for the discount. However, I can't get the coupon discount amount to change in the cart.
The code:
function change_coupon_price_for_promo( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
return;
}
// coupon code
$coupon_name = 'MYCOUPONNAME';
// if coupon is applied to cart...
if( $cart->has_discount( $coupon_name ) ) {
// Form array of products from the cart that belong to the specified category
$specProduct = array(); // array of specific products in cart
foreach ( $cart->get_cart() as $key => $cart_item ) {
$cart_item_id = $cart_item['product_id'];
$terms = wp_get_post_terms( $cart_item_id, 'product_cat' );
foreach ( $terms as $term ){
// gets product cat id
$product_cat_id = $term->term_id;
// gets an array of all parent category levels
$product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' );
// This cuts the array and extracts the last set in the array
$last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true);
if(in_array(3831, $last_parent_cat)){
$specProduct[$key] = $cart_item_id;
break;
}
}
}
//if there are items from the specific category in the cart
if(!empty($specProduct)){
$specPriceArray = array(); // array of specific product prices in cart
foreach ($specProduct as $key => $specItem) {
$thisProd = wc_get_product($specItem);
$thisPrice = $thisProd->get_price();
$specPriceArray[$key] = $thisPrice;
}
// most expensive specific product price
$highestSpec = max($specPriceArray);
// discount of most expensive item
$finalDiscount = $highestSpec * .1;
// print_r($specProduct); GOOD
// print_r($specPriceArray); GOOD
// echo $highestSpec; GOOD
// echo $finalDiscount; GOOD
/***** APPLY COUPON CHANGE HERE<-------HOW??? *****/
$cart->discount_total = $finalDiscount;
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'change_coupon_price_for_promo', 10, 1 );
What I'm asking:
The last line of the code before the closing brackets...
$cart->discount_total = $finalDiscount;
is what I tried using to change the coupon's discount in the cart. It doesn't work. How can I accomplish this? TY.
EDIT:
I should note that the coupon being added is set up as a "Fixed cart discount" with a rate of 0 so it is added as a line item to the order. This way the user sees the discount for the coupon code on its own line in the cart/checkout.
you can use function set_price.
you can call it in your code like:
$cart_item['data']->set_price('New price here')
use this inside the foreach that loops your cart items

Get order title(s) in Woocommerce

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

Update WP woocommerce price only in cart

I am trying to update a price in woo-commerce item that is already in cart.
This is the ajax function that i use to change but i cannot find any kind of option from woo-commerce to set a custom price to an item that is already inside cart and only to that item.
if ( !empty($_POST['product_id']) ){
$cart = $woocommerce->instance()->cart;
$id = $_POST['product_id'];
$qty = $_POST['qty'];
$money = $_POST['money'];
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
if ($cart_item['product_id'] == $id) {
}
}
}
Here is an example of what i am trying to achieve the second dropdown when selected to update the price accordingly

Categories