Display variation prices per month in Woocommerce subscriptions - php

I have three subscription products, yearly, half-yearly and monthly. By default, prices are shown per year, per every 6 months and per month on my Shop page.
Based on "Change product prices via a hook in WooCommerce 3" answer code, I am trying to display all the prices per month, so to change the price display based on the subscription term:
// Utility function to change the prices with a multiplier (number)
function get_price_multiplier($var_product) {
switch($var_product) {
case 111:
// Annual
return 12;
break;
case 222:
// Semiannual
return 6;
break;
case 333:
// Month
return 1;
break;
default:
return 1;
break;
}
}
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $product ) {
$var_product = $product->get_id();
return $price / get_price_multiplier($var_product);
}
This worked but when a product is added to the cart, the price is not the regular price but the modified price from the function above.
Based on "Set programmatically product sale price and cart item prices in Woocommerce 3" answer code, I have been able to fix that:
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
function set_cart_item_sale_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Iterate through each cart item
foreach( $cart->get_cart() as $cart_item ) {
$regular_price = $cart_item['data']->get_regular_price();
$variation_id = $cart_item['variation_id'];
$cart_item['data']->set_price( $regular_price * get_price_multiplier($variation_id) );
}
}
This reset the cart price and it seems to work although one of the prices is off by a penny.
Is this all convoluted and prone to problems or is it a solid way to achieve what I am after: to show the price per subscription term on the shop page?

You are doing it in the right way… There is 2 ways (the last is the best one):
Note: The calculation and the price change is not needed when the multiplier is equal to 1.
1) First alternative: Improved code version that is very similar to yours.
Here is my commented code:
// Utility function that increase conditionally the variation price with a multiplier (int)
function get_variation_calculated_price( $variation_id, $price, $multiplier = true ) {
switch( $variation_id ) {
case 111: // Annual
$rate = 12;
break;
case 222: // Semi-annual
$rate = 6;
break;
default: // Month (and others)
$rate = 1;
break;
}
// Return calculated price (or false when multiplier is 1, as calculation is not needed)
return $rate !== 1 ? ( $multiplier ? $price * $rate : $price / $rate ) : false;
}
// Change variations calculated prices
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $variation ) {
if( $new_price = get_variation_calculated_price( $variation->get_id(), $price, false ) )
return $new_price;
return $price;
}
// Customizing cart item prices
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
function set_cart_item_sale_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Required since Woocommerce version 3.2 for cart items properties changes
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
// Only for variations
if( $cart_item['variation_id'] > 0 ) {
if( $new_price = get_variation_calculated_price( $cart_item['variation_id'], $cart_item['data']->get_price() ) ) {
$cart_item['data']->set_price( $new_price );
}
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) Second Alternative: much more accurate and lighter, restricting the product price avoiding price change on cart, checkout and backend.
Note: You will need to avoid displaying Cross-sells products in cart page (as the prices will not be changed like in shop page).
So the code will be more light, compact and efficient:
// Utility function that increase conditionally the variation price with a multiplier (int)
function get_variation_calculated_price( $variation_id, $price ) {
switch( $variation_id ) {
case 111: // Annual
$rate = 12;
break;
case 939: // Semi-annual
$rate = 6;
break;
default: // Month (and others)
$rate = 1;
break;
}
// Return calculated price (or false when multiplier is 1, as calculation is not needed)
return $rate !== 1 ? $price / $rate : false;
}
// Change variations calculated prices
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $variation ) {
// Not in cart, checkout and admin
if( is_cart() || is_checkout() || is_admin() )
return $price;
if( $new_price = get_variation_calculated_price( $variation->get_id(), $price ) )
return $new_price;
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Related

Add or remove a free product based on WooCommerce cart total and month range

I am on a project, which needs to add to cart a free item on particular promotional month. Therefore, I need to make the product value 0.00 and add it automatically to cart in a particular promotional month. So far, I have added the product automatically when the add to cart total reaches certain amount
/*
* Automatically adding the product to the cart when cart total amount reach to $500.
*/
function aapc_add_product_to_cart() {
global $woocommerce;
$cart_total = 500;
if ( $woocommerce->cart->total >= $cart_total ) {
if ( ! is_admin() ) {
$free_product_id = 12989; // Product Id of the free product which will get added to cart
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $free_product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $free_product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $free_product_id );
}
}
}
}
add_action( 'template_redirect', 'aapc_add_product_to_cart' );
...
Try the following instead (that handle dynamic changes in cart page and also a month range).
Note: the total amount for the threshold can be only the cart items total.
The code:
function is_free_product_allowed_for_month() {
// Define allowed months in the array (values from 1 to 12)
$allowed_months = array('3', '7', '8');
return in_array( date('n'), $allowed_months );
}
add_action( 'woocommerce_before_calculate_totals', 'add_remove_free_product' );
function add_remove_free_product( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Check if we are in an allowed month
if ( ! is_free_product_allowed_for_month() )
return;
$free_product_id = 339; // ID of the free product
$threshold_amount = 200; // The threshold amount for cart subtotal
$cart_items_total = 0; // Initializing
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
// Check if the free product is in cart
if ( in_array( $free_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$cart_item['data']->set_price(0); // Set price to Zero
$free_item_key = $cart_item_key;
}
// Get cart subtotal incl. tax from items (with discounts if any)
$cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];
}
// If Cart total is up to the defined amount and if the free products is not in cart, we add it.
if ( $cart_items_total >= $threshold_amount && ! isset($free_item_key) ) {
$cart->add_to_cart( $free_product_id );
}
// If cart total is below the defined amount and free product is in cart, we remove it.
elseif ( $cart_items_total < $threshold_amount && isset($free_item_key) ) {
$cart->remove_cart_item( $free_item_key );
}
}
// For minicart displayed free product price
add_filter( 'woocommerce_cart_item_price', 'free_product_cart_item_price', 10, 3 );
function free_product_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
// Check if we are in an allowed month
if ( ! is_free_product_allowed_for_month() )
return $price_html;
$free_product_id = 339; // ID of the free product
if ( in_array( $free_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
return wc_price( 0 );
}
return $price_html;
}
Code goes in functions.php file of the active child theme (or active theme). It should work.
Related: Add or remove specific cart Item based on WooCommerce cart total

WooCommerce buy one get one 50% off, excluding a product variation

I am using WooCommerce discount: buy one get one 50% off with a notice answer code, if customer purchases a specific product, customer get 50% discount off on the 2nd item.
Now If the targeted product id is a variable product with for example 3 variations "A", "B" and "C", how can I exclude for example the variation "A" from discount calculation? Where should put that condition?
The following will handle variation exclusion from the original answer code:
add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
function add_custom_discount_2nd_at_50( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// YOUR SETTINGS:
$variable_product_id = 40; // <== HERE your targeted variable product ID
$excl_variations_ids = array( 41, 42); // <== HERE your variations to be excluded
// Initializing variables
$discount = $qty_notice = 0;
$items_prices = array();
// Loop through cart items
foreach ( $cart->get_cart() as $key => $cart_item ) {
if( $variable_product_id == $cart_item['product_id'] && in_array( $cart_item['variation_id'], $excl_variations_ids ) ) {
$quantity = (int) $cart_item['quantity'];
$qty_notice += $quantity;
for( $i = 0; $i < $quantity; $i++ ) {
$items_prices[] = floatval( $cart_item['data']->get_price());
}
}
}
$count_items = count($items_prices); // Count items
rsort($items_prices); // Sorting prices descending order
if( $count_items > 1 ) {
foreach( $items_prices as $key => $price ) {
if( $key % 2 == 1 )
$discount -= number_format( $price / 2, 2 );
}
}
// Applying the discount
if( $discount != 0 ){
$cart->add_fee('Buy one get one 50% off' ,$discount ); // true
// Displaying a custom notice (optional)
wc_clear_notices(); // clear other notices on checkout page.
if( ! is_checkout() ){
wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');
}
}
// Display a custom notice on cart page when quantity is equal to 1.
elseif( $qty_notice == 1 ){
wc_clear_notices(); // clear other notices on checkout page.
if( ! is_checkout() ){
wc_add_notice( __( "Add one more to get 50% off on 2nd item" ), 'notice');
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Cart discount for product that cost less in Woocommerce

How can I apply a discount in products cart for product that costs less?
For example:
I have two product in cart: one cost 150$ and one 200$. I would like to apply a 10 percent discount only for product that cost less, in this case the first one.
I have this code but it works only for the second product in cart:
add_filter( 'woocommerce_before_calculate_totals', 'discount_on_2nd_cart_item', 10, 1 );
function discount_on_2nd_cart_item( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Initialising
$count = 0;
$discount = 0.10; // 10 %
$discounted = 0;
// Iterating though each cart items
foreach ( $cart_object->get_cart() as $cart_item ) {
$count++;
if( 2 == $count){ // Second item only
$price = $cart_item['data']->get_price(); // product price
$discounted_price = $price - ($price * $discount); // calculation
$discounted = $price - $discounted_price;
// Set the new price
//$cart_item['data']->set_price( $discounted_price );
break; // stop the loop
}
}
$cart_object->add_fee( "Discount (10%) on second product", -$discounted, true );
}
For Cart fees, you should use woocommerce_cart_calculate_fees dedicated hook instead this way:
add_action('woocommerce_cart_calculate_fees', 'discount_on_cheapest_cart_item', 20, 1 );
function discount_on_cheapest_cart_item( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only for 2 items or more
if ( $cart->get_cart_contents_count() < 2 ) return;
// Initialising
$percentage = 10; // 10 %
$discount = 0;
$item_prices = array();
// Loop though each cart items and set prices in an array
foreach ( $cart->get_cart() as $cart_item ) {
$product_prices_excl_tax[] = wc_get_price_excluding_tax( $cart_item['data'] );
}
sort($product_prices_excl_tax);
$discount = reset($product_prices_excl_tax) * $percentage / 100;
$cart->add_fee( "Discount on cheapest (".$percentage."%)", -$discount );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

New cart item price based on a GET parameter in Woocommerce

There is a field on the main page with a numerical parameter:
length
and button - add to cart like:
add to cart
I would like to calculate the price of the goods from this parameter.
I understood how to change the price when adding to the basket but I can not understand how to transfer the numerical parameter of the field to the function in functions.php
The biggest problem is that the field is not on the single product page.
I think that you can pass a parameter through a link by type:
add to cart
But I could not get it in function yet.
add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price' );
global $woocommerce;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if($cart_item['data']->id == ####){
$original_price = $cart_item['data']->price;
$length = $_GET('length');
$new_price = $original_price + $length;
$cart_item['data']->set_price($new_price);
}
}
}
Using the following 2 hooked functions will allow you to set a new cart item price calculated on a Get "length" parameter this way:
// Set custom text and calculated price as custom cart data in the cart item
add_filter( 'woocommerce_add_cart_item_data', 'save_new_price_in_cart_object', 30, 3 );
function save_new_price_in_cart_object( $cart_item_data, $product_id, $variation_id ) {
if( ! isset( $_GET['length'] ) )
return $cart_item_data;
// Get an instance of the WC_Product object
$product = $variation_id > 0 ? wc_get_product($variation_id) : wc_get_product($product_id);
$product_price = (float) $product->get_price(); // Get the product price
// Get lenght
$length = esc_attr( $_GET['length'] );
// Set the new calculated price as custom cart data for the cart item
$cart_item_data['custom_data']['price'] = $product_price + $length;
return $cart_item_data;
}
// Set the new calculated price of the cart item
add_action( 'woocommerce_before_calculate_totals', 'set_new_cart_item_price', 90, 1 );
function set_new_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
if( isset( $cart_item['custom_data']['price'] ) ) {
// Get the new calculated price
$new_price = (float) $cart_item['custom_data']['price'];
// Set the new calculated price
$cart_item['data']->set_price( $new_price );
}
}
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works

Automatically recalculate quantity when clicking on update cart in Woocommerce

So by using the docs on the WooCommerce site I've been able to add a new product to the basket automatically which works great.
I calculate the number of products by taking the current quantity in the cart and multiplying it by a percentage modifier, this also works.
My problem comes when I update the cart with a new quantity as the bonus product quantity doesn't get updated.
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 265;
$found = false;
// Get the current cart quantity
foreach ( WC()->cart->get_cart() as $cart_item ) {
$quantity = $cart_item['quantity'];
$percentage = .25;
$bonus = $quantity * $percentage;
}
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id, $bonus );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id, $bonus );
}
}
}
Does anyone have an idea on how to get this to automatically recalculate when update cart is clicked?
Preliminary remarks:
Your code doesn't work when cart is empty as $bonus variable is not defined and you get an error…
Also as you are setting a variable float number for quantity: So if the quantity is not an integer, customer can't change and update the cart items quantities.
This is kind of complex, your bonus quantity need to be updated in another hooked function when:
A product is added to cart
Customer update quantities in cart page
Customer remove items from cart
Also, there is small errors like $_product->id that should instead $_product->get_id() in WC 3+
So your revisited code with additional hooked functions will be:
add_action( 'template_redirect', 'auto_add_product_to_cart', 50 );
function auto_add_product_to_cart() {
if ( ! is_admin() ) {
$cart = WC()->cart; // <== Cart object
$product_id = 37; // <== Specific product to be auto added
$bonus_rate = .25; // <== Bonus rate
$found = false;
// Check if product already in cart
if ( ! $cart->is_empty() ) {
$contents_count = $cart->get_cart_contents_count(); // The cart items count
$bonus_qty = $contents_count * $bonus_rate; // Bonus quantity calculation
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
if ( $cart_item['data']->get_id() == $product_id ){
$found = true;
break; // Stop the loop
}
}
// Product is not found in the loop, we add it
if( ! $found )
$cart->add_to_cart( $product_id, $bonus_qty );
} else {
// There is no items in cart, we add it
$cart->add_to_cart( $product_id, $bonus_rate );
}
}
}
// Calculate, set and update bonus quantity
add_action( 'woocommerce_before_calculate_totals', 'conditional_bonus_quantity_calculation', 20, 1 );
function conditional_bonus_quantity_calculation( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$bonus_rate = .25; // <== Bonus rate
$product_id = 37; // <== Specific product to be auto added
$contents_count = $cart->get_cart_contents_count(); // Total cart items count
// Loop through cart items to check our specific product
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// If specific product is in cart
if ( $cart_item['data']->get_id() == $product_id ){
$calc_qty = $cart_item['quantity'] < 1 ? 1 : $cart_item['quantity'];
// Bonus quantity calculation
if( $contents_count > 1 )
$bonus_qty = round( $contents_count - $calc_qty ) * $bonus_rate;
else
$bonus_qty = $bonus_rate;
// Update item quantity
$cart->set_quantity( $cart_item_key, $bonus_qty, false );
}
}
}
// Allowing float numbers quantity (step by .25) for a specific product
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 20, 2 );
function custom_quantity_input_args( $args, $product ) {
// Only for your specific product ID on cart page
if( $product->get_id() != 37 && is_cart() ) return $args;
//$args['input_value'] = 0.25; // Default starting value (Optional)
$args['min_value'] = 0.25;
$args['step'] = 0.25;
$args['pattern'] = '[0-9.]*';
$args['inputmode'] = 'numeric';
return $args;
}
// Allowing float numbers in Stock management
remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');
Code goes in function.php file of the active child theme (or active theme).
Tested and works.

Categories