Force specific woocommerce variation price instead of from - php

I have a woocommerce subscription product with two variations (Pay once and Pay over 12 months) in my shop/product pages the displayed price is the one of the variation (Pay over 12 months which is for ex. "From $19.9/month"). I want to show the price of the other variation (Pay once) which is the full price of the product. How can i do that?
I've tried this but it's not working
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
$price = '';
$price .= woocommerce_price($product->max_variation_price);
return $price;
}
FYI : i have other variable products without subscription, i just want to do that only for the subscription product.

Related

Woocommerce hide out of stock variation price from variable price range

I want to hide out of stock variation price from woocommerce variable products price-range
for example i have product that has 2 variation, one of them sold out but in price range it's still showing old price for sold out variation
i want to show price range only for available (in stock) variations
I searched a lot and did't find any solution
Found this code
add_filter( 'woocommerce_get_price_html', 'remove_price_ofs', 10, 2 );
function remove_price_ofs( $price, $product ) {
if ( ! $product->is_in_stock() && (is_product() || is_product_category() || is_shop() )) {$price = '';}
return $price;
}
but it's only hide price from product itself, but in price range it's still showing out of stock variation price
Thanks all in advance for help!

Woocommerce subscription WC()->cart->add_to_cart monthly is not working

We are selecting the number of products to insert in cart. We are using woocommerce subscription plugin for variation on yearly and monthly basis.
if We are selecting 5 products to insert in cart we are using the while loop for getting variation product related to monthly and yearly with count 5 and using code to insert in cart as.
WC()->cart->add_to_cart( $parent_id, $quantity = 1, $variation_id );
it is not working with this code as we want 5 different products to add in cart. but is use the code with variation_id it is working but the product remains the same.
WC()->cart->add_to_cart( $parent_id, $quantity = 1, $variation_id = 2918 );
this code is working but with the same product 5 times.
Need help for it Thanks

Coupon with 2 different percentage discounts based on product category in Woocommerce

I am looking for a Woocommerce hook that will help to change the discount percentage based on 2 different product category restrictions when a specific coupon is applied.
For example if customer add a specific coupon I will like to:
If a cart item is from product category A then it will give 10% discount on that item.
if it's in product category B it will give 20% discount on that item
Update total cart price
Is there any hook that could be available to achieve this? Any available action hook or filter hook?
This is my code so far:
add_filter( 'woocommerce_get_discounted_price', 'apply_coupon', 10);
function apply_coupon($price) {
global $woocommerce;
$product=$woocommerce->cart->product;
if(has_term( 'duplex-blinds', 'A' ,$product->id)){
get_product_cart_price;
10% DISCOUNT
}
if(has_term( 'duplex-blinds', 'A' ,$product->id)){
20% DISCOUNT
}
upadte total_discunt_incart($new_discount);
upadte new_price_in_cart($new_price);
upadte new_price_in_checkout($new_price);
return $price;
}
The important thing is i need to modify the total cart price , total checkout price , total discount price and discount price need to send to Paypal.
My shop have many hooks that's why woo commerce default coupon calculation is going to wrong. And i noticed that in cart page discount price is coming correctly based on the custom product value, but it not get updated from the original cart amount, so the total price remain the same.
But in checkout page discount price is calculated based on the product original price not the product custom price so the discount is coming wrong and also it is not minimize from the total price also...
The following is a completely different way to make that works… This answer code will enable a coupon code with 2 different discounts percentage based on 2 specific product categories.
Lest say for example that your related product categories are:
For the coupon discount of 10%, the product category slug will be 'hoodies'
For the coupon discount of 20%, the product category slug will be 't-shirts'
(you can use product category Ids, slugs or Names in the code)
That will need 2 Steps:
Coupon settings (Set correctly your coupon code):
Discount type: Percentage
Amount: 10
Restrictions > Product categories (names displayed): "Hoodies" and "T shirts"
You can have other settings if you need
The settings inside the code function:
Coupon code: set your coupon code in lowercase
The 't-shirts' product category slug (for 20% of discount).
Now Here comes the code (where you will add your settings):
add_filter( 'woocommerce_coupon_get_discount_amount', 'alter_shop_coupon_data', 20, 5 );
function alter_shop_coupon_data( $round, $discounting_amount, $cart_item, $single, $coupon ){
## ---- Your settings ---- ##
// Related coupons codes to be defined in this array (you can set many)
$coupon_codes = array('10percent');
// Product categories at 20% (IDs, Slugs or Names) for 20% of discount
$product_category20 = array('hoodies'); // for 20% discount
$second_percentage = 0.2; // 20 %
## ---- The code: Changing the percentage to 20% for specific a product category ---- ##
if ( $coupon->is_type('percent') && in_array( $coupon->get_code(), $coupon_codes ) ) {
if( has_term( $product_category20, 'product_cat', $cart_item['product_id'] ) ){
$original_coupon_amount = (float) $coupon->get_amount();
$discount = $original_coupon_amount * $second_percentage * $discounting_amount;
$round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
}
}
return $round;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Here is an illustrated real working example (with screen shots):
The 1st cart item (from 'hoodies' product category) get 10% of discount $40 x 10% = $4
The 2nd cart item (from 't-shirts' product category) get 20% of discount $30 x 20% = $6
So the total discount is $4 + $6 = $10 … That works!
I using this code, but it give 100% on the 20% category, but the code you provide does work if you change the 0.2 to 0.02
$second_percentage = 0.2; // 20 %
change too:
$second_percentage = 0.02; // 20 %
or:
$second_percentage = 0.015; // 15 %
If you need 15% discount

Adding Custom price with woocomerce product price in Cart & Checkout

I have added a custom field in the woocommerce which has extra price of frames for pictures, now if picture price is 10$ and user selects a frame it will add up 5$ let say and total will be 15$.
Now if i add another product it's selected custom frame price should get added.
e.g Product 1 price is: 10$ and the frame slected on it is:frame1 whos price is 5$ so total will be 15$ for that product and if Product 2 is added with price 10$ and selected frame2 with its price is 6$ total of that product will be 16$ however grandtotal will be 31$
The solution which is near to what i'm trying to do is:
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values )
{
$price += $_SESSION['framed_price'];
return $price;
}
}
I'm storing the frame value in session and it gets updated every time user click on the frame i'm using ajax on that and till that everything is working fine. i'm getting values as well.
This function is basically iterating over the added products and adding the last frame price to every product in the cart.
How do we add the product price with it's custom frame price?
I found an answer to this, that's solve the issue:
// Change the line total price
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values ) {
// You have all your data on $values;
$price += 10;
return $price;
}
// wc_price => format the price with your own currency
function display_discounted_price( $values, $item ) {
return wc_price( $item[ 'line_total' ] );
}
Reference: woocommerce, how can i add additional cost in cart product total price?

How to dynamically change the price of a product in Woocommerce - WordPress?

I need to change the price of my products in my store, with a 10% discount, if my customer is from some specific place, so, I wrote this code:
add_filter('woocommerce_price_html', 'my_price_edit');
function my_price_edit() {
$product = new WC_Product( get_the_ID() );
$price = $product->price;
echo $price * 0.9;
}
Ok, it works! But when in the checkout the price are normals without the 10% discount!
Does have some hook for change the price of the products in the checkout area or some different code to change correctly in the both (in the product page and checkout)?
New in Woocommerce too.
Your question looks really similiar to this one.
Adding Custom price with woocomerce product price in Cart & Checkout
I think you need to use the woocommerce_cart_item_subtotal hook to change the price in the cart directly (not exactly as a parameter)
I made a slightly modification to the code (changed price formula). I think that may help you.
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values ) {
// You have all your data on $values;
$price = $price*.09;
return $price;
}
// wc_price => format the price with your own currency
function display_discounted_price( $values, $item ) {
return wc_price( $item[ 'line_total' ] );
}

Categories