Ecommerce Shopify WordPress Discussion

How to Apply Discount Based on Shipping Method in Woocommerce [duplicate]

This question already has answers here: Discount for specific shipping methods in WooCommerce checkout (2 answers) Local pickup shipping option custom percentage discount in Woocommerce (1 answer) Closed yesterday. I want to give my customers a 20 percent discount when they choose the "pick up in store" shipping method in Woocommerce. I used the code below but I got an error. How can I apply it only in the "delivery from store" option? Wordpres: 6.4.2 Woocommerce: 8.4.0 // Sepet tablosuna bir sütun ekleyerek "mağzadan teslim" seçeneğini kontrol et function add_delivery_option_to_cart() { ?> <script type="text/javascript"> jQuery(document).ready(function ($) { $('body').on('change', 'input[name="shipping_method[0]"]', function () { var is_store_pickup = $(this).val() === 'local_pickup:1'; // "local_pickup:1" mağzadan teslim seçeneğinin değeridir $.ajax({ type: 'POST', url: wc_checkout_params.ajax_url, data: { 'action': 'is_store_pickup', 'is_store_pickup': is_store_pickup, }, success: function (response) { $('body').trigger('update_checkout'); }, }); }); }); </script> <?php } add_action('wp_footer', 'add_delivery_option_to_cart'); // Mağzadan teslim seçildiyse sepet toplamına indirim uygula function apply_discount_for_store_pickup() { if (is_checkout() && !is_wc_endpoint_url()) { ?> <script type="text/javascript"> jQuery(document).ready(function ($) { $(document.body).on('updated_checkout', function () { var is_store_pickup = $('input[name="shipping_method[0]"]:checked').val() === 'local_pickup:1'; if (is_store_pickup) { var discount_percentage = 20; // İndirim yüzdesi var cart_total = parseFloat($('span.woocommerce-Price-amount.amount').text().replace(/[^\d.-]/g, '')); var discount_amount = (cart_total * discount_percentage) / 100; var discounted_total = cart_total - discount_amount; $('span.woocommerce-Price-amount.amount').html('<span class="woocommerce-Price-currencySymbol">' + $('span.woocommerce-Price-amount.amount').find('.woocommerce-Price-currencySymbol').html() + '</span>' + discounted_total.toFixed(2)); } }); }); </script> <?php } } add_action('wp_footer', 'apply_discount_for_store_pickup'); // AJAX işlemleri için fonksiyon function is_store_pickup() { if (isset($_POST['is_store_pickup'])) { WC()->session->set('is_store_pickup', $_POST['is_store_pickup']); echo 'success'; } die(); } add_action('wp_ajax_is_store_pickup', 'is_store_pickup'); add_action('wp_ajax_nopriv_is_store_pickup', 'is_store_pickup');
This True Answer // Mağzadan teslim seçildiyse sepet toplamına indirim uygula function apply_discount_for_store_pickup( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Hedeflenen "Mağzadan Teslim" seçeneği $target_shipping_method = 'local_pickup:1'; // Varsa sepetin içeriğini kontrol et if ( WC()->session->__isset( 'chosen_shipping_methods' ) ) { // Seçilen gönderim yöntemlerini al $chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); // "Mağzadan Teslim" seçeneği sepete eklendiyse if ( in_array( $target_shipping_method, $chosen_methods ) ) { $discount_percentage = 18; // İndirim yüzdesi // İndirim miktarını hesapla $discount_amount = $cart->subtotal * $discount_percentage / 100; // İndirimi sepete uygula $cart->add_fee( 'Mağzadan Teslim Alacağınız İçin İndirim', -$discount_amount ); } } } add_action( 'woocommerce_cart_calculate_fees', 'apply_discount_for_store_pickup', 10, 1 );

January 17, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.