Woocommerce - manually add tax to cart - php

In woocommerce store I add manually product to cart by WC()->cart->add_to_cart() function.
I need add tax to this cart/product (in product menu I have selected tax class). When I complete order, I don't have tax column in order menu in ACP. I tried with WC()->cart->add_fee() and set_tax_class() but it don't work.
Maybe I'm doing something wrong, I will be very grateful for any help, thanks.

I've always used the following and it works fine. You need to hook into the woocommerce_cart_calculate_fees hook.
add_action( 'woocommerce_cart_calculate_fees','rs_packaging_fee' );
function rs_packaging_fee() {
global $woocommerce;
//Set the Fee
$materials_surcharge = '5.00';//if negative it will subtract from total.
//add the fee to the cart
$woocommerce->cart->add_fee( 'Metals Surcharge', $materials_surcharge, true, 'standard' );
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
return;
}
}

Related

Get custom calculated price of product on order again in Woocommerce

I'm stuck in a critical situation actually i am using custom price on my product which is calculated by the requirements of the product and on add to cart it works fine. But when the order is placed and customer wants to order it again...here order again functionality of woocommerce do not post the calculated price that has been paid by the customer before..after clicking on order again button it redirects to the cart page and set the regular price of the product
The question is woocommerce let us to use the custom price instead of regular price of the product but the Order Again functionality use regular price instead of custom price.
Thanks
global $product;
add_action( 'woocommerce_before_calculate_totals', 'quantity_based_bulk_pricing', 9999, 1 );
function quantity_based_bulk_pricing( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach($cart->get_cart() as $cart_item_key => $cart_item ) {
// Get the bulk price from product (variation) custom field
$bulk_price = $cart_item['wapf']['8']['values']['0']['label'];
$bulk_price_reorder = $cart_item['wapf']['6']['values']['0']['label'];
if($cart_item['data']->get_name() == "Print Custom Stickers"){
if($bulk_price != null){
$cart_item['data']->set_price( $bulk_price );
}
if($bulk_price_reorder != null){
$cart_item['data']->set_price( $bulk_price_reorder );
}
}
}
}
I've tried this hook in which i am saving custom price of product in a custom field and getting it from $cart_item array and it works for me but there are lot of orders that has been placed before so i want to get the price on cart from which customer has ordered this product before

Applying a coupon to Woocommerce cart

I am using Woocommerce and need to apply a coupon to the cart automatically when the user has selected a checkbox. I have this code
add_action( 'woocommerce_before_cart', 'apply_coupon' );
function apply_coupon() {
$coupon_code = 'sale';
if ( WC()->cart->has_discount( $coupon_code ) ) return;
WC()->cart->apply_coupon( $coupon_code );
wc_print_notices();
}
But this adds the coupon ALL THE TIME. How can I add a checkbox and make the coupon only applied when the checkbox is selected?
In order to do that, i'm using the free plugin Yith WooCommerce Deals : https://yithemes.com/themes/plugins/yith-woocommerce-deals/

Set a custom cart item price value from a GET variable In Woocommerce 3

I have a function on my Woocommerce website that enables customers to set a custom amount to pay for a specific product, based on a value I'm passing through the URL.
I'm using the woocommerce_before_calculate_totals hook, and up until I upgraded to WC 3.3.5, it was working fine. Now, when I run the code, the checkout initially shows the custom amount.
However, once the loader has finished updating, it resets the price to '0' (i.e. displaying £0.00 the checkout page's total fields).
Here's that code:
add_action( 'woocommerce_before_calculate_totals', 'pay_custom_amount', 99);
function pay_custom_amount() {
$payment_value = $_GET['amount'];
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if($cart_item['data']->id == 21 ){
$cart_item['data']->set_price($payment_value);
}
}
}
Well, colour me baffled. I've trawled Stack Overflow for solutions but can't see any similar problems. I see the hook runs multiple times but gather this is normal.
If anyone know what might be happening here, it would be great if you could share.
You can't get a price from an URL and set it in woocommerce_before_calculate_totals action hook. This needs to be done differently.
In the below code:
the first hooked function will get that "amount" from the URl and will set it (register it) in cart item object as custom data.
the 2nd hooked function will read that amount from custom cart item data and will set it as the new price.
Now your the target product ID in your code need to be the same ID that the added to cart product.
The code:
// Get the custom "amount" from URL and save it as custom data to the cart item
add_filter( 'woocommerce_add_cart_item_data', 'add_pack_data_to_cart_item_data', 20, 2 );
function add_pack_data_to_cart_item_data( $cart_item_data, $product_id ){
if( ! isset($_GET['amount']) )
return $cart_item_data;
$amount = esc_attr( $_GET['amount'] );
if( empty($amount) )
return $cart_item_data;
// Set the custom amount in cart object
$cart_item_data['custom_price'] = (float) $amount;
$cart_item_data['unique_key'] = md5( microtime() . rand() ); // Make each item unique
return $cart_item_data;
}
// Alter conditionally cart item price based on product ID and custom registered "amount"
add_action( 'woocommerce_before_calculate_totals', 'change_conditionally_cart_item_price', 30, 1 );
function change_conditionally_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE set your targeted product ID
$targeted_product_id = 21;
foreach ( $cart->get_cart() as $cart_item ) {
// Checking for the targeted product ID and the registered "amount" cart item custom data to set the new price
if($cart_item['data']->get_id() == $targeted_product_id && isset($cart_item['custom_price']) )
$cart_item['data']->set_price($cart_item['custom_price']);
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Woocommerce additional price (woocommerce_before_calculate_totals) is not adding during payment

In my application there is need to add extra price to each product if customer wants certain services, so I have added price from session (the extra price ) to each product by using woocommerce_before_calculate_totals hook,It works fine, whenever the total is showing including my extra price.
But the extra price is not added while making payment, only the product original price is sent to payment gateway, other wise in all places like (cart / checkout the right amount is showing)
Is there any hook that I am missing for payment where the extra price is not added.
Thanks in advance.
You have to use the following hook woocommerce_cart_calculate_fees.
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
For reference: https://docs.woocommerce.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/
add_fee reference: http://woocommerce.wp-a2z.org/oik_api/wc_cartadd_fee/

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