Referring to this question How do I programmatically apply a coupon in Woocommerce?
I'm looking for something very similar to the final solution in the first post, but I'd like the coupon to be applied if the subtotal is > 99 euros.
How do you think I can modify the code? And since I'm a newbie... where do I have to paste the whole code?
Thanks a lot
Something like this could be done:
add_action('woocommerce_before_checkout_process','add_discount_at_checkout');
function add_discount_at_checkout(){
global $woocommerce;
$minorder = 99;
if( $woocommerce->cart->get_cart()->cart_contents_total>$minorder){
//APPLY COUPON HERE
}
}
So basically, if your cart total is greater than 99, you can do something, like add a coupon.
Related
Been searhing for a simple way to add 400 g to all orders for the shipping plugin to get the correct weight. Looked at Cart total Weight and Shipping Recalculation on WooCommerce but do not need all that
You may need to change your Weight Unit first in WooCommerce > Settings > Product > Measurements section.
Then use the following filter hook in your functions.php to override the total cart weight.
add_filter('woocommerce_cart_contents_weight','add_package_weight_to_cart_contents_weight');
function add_package_weight_to_cart_contents_weight( $weight ) {
$weight = 400; // change to your expected cart content weight.
return $weight;
}
Please note that it's recommended to use a child while you add custom codes to your theme.
I am building a website that sells products with different variation. I created some variations to the products. However, the variations on the single product show up in a select box.
I want the customer to be able to choose an amount they want for each variation on the same page.
For example:
Chocolate 2x (customer chooses 2 Chocolate variations)
Vanille 3x
I want the customer to be able to buy those 5 flavours in 1 go and that they can select the amount for each variation of the product.
How can we reach this?
You should use Grouped Product.
It will look something like this on the frontend.
if you insist on using the variable product and treat it like it's a group, we can do that too.
First we have to remove the hook for the add to cart form of the variable product like so:
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
then we have to change it to use the add to cart form of the grouped variable like so:
add_action( 'woocommerce_variable_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
in doing so, you'll end up something like this on the front end:
take note of the dashes after the quantity input. It's in format Title - Variation
So this must be it. Then when you're about to do an add to cart it shows like this:
It says "Please choose product options…". Why so? This is because the product is of type variable, and we are expected to choose on option. Remember select box.
However, we can fixed it like this:
add_filter( 'woocommerce_add_to_cart_handler', 'woocommerce_add_to_cart_handler', 10, 2 );
function woocommerce_add_to_cart_handler( $product_type, $product ) {
// if ( in_array( $product->get_id(), array( 100, 99, 504 ) ) ) {
$product_type = 'grouped';
// }
return $product_type;
}
We'll change the product type of the form handler. This way, we're using the grouped form handler and not the variable. Just be careful to put a conditional statements like if to only apply to specific products. Or else you'll end up confused between grouped products and variable products if you know what I mean. I've also recklessly did an add_action( 'woocommerce_variable_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 ); without conditional statement, I suggest you do.
But once everything is in place, it will look something like this:
Basically, I finished building custom plugin for my client.
the only thing after products added to cart, before the checkout.
user able to change the quantity of the products, is it possible to display the selected quantity, but disabled the options to read only so client will able to see the quantity in cart page that he selected but can't change it?
and to apply this only to products that I used with my plugin either product ids or better category id because all the products there.
other product display and able to change quantity regular
by the way its regular products not virtual and not Sold Individually i need to find a way to limit clients to change quantity for some products only in cart page!, and not in product page.
I really appreciate any help.
As mentioned in the comment, you can use the woocommerce_cart_item_quantity filter for that. So that might look something like this:
function 668763_change_quantity_input( $product_quantity, $cart_item_key, $cart_item ) {
$product_id = $cart_item['product_id'];
// whatever logic you want to determine whether or not to alter the input
if ( $your_condition ) {
return '<h3>' . $item['quantity'] . '</h3>';
}
return $product_quantity;
}
add_filter( 'woocommerce_cart_item_quantity', '668763_change_quantity_input', 10, 3);
This would be just a simple example to replace the input with a h3 element containing the quantity. It can easily be adjust to alter the quantity input element to your liking.
Using Woocommerce plugin.
I get this code to add Tax based in the buyer rol.
It works fine but the Tax is only applied to product price. I also need to apply the same Tax to the Shipping cost.
I guess I need to use another filter parameter but I don't know which one.
Here is the real code:
function wc_re_eq( $tax_class, $product ) {
if ( is_user_logged_in() && current_user_can( 'client_re' ) ) {
$tax_class = 'R.E';
}
return $tax_class;
}
add_filter( 'woocommerce_product_tax_class', 'wc_re_eq', 1, 2 );
You should be applying that in your tax rate chart. There is a checkbox for shipping, where you apply tax to shipping.
WooCommerce documentation: https://docs.woocommerce.com/document/setting-up-taxes-in-woocommerce/#tax-rate-examples
#Jpash do you solve it?
Through the label "R.E.", I get the feeling that your problem is related to "recargo de equivalencia" of Spain.
I have the same problem. For certain users I must apply another type of tax only for the shipping costs.
My task is :
I have test, test1, test2, test3 ==> 4 products
The test product price is $0.
While add to cart the price will be added to that particular 'test' product is $500
How to achieve this.
I use the following hook
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
But it only shows the total as 500. I need to show this price as product price in my entire cart. How to do this. Please help me. Thanks
I wrote a nice guide on how to add a form to a product and then change the price.
If you look for my function 'calculate_cost' and find every where it is used, you should be able to figure out how to modify the price such that 'test' is $500.
For example you could do something like this:
add_filter('woocommerce_add_cart_item', array(&$this, 'add_cart_item'), 10, 1);
function add_cart_item($cart_item) {
// TODO: Logic to determine when this is the 'test' product
$cart_item['data']->set_price('500');
}