I want to change total tax amount in the checkout page and my code is below.
function change_tax_for_checkout_page( $cart ) {
$cart->set_total_tax(20);//20USD
$check = $cart->calculate_totals();
return $cart;
}
add_action( 'woocommerce_before_checkout_form', 'change_tax_for_checkout_page' );
But it's not working.
Related
Bit of a newbie... I have a survey and on submission, I am generating a dynamic coupon code to incentivize purchase of a product. When user adds a product to the cart, the discount code is applied successfully, however I would like to display a notice on the product page indicating that (because the user took the survey), they will receive a discount applied at checkout.
Code for adding the product to cart:
// Add generated coupon to cart
add_action( 'woocommerce_add_to_cart', 'apply_matched_coupons' );
function apply_matched_coupons() {
if ( is_cart() || is_checkout() ) return;
// Load coupon code
$coupon_code = WC()->session->get( 'unique_coupon' );
if ( $coupon_code && ! WC()->cart->has_discount( $coupon_code ) ) {
WC()->cart->apply_coupon( $coupon_code ); // Apply coupon code
WC()->session->__unset( 'unique_coupon' ); // remove session variable
}
}
One of the many ways I've tried to add a notice:
add_action( 'woocommerce_before_single_product', 'custom_message_before_single_product', 5 ); // For single product page
function custom_message_before_single_product() {
if ( $coupon_code && ! WC()->cart->has_discount( $coupon_code ) )
wc_print_notice(sprintf('You will receive a discount at checkout!', $coupon_code), 'notice');
}
I also tried product-based, but the notice appears regardless of whether a coupon has been created:
add_action( 'woocommerce_before_single_product', 'custom_message_before_single_product', 5 ); // For single product page
function custom_message_before_single_product() {
global $product;
if ( $product->get_id() == 1218 && $coupon_code = WC()->session->get( 'unique_coupon' );) {
wc_print_notice(sprintf('You will receive a discount at checkout!', $coupon_code), 'notice');
} }
Of course I'm basing this off the belief that because a session-based coupon was created, it's possible to grab that coupon, or recognize it has been created and display a notice prior to it being added to the cart but perhaps it's not possible...?
how Hide order review based on cart items total in WooCommerce ?
i found this code, but i need hide review based on cart items
add_filter( 'woocommerce_cart_needs_shipping', 'show_hide_shipping_methods' );
function show_hide_shipping_methods( $needs_shipping ) {
$cart_items_total = WC()->cart->get_cart_contents_total();
if ( $cart_items_total < 40 ) {
$needs_shipping = false;
// Optional: Enable shipping address form
add_filter('woocommerce_cart_needs_shipping_address', '__return_true' );
}
return $needs_shipping;
}
Do you mean this?
// Remove on checkout page
function remove_checkout_totals() {
$cart_items_total = WC()->cart->get_cart_contents_total();
if ( $cart_items_total < 40 ) {
// Remove cart totals block
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
}
}
add_action( 'woocommerce_checkout_order_review', 'remove_checkout_totals', 1 );
For a client, I need to let backorders for products but they don't want to charge for those. So, I need to update the price dynamically if they stock is 0 or the stock is less than the quantity on the cart.
This is my code:
add_filter( 'woocommerce_cart_product_subtotal', 'modify_cart_product_subtotal', 10, 4 );
function modify_cart_product_subtotal( $product_subtotal, $product, $quantity, $cart ) {
if ( $product->get_stock_quantity() < $quantity ) {
return $product->get_stock_quantity() * $product->get_price();
}
return $product_subtotal;
}
In case of 0 stock, it works like a charm. In case of a quantity greater than the stock, it charges only for the available products, but the tax keeps increasing even for the rest. How can I exclude those from taxes calculations as well?
EDIT:
If I just update the price of the product, it does it for all of them, not only those above the available stock.
function zero_price_for_backorders( $cart_object ) {
global $isProcessed;
if( !WC()->session->__isset( "reload_checkout" )) {
foreach ( $cart_object->get_cart() as $key => $value ) {
if ( $value['data']->get_stock_quantity() < $value['quantity']) {
$value['data']->set_price(0);
}
}
$isProcessed = true;
}
}
add_action( 'woocommerce_before_calculate_totals', 'zero_price_for_backorders', 99 );
You should use before_calculate_total and change product price using set_price function so that you don't have to modify subtotal separately. The rest of the flow will fall in place automatically.
You can have a look at this link on how to change it dynamically:
Change product price dynamically woocommerce
I want to apply a discount if a custom checkout field is filled.
That's the code i use to generate the discount:
function custom_wc_add_discount() {
$0tax = WC()->cart->subtotal * -0.22;
WC()->cart->add_fee( '0% tax', $0tax );
}
add_action( 'woocommerce_cart_calculate_fees','custom_wc_add_discount' );
And I want to condition it with something like this:
if ( $_POST[billing_vat]){
Edit: I solved the condition problem creating a coupon like this
add_action( 'woocommerce_checkout_process', 'apply_tax_coupon' );
function apply_tax_coupon() {
global $woocommerce;
$coupon_code = '0%val';
$woocommerce->cart->remove_coupon( $coupon_code );
if ( $_POST[billing_vat]){
$woocommerce->cart->add_discount( $coupon_code );
}
wc_print_notices();
}
But now in checkout page it doesn't show the discounted total.
Any help is appreciated.
You have some problems :
You can't start php var with a number
you missed the quote arroung billing_vat in the condition
Try resolve this and we will see if there is other things to do.
I want to apply the discount coupon to the specific product in cart from checkout page.
Inside the cart:
product1 w/ upgrades
product2 w/o upgrades
when I apply the discount coupon I want that the discount will take effect only in product2's price. I tried to search in some forums but the coupon will take effect in the whole cart items.
You can try this piece of code:
function calculate_variation_addon_price( $cart_object ) {
global $isProcessed;
// Loop for all products in cart
foreach ( $cart_object->cart_contents as $key => $value ) {
// Your condition here for product specific i.e. id == 25
if((!isset($isProcessed) || empty($isProcessed))) {
$orgPrice = floatval( $value['data']->get_price() );
$cart_object->cart_contents[$key]['data']->set_price( $orgPrice * 0.5 );
}
}
$isProcessed = 1;
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_variation_addon_price', 2, 1 );
While adding coupon from admin panel, find a tab there, called 'Usage Restriction'.
There you can add products or product categories or even exclude some for the coupon code to be applied.
You can so that using product id, sku or name
function filter_woocommerce_coupon_get_discount_amount( $discounting_amount, $price_to_discount , $cart_item, $single, $coupon ) {
// On backorder
if ( $cart_item['data']->get_id() !== 'product2_id' ) {
$discounting_amount = 0;
}
return $discounting_amount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
You can use in_array instead of !== for multiple products
You also replace get_id() by other properties or use custom attributs...