Home / Admin / Local pickup custom percentage discount in WooCommerce checkout
Duplicate Snippet

Embed Snippet on Your Site

Local pickup custom percentage discount in WooCommerce checkout

Offering discounts for local pickup is a great way to attract customers who are looking for an easy and convenient option. This is especially true for those people who live in the area and don’t want to have to drive across town to pick up their online order.

How to Offer Local Pickups and Save Your Customers Time and Money
We all know the pain of waiting for a package to be delivered. It can take days, weeks, even months for some deliveries. Imagine how frustrating it is when you are waiting for something that is urgent and can’t wait.

Local pickups provide a solution to this problem by allowing customers the opportunity to pick up their order in person. This way, they can get their order right away and save time and money on shipping costs.
There are many benefits that come with offering local pickups:
– Customers get their order faster because they don’t have to wait for it to be shipped;
– Customers save money on delivery fees;
– You offer your customers a more personalized experience when they pick up their orders in person; and
– You have more control over the customer’s experience from start to finish because you are able to manage the pickup location and process.

Code Preview
php
<?php
// Add discount for Local Pickup shipping method
add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_method', 10, 1 );
function custom_discount_for_pickup_shipping_method( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }
    $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
    if ( !empty($chosen_shipping_methods) ) {
        $chosen_shipping_method = $chosen_shipping_methods[0];
        if ( strpos( $chosen_shipping_method, 'local_pickup' ) !== false ) {
            $percentage = get_option( 'pickup_discount_value', 5 );
            $discount = ( $cart->subtotal * $percentage ) / 100;
            $cart->add_fee( __('Pickup discount') . ' (' . $percentage . '%)', -$discount );
        }
    }
}
// Add a custom tab in WooCommerce settings to set the percentage discount value
add_filter( 'woocommerce_settings_tabs_array', 'add_pickup_discount_settings_tab', 50 );
function add_pickup_discount_settings_tab( $tabs ) {
    $tabs['pickup_discount'] = 'Pickup Discount';
    return $tabs;
}
// Display the content of the custom tab in WooCommerce settings
add_action( 'woocommerce_settings_tabs_pickup_discount', 'pickup_discount_settings_page' );
function pickup_discount_settings_page() {
    if ( isset( $_POST['pickup_discount_value'] ) ) {
        $discount_value = floatval( $_POST['pickup_discount_value'] );
        update_option( 'pickup_discount_value', $discount_value );
    }
    if ( isset( $_POST['pickup_show_notice'] ) ) {
        $show_notice = sanitize_text_field( $_POST['pickup_show_notice'] );
        update_option( 'pickup_show_notice', $show_notice );
    }
    if ( isset( $_POST['pickup_custom_notice_before'] ) ) {
        $custom_notice_before = sanitize_text_field( $_POST['pickup_custom_notice_before'] );
        update_option( 'pickup_custom_notice_before', $custom_notice_before );
    }
    if ( isset( $_POST['pickup_custom_notice_after'] ) ) {
        $custom_notice_after = sanitize_text_field( $_POST['pickup_custom_notice_after'] );
        update_option( 'pickup_custom_notice_after', $custom_notice_after );
    }
    $discount_value = get_option( 'pickup_discount_value', 5 );
    $show_notice = get_option( 'pickup_show_notice', 'yes' );
    $custom_notice_before = get_option( 'pickup_custom_notice_before', 'Get' );
    $custom_notice_after = get_option( 'pickup_custom_notice_after', '% off for in-store pickup (Local Pickup only)!' );
    ?>
    <h2>Pickup Discount Settings</h2>
    <table class="form-table">
        <tr valign="top">
            <th scope="row">Percentage Discount:</th>
            <td>
                <div style="display: flex; align-items: center;">
                    <input type="number" id="pickup_discount_value" name="pickup_discount_value" step="0.1" min="0" max="100" value="<?php echo esc_attr( $discount_value ); ?>" style="width: 60px;" />
                    <span style="margin-left: 5px;">%</span>
                </div>
            </td>
        </tr>
        <tr valign="top">
            <th scope="row">Show Notice on Single Product Page:</th>
            <td>
                <label>
                    <input type="checkbox" name="pickup_show_notice" id="pickup_show_notice" value="yes" <?php checked( $show_notice, 'yes' ); ?> />
                    Yes
                </label>
            </td>
        </tr>
        <tr valign="top">
            <th scope="row">Customize Single Product Notice:</th>
            <td>
                <label for="pickup_custom_notice_before">Before %:</label>
                <input type="text" id="pickup_custom_notice_before" name="pickup_custom_notice_before" value="<?php echo esc_attr( $custom_notice_before ); ?>" style="width: 100px;" />
                <label for="pickup_custom_notice_after">After %:</label>
                <input type="text" id="pickup_custom_notice_after" name="pickup_custom_notice_after" value="<?php echo esc_attr( $custom_notice_after ); ?>" style="width: 300px;" />
            </td>
        </tr>
    </table>
    <?php
}
// Save the settings when the WooCommerce settings are saved
add_action( 'woocommerce_update_options', 'save_pickup_discount_settings' );
function save_pickup_discount_settings() {
    if ( isset( $_POST['pickup_discount_value'] ) ) {
        $discount_value = floatval( $_POST['pickup_discount_value'] );
        update_option( 'pickup_discount_value', $discount_value );
    }
    // Check if the checkbox is checked and set the option accordingly
    $show_notice = isset( $_POST['pickup_show_notice'] ) ? 'yes' : 'no';
    update_option( 'pickup_show_notice', $show_notice );
    if ( isset( $_POST['pickup_custom_notice_before'] ) ) {
        $custom_notice_before = sanitize_text_field( $_POST['pickup_custom_notice_before'] );
        update_option( 'pickup_custom_notice_before', $custom_notice_before );
    }
    if ( isset( $_POST['pickup_custom_notice_after'] ) ) {
        $custom_notice_after = sanitize_text_field( $_POST['pickup_custom_notice_after'] );
        update_option( 'pickup_custom_notice_after', $custom_notice_after );
    }
}
// Output JavaScript to update the notice dynamically
add_action( 'wp_footer', 'update_single_product_notice_script' );
function update_single_product_notice_script() {
    $show_notice = get_option( 'pickup_show_notice', 'yes' );
    if ( $show_notice === 'yes' ) {
        $discount_value = get_option( 'pickup_discount_value', 5 );
        $custom_notice_before = get_option( 'pickup_custom_notice_before', 'Get' );
        $custom_notice_after = get_option( 'pickup_custom_notice_after', '% off for in-store pickup (Local Pickup only)!' );
        ?>
<script>
    function updateSingleProductNotice() {
        var pickupDiscountNotice = document.getElementById("pickup-discount-notice");
        var discountValue = parseFloat(jQuery("#pickup_discount_value").val());
        if (!isNaN(discountValue) && pickupDiscountNotice) {
            var notice = '<?php echo $custom_notice_before; ?> ' + discountValue + ' <?php echo $custom_notice_after; ?>';
            pickupDiscountNotice.innerHTML = notice;
        }
    }
    jQuery(document).ready(function($) {
        updateSingleProductNotice();
        $("#pickup_discount_value, #pickup_custom_notice_before, #pickup_custom_notice_after").on("change", function() {
            updateSingleProductNotice();
        });
        // Toggle the notice on checkbox change
        var showNoticeCheckbox = $("#pickup_show_notice");
        showNoticeCheckbox.on("change", function() {
            var showNotice = $(this).prop("checked");
            $("#pickup-discount-notice").toggle(showNotice);
            // Update the notice content when toggling the checkbox
            if (showNotice) {
                updateSingleProductNotice();
            }
        });
        // Trigger the checkbox change event initially
        showNoticeCheckbox.trigger("change");
    });
</script>
        <?php
    }
}
// Add notice on single product page after short description and style it with CSS
add_action( 'woocommerce_single_product_summary', 'add_pickup_discount_notice', 15 );
function add_pickup_discount_notice() {
    global $product;
    $show_notice = get_option( 'pickup_show_notice', 'yes' );
    if ( $show_notice === 'yes' ) {
        $discount_value = get_option( 'pickup_discount_value', 5 );
        $custom_notice_before = get_option( 'pickup_custom_notice_before', 'Get' );
        $custom_notice_after = get_option( 'pickup_custom_notice_after', '% off for in-store pickup (Local Pickup only)!' );
        // Style the notice with CSS
        echo '<style>
            .pickup-discount-notice {
                display: block;
                margin: 10px auto;
                                padding: 5px 10px;
                font-size: 16px;
                color: #fff;
                background-color: #FF9800;
                border-radius: 5px;
                text-align: center; /* Center-align the notice */
            }
        </style>';
        // Display the notice with custom text and percentage discount
        echo '<div class="pickup-discount-notice" id="pickup-discount-notice">' . $custom_notice_before . ' ' . $discount_value . ' ' . $custom_notice_after . '</div>';
        // Calculate the discount
        $discount = ( $discount_value / 100 ) * $product->get_regular_price();
        // Apply the discount to the product price
        $product->set_sale_price( $product->get_regular_price() - $discount );
        $product->set_price( $product->get_regular_price() - $discount );
    }
}

Comments

Add a Comment