Woocommerce - Tax optional - wordpress

Is there a way for the customer to choose whether or not to include the tax in his order?
I imagine a field called "With / Without TAX"
And some code that based on that field, perform the necessary calculations.
If this is not possible, how could you add a button within the order edition to include or exclude taxes?
Thank you.

I did something similar by hooking into woocommerce_checkout_create_order when the order is placed. (Normal orders are taxed on customer's address, curbside orders are based on store address. You can simply use 0 based on if your customer checks a box)
add_action( 'woocommerce_checkout_create_order', array( $this, 'curbside_order'), 10, 1);
public function curbside_order( $order ) {
if( /** without tax - post_meta, checkbox or however you set it**/){
$order->set_cart_tax(0);
}
}

Related

Update ACF custom fields for new WooCommerce order

I'm trying to update 2 ACF custom fields every time an order is created.
We have orders coming from our own website and orders coming from an external marketplace integration. My goal is to differentiate the 2 type of orders using custom fields in the order.
The data for the custom fields is provided by an external marketplace integration. The first field has to be filled with the ordernumber (if it it exists) and the other field is just a simple true/false field.
For website orders we're using "Free shipping". For external orders, the provided shipping details are as follows:
Shipping "Verzending voor bol(xxxxx)"
I'm trying to get the shipping method using the woocomerce_new_order action, and using $order->get_shipping_method()
However, when a new order is created, the custom fields won't update.
This is what I've tried so far:
add_action( 'woocommerce_new_order', 'vliegwerk_bolcom_order_filter', 99, 2);
function vliegwerk_bolcom_order_filter( $order_id, $order ) {
$order = wc_get_order( $order_id );
$verzendmethode = $order->get_shipping_method();
$bol_ordernummer = (int) filter_var($verzendmethode, FILTER_SANITIZE_NUMBER_INT);
if ($bol_ordernummer > 0) {
update_field('field_61fa977c8dff9', $bol_ordernummer, $order_id );
update_field('field_61fa95f594b13', 1, $order_id );
}
}
I already tested the woocommerce_new_order trigger, it works as expected.
I've also tried updating the fields using a foreach loop, and triggering the function with a shortcode. This also works as expected.
However, when combining it all together, the fields won't update. Any help would be appreciated!

exclude "On Hold" orders from Woo Sales reports

I want to remove/exclude any orders with the order status "On Hold" from woocommerce sales reports. By default On hold orders are included in the sales reports.
There is a function "exclude_from_order_sales_reports" found here - https://docs.woothemes.com/wc-apidocs/function-wc_register_order_type.html - that will exclude a post type from sales reports.
Also the woo order types and order status are handled here - https://docs.woothemes.com/wc-apidocs/source-function-wc_register_order_type.html#167-221
Unfortunately my php coding is basic (im still learning) and i have no clue where to start with all this code. How can I use the above function to remove on hold order from sales reports?
i seen on woocommerce main report class WC_Admin_Report on line 67 they have filter like this $order_status = apply_filters( 'woocommerce_reports_order_statuses', $order_status );
so i think you can easily alter the default order status for reports by adding filter like this on your functions.php
add_filter( 'woocommerce_reports_order_statuses', 'my_custom_order_status_for_reports', 10, 1 );
function my_custom_order_status_for_reports($order_statuses){
$order_statuses = array('completed'); // your order statuses for reports
return $order_statuses;
}

woocommerce add function when admin changes product's attributes

I am relatively new to woocommerce development so I am sorry if this question might be too trivial but I need help.
I need in my application a way to make some checks when an admin makes changes to a product in woocommerce.
For example, I want to create a log file of all the changes that occurred on products. Who made them, when and what was the change (price, inventory, description, etc.).
I understand that there are hooks in woocommerce that I can use. Which ones can help me do something like that?
Use post_updated hook for this purpose, place the following code in your functions.php
function product_update_handler( $id, $before_data, $after_data ) {
if( $before_data->post_type == "product" ) {
$current_user = wp_get_current_user();
error_log( $before_data->post_title ." has been updated by ".$current_user->user_login );
}
}
add_action('post_updated', 'product_update_handler', 0, 3);
you have two product objects ( before update, after update ) with the above hook, you can compare both object and log the changes.

How to force remove products from WooCommerce cart?

I'm building a site with Wordpress and WooCommerce for someone for the purposes of a business, but they have certain items that cannot be sold together - essentially the opposite of a force sell or chained products. If a customer puts product A in their cart, I don't want them to be able to put product C in with it, but B and D are fine. Alternatively, when it goes to checkout, separating the products into two separate orders would work as well. Is there any way to do this? This is my first time using Wordpress, so I'm a bit at a loss.
You can use this code below :
add_action('woocommerce_add_to_cart_handler','mycustomfuncion',11,2);
function mycustomfuncion($p,$q)
{
global $woocommerce;
$cartItem = $woocommerce->cart->cart_contents;
$currentProductId = $q->id;
foreach($cartItem as $item)
{
$productItemId = $item['product_id'];
///your condition will be here
}
return $q;
}
You will get all product ids which are in cart by the $productItemId. And you will get current product id (which user wants to add to cart ) by $currentProductId. After your conditions if you want to allow to add to cart the product then return $q from the function otherwise don't return any value.

woocommerce calculate price server-side

I need do sell a product whose price depends on a complex calculation over non-discrete parameters set by the customer on the product page, and also on a custom database query result.
How can i calculate the price server-side every time the customer changes parameter-values and apply that price when the customer adds to cart?
i read a similar post whose answer suggests a WC plugin, but even that plugin doesn't satisfy my needs.
Thanks
Probably, you should try to use woocommerce_get_price filter
add_filter('woocommerce_get_price', 'get_dynamically_generated_price', 10, 2);
function get_dynamically_generated_price($price, $product) {
// ... here doing your magic with $price based on $product
// ...
return $price;
}

Resources