Servus Souri, vielen Dank für die wertvollen Tips. Ich werde das ausprobieren und berichten welche Variante für meinen Case besser funktioniert hat. Beste Grüße, Marcus
-
Diese Antwort wurde vor 3 Jahren, 1 Monat von
marcush73 geändert.
Super! Ich würd PHP-Code dem Plugin vorziehen, fallst du das schaffst. Weil du sonst ein Plugin mehr installiert hast, für nur einen kleinen extrigen Anwendungszeck und die restlichen Plugin-Funktionalitäten eh nicht brauchst.
Das war der Plan 🙂 Ich werde schauen, dass ich den Code von stackoverflow zum Laufen bekomme und sollte das aus irgendeinem Grund nicht funktionieren, wäre das Plugin Plan B. Vielen Dank nochmals!
Ok, super! Halt uns am Laufenden und viel Erfolg!
Hi zusammen, ich habe den Code von Stackoverflow ausprobiert. Allerdings wird der Wert nicht korrekt berechnet. Ich habe einen Warenkorb mit einer Zwischensumme von 14,70 €.
5% davon wären 0,735 €. D.h. Die Gesamtsumme müsste eigentlich 13,97 € sein.
Im Shop sind es aber Gesamt 14,04 €. Warum ist das so? Hat jemand eine Idee? Ich komme nicht dahinter.
add_filter('woocommerce_package_rates', 'local_pickup_percentage_discount', 12, 2);
function local_pickup_percentage_discount( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE define the discount percentage
$percentage = 5; // 5%
$subtotal = WC()->cart->get_subtotal();
// Loop through the shipping taxes array
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Targetting "flat rate"
if( 'local_pickup' === $rate->method_id ){
// Add the Percentage to the label name (otional
$rates[$rate_key]->label .= ' ( - ' . $percentage . '% )';
// Get the initial cost
$initial_cost = $new_cost = $rates[$rate_key]->cost;
// Calculate new cost
$new_cost = -$subtotal * $percentage / 100;
// Set the new cost
$rates[$rate_key]->cost = $new_cost;
// Taxes rate cost (if enabled)
$taxes = [];
// Loop through the shipping taxes array (as they can be many)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
// Get the initial tax cost
$initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
// Get the tax rate conversion
$tax_rate = $initial_tax_cost / $initial_cost;
// Set the new tax cost
$taxes[$key] = $new_cost * $tax_rate;
$has_taxes = true; // Enabling tax
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
Grüße, Marcus