| |
| <?php
|
|
|
| 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_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;
|
| }
|
|
|
|
|
| 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 );
|
| }
|
|
|
|
|
| $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 );
|
| }
|
| }
|
|
|
|
|
| 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();
|
| });
|
|
|
|
|
| var showNoticeCheckbox = $("#pickup_show_notice");
|
| showNoticeCheckbox.on("change", function() {
|
| var showNotice = $(this).prop("checked");
|
| $("#pickup-discount-notice").toggle(showNotice);
|
|
|
|
|
| if (showNotice) {
|
| updateSingleProductNotice();
|
| }
|
| });
|
|
|
|
|
| showNoticeCheckbox.trigger("change");
|
| });
|
| </script>
|
|
|
| <?php
|
| }
|
| }
|
|
|
|
|
| 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)!' );
|
|
|
|
|
| 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>';
|
|
|
|
|
| echo '<div class="pickup-discount-notice" id="pickup-discount-notice">' . $custom_notice_before . ' ' . $discount_value . ' ' . $custom_notice_after . '</div>';
|
|
|
|
|
| $discount = ( $discount_value / 100 ) * $product->get_regular_price();
|
|
|
|
|
| $product->set_sale_price( $product->get_regular_price() - $discount );
|
| $product->set_price( $product->get_regular_price() - $discount );
|
| }
|
| }
|
| |
| |
Comments