woocommerce calculate price server-side - woocommerce

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;
}

Related

Changing shipping cost in Woocommerce

I am creating a plugin that connects to an 3rd API to request the shipping cost based on the address of the person and the dimensions of the product.
Everything is fine but now I need the values ​​returned by the API to be added to the prices of the shipping methods and I tried with this code but it still doesn't work.
add_filter('woocommerce_package_rates','test_overwrite_fedex',100,2);
function test_overwrite_($rates,$package) {
foreach ($rates as $rate) {
//Set the price
$rate->cost = $phpVar1; <-//variable that receives the value of the shipment
}
return $rates;
But I don't find any way to change the shipping cost with functions..
Is it possible ? Thanks

Woocommerce - Tax optional

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);
}
}

How to add custom total of an individual product on order backend page

Basically, on the order edit page, I want to calculate the total of an individual product as price * product_height * quantity_of_products.
reference image
I have used the following hook but this hook is not getting executed.
add_filter( 'woocommerce_data_get_total', 'change_product_total_on_order_edit_page', 10, 2);
function change_product_total_on_order_edit_page ($value, $abc ) {
var_dump($value);
die();
return 15;
}
Use 'woocommerce_order_item_get_total' filter instead of 'woocommerce_data_get_total' because this filter is created dynamically by Woocommerce. The data in the hook is object type. When order items are fetched on orders edit page the object type is order_item.

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 - Override Shipping Cost

I am building an addon for WooCommerce that will allow the admin to set a per-customer flat rate shipping price. I am trying to hook into the function that calculates the shipping price so that I can override the calculated price and method and return only one shipping option with the pre-set price for that customer. What hook can I use to accomplish this?
I think it might be the woocommerce_calculated_shipping hook, but I can't find a good example of how to use it.
This is probably a late answer, but if someone else needs it:
add_filter('woocommerce_package_rates','test_overwrite_fedex',100,2);
function test_overwrite_fedex($rates,$package) {
foreach ($rates as $rate) {
//Set the price
$rate->cost = 1000;
//Set the TAX
$rate->taxes[1] = 1000 * 0.2;
}
return $rates;
}
The rates are cached by Woocommerce using the wordpress transient function. So when you're testing, make sure you change the item quantity so the package rates are updated correctly, or you can empty the cart each time you refresh:)

Resources