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
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'];
}
}
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
I'm using the following code to add a free product to the basket when over a certain cart total.
When I drop below the cart total it hits the if statement but won't remove the product.
I think this may be because the quantity is set to 1 on the basket form and my code isn't overriding that quantity to set it to 0 (and remove it).
Is there another way to remove it or override it?
/*
* Automatically adding the product to the cart when cart total amount reach to £20.
*/
function aapc_add_product_to_cart() {
global $woocommerce;
$cart_total = 20;
$free_product_id = 85028; // Product Id of the free product which will get added to cart
if ( $woocommerce->cart->total >= $cart_total ) {
echo "Over the limit";
$quantity = 1;
WC()->cart->add_to_cart( $free_product_id, $quantity );
} elseif($woocommerce->cart->total < $cart_total) {
echo "Under the limit";
$quantity = 0;
WC()->cart->remove_cart_item( $free_product_id, $quantity );
}
}
add_action( 'template_redirect', 'aapc_add_product_to_cart' );
I've tried using this question but cannot get it to work, it won't even add the item to my basket.
I'm using Woocommerce version 3.6.5 if that helps.
I'm not sure on how Woocommerce works but I assume the remove_cart_item 'quantity' parameter takes the amount of the item to remove from the basket. So set the value of $quantity to 1 and it should remove one of the free product from the basket. If there happens to be more than one free item in the basket then you'll need to set your $quantity value accordingly.
Furthermore, you are adding a free item every time a product is added when the basket value is higher than $cart_total. Maybe add a check to see if it already exists in the basket before adding it?
Hope I understood your question correctly
/*
* Automatically adding the product to the cart when cart total amount reach to £20.
*/
function aapc_add_product_to_cart() {
global $woocommerce;
$cart_total = 20;
$free_product_id = 85028; // Product Id of the free product which will get added to cart
if ( WC()->cart->total >= $cart_total ) {
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $free_product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $free_product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $free_product_id );
}
} elseif(WC()->cart->total < $cart_total) {
$quantity = 0;
$prod_unique_id = WC()->cart->generate_cart_id( $free_product_id );
// Remove it from the cart by un-setting it
unset( WC()->cart->cart_contents[$prod_unique_id] );
WC()->cart->remove_cart_item( $free_product_id );
}
}
add_action( 'woocommerce_after_calculate_totals', 'aapc_add_product_to_cart' );
My WordPresss related task is simple, but I can't find the solution. I have 2 products in my woocommerce shop, and I would like to show both of them on the cart-page in the woocommerce table, let the customer set the quantity of them. If the customer don't want to buy something, just leave it on 0.
The problem is the cart table is not shown if it is empty and I can only see the items that I put in there.
you can add product like that and add condition according you i set for admin and product id. change product id with your product id
/*
* goes in theme functions.php or a custom plugin
**/
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 64;
$found = false;
//check if product already in cart
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 );
}
}
}
I have a coupon automatically adding to my cart on checkout. Basically; if category A and B are present apply the coupon. If the coupon is applied and both categories are no longer present (customer removed item from cart) then remove coupon.
This works great but I am having trouble setting up what I actually want which goes a step further:
if Both products A and B are present in cart apply the discount for EACH Of product A in cart. so if you have 5 Product A apply the discount 5 times. if the customer then removes one product A remove the extra coupon that was applied.
How would I achieve this? here is the current code for matching and applying the discount:
global $woocommerce;
//create empty category array for push
$category_array = [];
// coupon code created in wooCommerce to apply
$discount = 'A-bought-with-B';
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
// find product categories and push them to the array
foreach ($terms as $term) {
$_categoryid = $term -> name;
$category_name = strtolower( $_categoryid );
// push term name to category array
$category_array[] = $category_name;
}
}
// 01
// A Discount when purchased with B
if ( in_array("A", $category_array) && in_array("B", $category_array) ) {
$woocommerce->cart->add_discount( sanitize_text_field( $discount ) );
} // if categories are not in cart, but the coupon is applied remove it
else if ( !in_array("A", $category_array) || !in_array("B", $category_array) && $woocommerce->cart->has_discount( $sticker_coupon_code ) ) {
$woocommerce->cart->remove_coupon( $discount );
}