Show Role Based Discount on Product Page - php

I have multiple discount tiers on my site, based on user role. After much digging and scratching and a little help from a friend I found some code that applies this discount perfectly on the cart page. Is there a way to also display this on the product page?
We used the code found here - Apply a discount for a specific user role in Woocommerce
Thanks to LoicTheAztec.
This is what we are currently using:
// Reseller Discounts
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
// Discount for Reseller_45
if ( ! current_user_can('wcwp_reseller_45') )
return; // Exit
$percentage = 45;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
// Discount for Reseller_40
if ( ! current_user_can('wcwp_reseller_40') )
return; // Exit
$percentage = 40;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
// Discount for Reseller_35
if ( ! current_user_can('wcwp_reseller_35') )
return; // Exit
$percentage = 35;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
// Discount for Reseller_30
if ( ! current_user_can('wcwp_reseller_30') )
return; // Exit
$percentage = 30;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
// Discount for Reseller_25
if ( ! current_user_can('wcwp_reseller_25') )
return; // Exit
$percentage = 25;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
// Discount for Reseller_20
if ( ! current_user_can('wcwp_reseller_20') )
return; // Exit
$percentage = 20;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
// Discount for Reseller_15
if ( ! current_user_can('wcwp_reseller_15') )
return; // Exit
$percentage = 15;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
// Discount for Reseller_10
if ( ! current_user_can('wcwp_reseller_10') )
return; // Exit
$percentage = 10;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}

Update....
I have had to change to coupons in order to integrate with our POS system (Pastel),
So created role based coupons that are auto applied.

Related

What code snippet is used to call a functions.php price change script in wp allimport

I have installed a PHP script into the functions.php file and now I need to call it from the price field for import have no clue on which code to use? Below is the function.PHP code\
I am new to this so I am learning have checked 20 allimport help posts but can not connect the dots Thank you in advance XMark
function price_increment( $price ) {
if($price <= 20){
$adjustment = .15;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
} else if($price <= 50){
$adjustment = .12;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
} else if($price <= 100){
$adjustment = .07;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
} else {
$adjustment = .05;
$new_price = $price * $adjustment;
$finalPrice = $price + $new_price;
return round( $finalPrice, 2 );
}
}

Wp All Import Function Editor

I'm trying to import a csv file with wp all import set specific functions to set the sale price and the offer price.
Now I would need to call the function that forms the regular price, inside the function of the offer price, to work on the result and maybe apply a percentage discount.
<?php
$GLOBALS['$commissione_paypal_lm'] = 4;
$GLOBALS['$spese_spedizione_moto_lm'] = 10;
$GLOBALS['$spese_spedizione_autocarro_lm'] = 10;
$GLOBALS['$spese_spedizione_varie_lm'] = 7;
$GLOBALS['$pfu_moto_lm'] = 1.50;
$GLOBALS['$pfu_autocarro_lm'] = 4.90;
$GLOBALS['$pfu_varie_lm'] = 2.90;
// First function for regular price,i take variables from csv and global variables
function prezzo_finale( $price = null, $pfu = null, $diametro = null ) {
if ( !empty( $price ) ) {
// strip any extra characters from price
$price = preg_replace("/[^0-9,.]/", "", $price);
$pfu = preg_replace("/[^0-9,.]/", "", $pfu);
//price ommas and dots fix
$price = str_replace(",",".",str_replace(".","",$price));
$pfu = str_replace(",",".",str_replace(".","",$pfu));
// calculate percentage
$percent = 0;
if ($diametro != '') {
$term = term_exists( $diametro, 'pa_diametro', 0 );
if ( $term !== 0 && $term !== null ) {
$percent = get_field('percentage', 'pa_diametro_' . $term["term_id"]);
}
}
// final price
$prezzo_finale = $price;
if (empty( $percent ) ) {
// Se il campo percentuale è vuoto metto 20% automatico
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu + $prezzo_appoggio_finale);
}else if ($percent > 0){
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * ($percent / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * ($percent / 100), 2) + $pfu + $prezzo_appoggio_finale);
}else{
// Se il campo percentuale è inferiore uguale a zero metto 20% automatico
$prezzo_appoggio_finale = round((($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu ) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale = ($prezzo_finale + round($prezzo_finale * (20 / 100), 2) + $pfu + $prezzo_appoggio_finale);
}
// perform calculations
return $prezzo_finale;
}
}
function prezzo_finale_lm( $fifo_ponderato = null, $diametro = null, $settori_codice= null ) {
if ( !empty( $fifo_ponderato ) ) {
// strip any extra characters from price
$fifo_ponderato = preg_replace("/[^0-9,.]/", "", $fifo_ponderato);
//price ommas and dots fix
$fifo_ponderato = str_replace(",",".",str_replace(".","",$fifo_ponderato));
// calculate percentage
$percent = 0;
if ($diametro != '') {
$term = term_exists( $diametro, 'pa_diametro', 0 );
if ( $term !== 0 && $term !== null ) {
$percent = get_field('percentage', 'pa_diametro_' . $term["term_id"]);
}
}
// final price
// moto = 10; vettura 8; isole extra valutare; paypal 3%;
$prezzo_finale_lm = ($fifo_ponderato);
if ($settori_codice === 'MOTO' || $settori_codice === 'SCOOTER' || $settori_codice === 'CICLOMOTORI'){
$spese_spedizione = $GLOBALS['$spese_spedizione_moto_lm'];
$pfu = $GLOBALS['$pfu_moto_lm'];
}else if ($settori_codice === 'AUTOCARRO'){
$spese_spedizione = $GLOBALS['$spese_spedizione_autocarro_lm'];
$pfu = $GLOBALS['$pfu_autocarro_lm'];
}else{
$spese_spedizione = $GLOBALS['$spese_spedizione_varie_lm'];
$pfu = $GLOBALS['$pfu_varie_lm'];
}
if (empty( $percent ) ) {
// Se il campo percentuale è vuoto metto 20% automatico
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}else if ($percent > 0){
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * ($percent / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * ($percent / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}else{
// Se il campo percentuale è inferiore uguale a zero metto 20% automatico
$prezzo_appoggio_lm = round((($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione) * $GLOBALS['$commissione_paypal_lm'])/100,2) ;
$prezzo_finale_lm_paypal = ($prezzo_finale_lm + round($prezzo_finale_lm * (20 / 100), 2) + $pfu + $spese_spedizione + $prezzo_appoggio_lm);
}
// perform calculations
return $prezzo_finale_lm_paypal;
}
}
// Second function for offer price,i would take regular price and work with
function prezzo_offerta_lm() {
if (prezzo_finale_lm()){
return prezzo_finale_lm();
// and for example apply discount as global variable
}else{
}
// perform calculations
}
?>
This is shortcode i use for regular price:
[prezzo_finale_lm({fifo_ponderato[1]},{diametro[1]},{settori_codice[1]})]
Thank you all for any advice.
<?php
// First function for regular price,i take $price variable from imported csv column
function regular_price( $price = null) {
if ( !empty( $price ) ) {
// final price
$prezzo_finale = $price;
// perform calculations as example add markup or paypal commission
return $prezzo_finale;
}
}
// Second function for offer price,where i need to get returned value from first function
function offer_price($price = null)) {
if ( !empty( $price ) ) {
$offer_price = regular_price(); // i need to return here value from first function to perform discount calculation
// perform calculations
return $offer_price;
}
}
?>

How can you express the following code to a formula

Let's say we have a code:
<?php
$maximum = 10000;
$minimum = 5000;
$size = 10000;
$step = 0.0001;
$total = 0;
$counter = $maximum;
while($counter >= $minimum)
{
$counter -= $step;
$total += $size / $maximum * $step / $counter;
}
echo "Total is: $total\n";
?>
How to express $total variable as a formula? The code acts as an integral where step is infinitesimally small.
Your integral is:
$total = integral(x from $minimum to $maximum; $size / $maximum / x);
$size / $maximum is constant and thus simply scales the integral. The closed form of the integral is therefore:
$total = $size / $maximum * (log($maximum) - log($minimum))
= $size / $maximum * log($maximum / $minimum)
Assuming that both $maximum and $minimum are positive.

PHP perfect round off

I'm trying to round off following figures:
Case 1:
round( ((4/6) * 100), 2 ) . '%'; = 66.67%
round( ((1/6) * 100), 2 ) . '%'; = 16.67%
round( ((1/6) * 100), 2 ) . '%'; = 16.67%
Total % = 66.67 + 16.67 + 16.67 = 100.01%
Case 2:
round( ((5/11) * 100), 2 ) . '%'; = 45.45%
round( ((3/11) * 100), 2 ) . '%'; = 27.27%
round( ((3/11) * 100), 2 ) . '%'; = 27.27%
Total % = 45.45 + 27.27 + 27.27 = 99.99%
Can anyone tell me how would I make it perfect 100%
Thanks
If a prefect 100 is 100.00, use this case:
round( ((4/6) * 100), 3 ); = 66.667
round( ((1/6) * 100), 3 ); = 16.667
round( ((1/6) * 100), 3 ); = 16.667
round(66.667+16.667+16.667, 2); = 100
echo round( ((5/11) * 100), 3 ); = 45.455
echo round( ((3/11) * 100), 3 ); = 27.273
echo round( ((3/11) * 100), 3 ); = 27.273
echo round(45.455+27.273+27.273, 2); = 100
You can make it more prefect by increasing rounded precision, e.g. use 15 instead of 3 for double values ;).
or
$a1 = (4/6)*100;
$b1 = (1/6)*100;
$c1 = (1/6)*100;
round($a1, 2);
round($b1, 2);
round($c1, 2);
round($a1+$b1+$c1, 2);
$a2 = (5/11)*100;
$b2 = (3/11)*100;
$c2 = (3/11)*100;
round($a2, 2);
round($b2, 2);
round($c2, 2);
round($a2+$b2+$c2, 2);
[PHP Demo]

Calculate shipping discount per extra item

I am trying to create a loop in PHP where for each extra item in a shopping cart, the shipping gets a ascending 10% discount.
This is the result I want:
Quantity = 1: Shipping = 30;
Quantity = 2: Shipping = 30 + (30 x 0.5);
Quantity = 3: Shipping = 45 + (30 x 0.4);
Quantity = 4: Shipping = 57 + (30 x 0.3);
Quantity = 5: Shipping = 69 + (30 x 0.2);
Quantity = 6: Shipping = 78 + (30 x 0.1);
Quantity = 7: Shipping = 81 + (30 x 0.1);
Quantity = 8: Shipping = 84 + (30 x 0.1);
etc.
I don't know where to place my $quantity variable now.
My PHP:
<?php
$quantity = 10;
$shipping = 30;
for( $discount = 0.5; $discount >= 0.1; $discount - 0.1 ) {
$shipping = $shipping + ( $shipping * $discount );
}
echo $shipping;
?>
you should iterate the quantity value, not the discount.
$shippingDefault = 30;
$discount = 0.5;
for ($i = 2 ; $i <= $quantity; $i++) {
if ($discount < 0.1) {
$discount = 0.1;
}
$shipping += ( $shippingDefault * $discount );
$discount -= 0.1;
}

Categories