woocommerce add dynamic price while add to cart - wordpress

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

Related

Add the same product more than once, in an order from the admin panel

I need to be able to add the same product more than once within an order in WooCommerce.
This is the scenario.
I create an order manually from the admin panel of Woocommerce, and I need to be able to add the same product N times to this order.
Basically I need something like what this snippet does, but for the backend instead of for the frontend cart.
function separate_individual_cart_items( $cart_item_data, $product_id ) {
$unique_cart_item_key = md5( microtime() . rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'separate_individual_cart_items', 10, 2 );
Any suggestion to get this?
Thank you.
In order to duplicate product N times you can add any woocommerce clone duplicate plugin.
You can download a plugin on link to accomplish your task

Add amount option for each variation

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:

Woocommerce is it possible to disable quantity field on cart page, for some product?

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.

woocommerce custom price based on quantity

$productPrice = $12.00
Is it possible if productQuantity = 12 would be $productPrice = $99.00
I have used these 2 filters and also i have used dynamic pricing plugin that also not worked
add_action( 'woocommerce_before_calculate_totals', 'cp_add_custom_price' );
add_action('woocommerce_before_cart_table', 'discount_10');
Please let me know this thing is possible
Thanks
I can make it work with this hook
add_action( 'woocommerce_before_calculate_totals', 'cp_add_custom_price' );
you can do as much customization you need want to custom price based on quantity with the above hook
$value['data']->price = $customPrice;
based on my requirement I have calculated $customPrice while adding 12 product in cart.

How to apply an automatic discount in WooCommerce based on cart total?

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.

Resources