Which Hook is running after Woocommerce update cart button action - php

i need to know which hook is running after clicking the update cart button in the cart page .
That is in cart page we have 4 button , update cart , continue shopping, proceed to checkout , apply coupon .
So i want to know which hook is run after update cart button is clicked . When customer click the update cart button after changing the quantity then i have to run a special function that can change total price in the cart , If some condition met , i will change the total price in the cart , and this total price need to pass to the checkout page also /
Please help .
For example
add_filter('after_update_cart_function_finished', 'special_function');
function special_function(){
$applied_coupons= WC()->cart->get_applied_coupons();
if(!empty($applied_coupons)){
$new_value=WC()->cart->get_cart_subtotal();
$discounted=WC()->cart->coupon_discount_totals;
$discounted_value=array_values($discounted)[0];
$new_value=$new_value-$discounted_value+100;
WC()->cart->set_total_price($new_value);
After this update all post_meta value that regarding to order total
}
}
Please see the following custom function that i write for to change value in cart
function action_woocommerce_cart_totals_after_order_total( ) {
$applied_coupons= WC()->cart->get_applied_coupons();
if(!empty($applied_coupons)){
$new_value=WC()->cart->get_cart_subtotal();
$discounted=WC()->cart->coupon_discount_totals;
$discounted_value=array_values($discounted)[0];
$new_value=$new_value-$discounted_value;
if($new_value<100){
$new_value=$new_value+5;
}
?>
<style>
.new-price-new{
color:black;
font-size: 17px;
}
</style>
<script>
jQuery(function($){
$(".order-total .woocommerce-Price-amount.amount").text("£<?php echo $new_value;?>");
$(".order-total .woocommerce-Price-amount.amount").hide();
$(".new-price").remove();
$('.order-total .woocommerce-Price-amount.amount').after('<div class="new-price-new">£<?php echo $new_value;?></div>');;
$(".new-price-new").show();
});
</script>
<?php
}
else{
?>
<script>
jQuery(function($){
$(".new-price-new").remove();
});
</script>
<?php }
};
add_action( 'woocommerce_cart_totals_after_order_total', 'action_woocommerce_cart_totals_after_order_total', 10, 0 );
And this function have many problems , i write this function because of some reason or some other function woocommerce is not calculating coupon price correctly , so i write this function for to manually update the product price in cart .Here if the cart value is more than 100 we provide free shipping other vise we will add 5 . Even this function is not working properly .
Woocommerce Create new discount functionality

You should try woocommerce_update_cart_action_cart_updated action hook. I have revisited your code a bit. Try this:
add_action( 'woocommerce_update_cart_action_cart_updated', 'on_action_cart_updated', 20, 1 );
function on_action_cart_updated( $cart_updated ){
$applied_coupons = WC()->cart->get_applied_coupons();
if( count( $applied_coupons ) > 0 ){
$new_value = WC()->cart->get_cart_subtotal();
$discounted = WC()->cart->coupon_discount_totals;
$discounted_value = array_values($discounted)[0];
$new_value = $new_value-$discounted_value + 100;
WC()->cart->set_total( $new_value );
if ( $cart_updated ) {
// Recalc our totals
WC()->cart->calculate_totals();
}
}
}
Code goes in function.php file of your active child theme (or active theme). Untested. It could work.
Update: The WC_Cart set_total_price() method doesn't exist… I have replaced it by existing WC_Cart set_total()

Related

Disable add to cart button based on WooCommerce product custom stock status

Currently in woocommerce, add to cart button disabled, if the stock status is out of stock. I add new stock status with the label Discontinued product by using woocommerce_product_stock_status_options, now I am looking for a way to treat this product like it is out of stock.
Since I believe it is better to separate between a product that is not produced anymore and a product that produced and available in another store but it's not in stock.
You can use the following to disable add to cart button based on a custom stock status (where you will replace custom_status_slug by your custom status slug):
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
if ( $product->get_stock_status() === 'custom_status_slug' ) {
return false;
}
return $purchasable;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I propose a solution that displays the stock status on the product page but not the 'add to cart' button
add_filter('woocommerce_product_is_in_stock', 'filter_is_in_stock_callback', 10, 2 );
function filter_is_in_stock_callback( $stock, $product ) {
if ( $product->get_stock_status() === 'custom_status_slug' ) {
return false;
}
return $stock;
}
Thanks for the first answer :)

Conditional Add To Cart Button On Woocommerce Product Archive

I'm trying to replace the default woocommerce product archive add to cart button based on a condition.
For example
Product A - Checkbox Active --> Display Find A Dealer Button
Product B - Checkbox inactive -- > Display default add to cart button
I have managed to successfully write the code to add the checkbox and the condition to replace the button if the product has a custom checkbox active. The button for product A works fine and diaplyas as intended in the shop archives.
I am however not sure how to retain the woocommerce default add to cart button if for products that do no have this checkbox activate. I thought adding the action would work however I am stumped. Any help would be greatly appreciated. Thank you in advance.
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button' );
function replace_default_button(){
global $product;
if ($product->get_meta('_checkbox_active') === 'yes' ){
return '<button>Finda Dealer</button>';}
else {add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );}
You just forgot the hooked function variables arguments. Try the following instead:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
if ( $product->get_meta('_checkbox_active') === 'yes' ){
$button = '' . __( "Find a dealer", "woocommerce" ) . '';
}
return $button;
}
Code goes in functions.php file of your active child theme (or active theme). It should work.

Woocommerce custom add to cart button with custom checkout page

I have a Woocommerce store, I was looking to add another add to cart button named "Try now" to a particular category(not to all the categories). When the user clicks on "try now", it gets redirected to the custom checkout page.
On custom checkout page, try-now products can be added up-to 5 products max, total is fixed as 500 for all the try now products and payment only by card.
I have found the following codes.
Code to replace add to cart button
function remove_loop_button(){
if (in_array( 10, $product->category_ids))
remove_action(‘woocommerce_after_shop_loop_item’,‘woocommerce_template_loop_add_to_cart’, 10);
}
add_action(‘init’,’remove_loop_button’);
add_action(‘woocommerce_after_shop_loop_item’,’replace_add_to_cart’);
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
echo ‘<a href=”‘.esc_attr($link).'”>Try Now</a>’;
}
And also this code to customize checkout page from Make 2 different WooCommerce checkout pages?
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
global $woocommerce;
//assuming only one product in cart
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
}
switch ($_product->id) {
case '666': //product id for product 1
//do stuff with the checkout fields
break;
case '999': //product id for product 2
//do stuff with the checkout fields
break;
}
return $fields;
}
I know it's quite complicated, Can any one please suggest me where to start? Or any hint will be appreciated. Thanks.

Hooks around add to cart for Woocommerce

I am running a WooCommerce store with WC Marketplace. What I am trying to achieve with the hook below is to prevent a new item being added to a basket if there is already a product in the basket from a different vendor. e.g. If a shopper adds product x from vendor y to his basket, if they were to add product a from vendor b, then the item will not be added and the user will be informed of the error.
I have 2 questions:
- firstly when does a hook run, is it before the main function fired or after? I have a hook for the function woocommerce_add_to_cart. So I want to know will the hook fire after the function woocommerce_add_to_cart runs or before.
- My second question is, I have attached the hook below, in your opinion would this work?
function action_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
$same_vendor = 1;
$empty = WC_Cart::is_empty();
//If there is an item in the cart then,
if (!$empty) {
//Get the VendorId of the product being added to the cart.
$vendor = get_wcmp_product_vendors($product_id);
$vendor_id = $vendor->id;
foreach( WC()->cart->get_cart() as $cart_item ) {
//Get the vendor Id of the item
$cart_product_id = $cart_item['product_id'];
$cart_vendor = get_wcmp_product_vendors($product_id);
$cart_vendor_id = $cart_vendor->id;
//If two products do not have the same Vendor then set $same_vendor to 0
if($vendor_id !== $cart_vendor_id) {
$same_vendor = 0;
}
}
if ($same_vendor === 0) {
WC()->cart->remove_cart_item( $cart_item_key );
//How do I show a message to tell the customer.
}
}
}
Regards
Here below are the hooks involved in WC_Cart add_to_cart() method:
A) Before an item is added to cart:
Validation filter hook woocommerce_add_to_cart_validation
Item Quantity change filter hook woocommerce_add_to_cart_quantity (not with ajax)
Item Data change filter hook woocommerce_add_cart_item_data (not with ajax)
and some others related to "sold individually" products (see here)
A) After an item is added to cart:
Change cart Item filter hook woocommerce_add_cart_item
Add an event, action hook woocommerce_add_to_cart
To be clear in your case:
As you can see now woocommerce_add_to_cart is not a function but only an action hook.
Hook location: It's located inside WC_Cart add_to_cart() method (at the end of the source code).
When the hook is fired: It's fired once the WC_Cart add_to_cart() method is executed.
What is the purpose: To execute some custom code when this method is executed (event).
Regarding your code:
It should be better to use the dedicated filter hook woocommerce_add_to_cart_validation that will stop customer that want to add a new item to the cart if there is already a product in cart from a different vendor, displaying optionally a custom message:
add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 3 );
function filter_add_to_cart_validation( $passed, $product_id, $quantity ) {
if ( WC()->cart->is_empty() ) return $passed;
// Get the VendorId of the product being added to the cart.
$current_vendor = get_wcmp_product_vendors($product_id);
foreach( WC()->cart->get_cart() as $cart_item ) {
// Get the vendor Id of the item
$cart_vendor = get_wcmp_product_vendors($cart_item['product_id']);
// If two products do not have the same Vendor
if( $current_vendor->id != $cart_vendor->id ) {
// We set 'passed' argument to false
$passed = false ;
// Displaying a custom message
$message = __( "This is your custom message", "woocommerce" );
wc_add_notice( $message, 'error' );
// We stop the loop
break;
}
}
return $passed;
}
Code goes in function.php file of your active child theme (or active theme) or in any plugin file.
Tested and works.

woocommerce change price in checkout and cart page

With woocommerce, in my website I'd like to add in the cart page a select input where the user can select a value between two options, and depending on this value I will change the price.
so far, I could get the total and change it using this :
function action_woocommerce_before_cart_totals( ) {
global $woocommerce;
$woocommerce->cart->total = $woocommerce->cart->total*0.25;
var_dump( $woocommerce->cart->total);};
The issue is that when I go to checkout page it doesn't take the total calculated in functions.php
Thanks for helping me.
You can use woocommerce_review_order_before_order_total hook too at the same time, to display your custom price in checkout, this way:
add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
function custom_cart_total() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
WC()->cart->total *= 0.25;
//var_dump( WC()->cart->total);
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
Payment gateway always uses $order->get_total() variable to fetch cart grand total. So in order to tweak use this filter woocommerce_order_amount_total
for your function if you do follow below steps. Your payment gateway always shows the total you tweaked.
add_filter( 'woocommerce_order_amount_total', 'custom_cart_total' );
function custom_cart_total($order_total) {
return $order_total *= 0.25;
}

Categories