woocommerce custom price based on quantity - wordpress

$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.

Related

Update Item Cart change to delete and add

i want change update item in woocomerce. I use plugin (sumo measures) to calculate metadata (size, pack) and when I try to update quantity of cart product metadata dont change.
My idea to solution problem is remove item from cart and add.
My code:
add_action( 'woocommerce_update_cart_action_cart_updated', 'on_action_cart_updated', 20, 1 );
function on_action_cart_updated( $cart_updated ){
WC()->cart->empty_cart();
foreach( WC()->cart->get_cart() as $cart_updated ){
$product_id = $cart_updated['product_id'];
WC()->cart->add_to_cart( $product_id, $cart_updated['quantity'] );
}
}
But code dont work.
I find using this action hook : woocommerce_update_cart_action_cart_updated
Using this you can get the cart data: $cart = WC()->cart->cart_contents;
You can get the cart itemmeta and quantity and then update the Sumo measures cart item data.
Because to remove and re-add the product with updated quantity will be the length process and also there will be much other factors need to take care.
Please let me know if you find any issues.
Thanks.

WooCommerce Upsells Same Order as Admin

really hoping someone can help with this as I thought it would be far more simple!
Long story short, I have created a script that populates the upsells of WooCommerce products, all works great using the API and they are there. They show on the product page as expected but in a completely different order to how they were inputted in the admin area and I cannot seem to find a way for the order to follow admin?
function filter_woocommerce_upsells_orderby( $orderby ) {
return 'menu_order';
};
add_filter( 'woocommerce_upsells_orderby', 'filter_woocommerce_upsells_orderby', 10, 1 );
Above is the hook I have found but from the options I have found such as menu order / id / price etc, there is not simply an overide option to ignore the order and just take them as they are in admin!?
Please help!
I also encountered this problem. And i have an idea.
I checked all wordpress parameters about Order & Orderby.Link is https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters.
And i use the paramteters name "none",It can make your orderby not based on any sorting rules.In other words, it is sorted according to upsells. here is the code.
// ORDER BY
add_filter( 'woocommerce_upsells_orderby', 'filter_woocommerce_upsells_orderby', 10, 1 );
function filter_woocommerce_upsells_orderby( $orderby ){
return "none";
};
// ORDER
add_filter( 'woocommerce_upsells_order', 'filter_woocommerce_upsells_order', 10, 1 );
function filter_woocommerce_upsells_order(){
return 'asc'; // Default is 'desc';
};
But it is still chaotic, when adding any product to upsells, it is still random. Therefore I also used a plug-in "WooCommerce Drop/Drag For Upsells Cross-Sells", which allows you to drag your products in upsell at will.
If you have any question you can ask me.
Thanks.

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

Woocommerce product bundles doubling

I have installed the Woocommerce product bundles plugin where I am having issues. For example:
My bundle is setup like:
When you buy Product-A, You can optionally buy Product-B and Product-C in that bundle.
When I purchase 4x Product-A and optionally add 2x Product-B and click add to cart my cart contents/total is
4x Product-A
8x Product-B
It seems as though however many of the parent product there is it will times itself by optional products qty.
Hope someone can help.
Fixed this problem.
I looked at wc-pb-cart.php and looked over the function bundle_add_to_cart and came across this:
$quantity = $bundled_item->is_sold_individually() ? 1 : $item_quantity * $bundled_item->get_quantity();
So I created my own class and included it within functions.php and then extended WC_PB_Cart and called my new function(same function just removed * $bundled_item->get_quantity() which was causing the duplication.
Then I needed to remove_action on bundle_add_to_cart in my functions.php
include 'class-cartFeatures.php';
remove_action( 'woocommerce_add_to_cart', array( WC_PB_Cart::instance(), 'bundle_add_to_cart' ), 10, 6 );
add_action( 'woocommerce_add_to_cart', array( new cartFeatures(), 'bundle_add_to_cart_excalibur' ), 10, 6 );

woocommerce add dynamic price while add to cart

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

Resources