Plugin Directory

Changeset 3142103


Ignore:
Timestamp:
08/27/2024 08:08:21 AM (6 months ago)
Author:
Rynald0s
Message:
Location:
wc-hide-shipping-methods/trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • wc-hide-shipping-methods/trunk/hide-shipping-free-shipping.php

    r3136447 r3142103  
    11<?php
    22/**
    3  * Plugin Name: Hide Shipping Methods for WooCommerce
     3 * Plugin Name: WC Hide Shipping Methods
    44 * Plugin URI: https://wordpress.org/plugins/wc-hide-shipping-methods/
    55 * Documentation URI: https://woocommerce.com/document/hide-shipping-methods/
     
    77 * Author: Rynaldo Stoltz
    88 * Author URI: https://profiles.wordpress.org/rynald0s/
    9  * Version: 1.7
     9 * Version: 1.8
    1010 * Text Domain: wc-hide-shipping-methods
    1111 * Domain Path: /languages
     
    1313 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
    1414 * WC requires at least: 3.9.4
    15  * WC tested up to: 8.1
     15 * WC tested up to: 7.8.1
    1616 * Requires at least: 6.5
    1717 * Requires PHP: 7.4
     
    2525
    2626/**
    27  * Check if WooCommerce is active
     27 * WC_Hide_Shipping_Methods class.
     28 *
     29 * Handles the hiding of shipping methods based on the settings in WooCommerce.
    2830 */
    29 if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {
    30     return;
    31 }
     31class WC_Hide_Shipping_Methods {
    3232
    33 /**
    34  * Add settings for hiding shipping methods.
    35  *
    36  * @param array $settings WooCommerce shipping settings.
    37  * @return array Updated WooCommerce shipping settings.
    38  */
    39 function wchfsm_add_settings( $settings ) {
     33    /**
     34     * Constructor to initialize the class.
     35     */
     36    public function __construct() {
     37        // Check if WooCommerce is active, if not, show an admin notice.
     38        add_action( 'admin_notices', array( $this, 'check_woocommerce_active' ) );
    4039
    41     $settings[] = array(
    42         'title' => __( 'Shipping Method Visibility', 'wc-hide-shipping-methods' ),
    43         'type'  => 'title',
    44         'id'    => 'wc_hide_shipping',
    45     );
     40        // Add WooCommerce settings and declare compatibility.
     41        add_filter( 'woocommerce_get_settings_shipping', array( $this, 'add_settings' ), 10, 2 );
     42        add_action( 'before_woocommerce_init', array( $this, 'declare_woocommerce_compatibility' ) );
    4643
    47     $settings[] = array(
    48         'title'    => __( 'Free Shipping: ', 'wc-hide-shipping-methods' ),
    49         'desc'     => '',
    50         'id'       => 'wc_hide_shipping_options',
    51         'type'     => 'radio',
    52         'desc_tip' => true,
    53         'options'  => array(
    54             'hide_all'          => __( 'Show "Free Shipping" only. Hide all the other methods', 'wc-hide-shipping-methods' ),
    55             'hide_except_local' => __( 'Show "Free Shipping" and "Local Pickup" only (if available). Hide all the other methods.', 'wc-hide-shipping-methods' ),
    56         ),
    57     );
     44        // Register activation hook.
     45        register_activation_hook( __FILE__, array( $this, 'update_default_option' ) );
    5846
    59     $settings[] = array(
    60         'type' => 'sectionend',
    61         'id'   => 'wc_hide_shipping',
    62     );
    63     return $settings;
    64 }
    65 add_filter( 'woocommerce_get_settings_shipping', 'wchfsm_add_settings', 10, 2 );
     47        // Add plugin action links.
     48        add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
    6649
    67 // Handle hiding shipping methods based on the selected option.
    68 $hide_shipping_option = get_option( 'wc_hide_shipping_options' );
     50        // Apply filters for hiding shipping methods.
     51        $this->apply_shipping_method_filters();
     52    }
    6953
    70 if ( 'hide_all' === $hide_shipping_option ) {
     54    /**
     55     * Checks if WooCommerce is active and shows a warning if it is not.
     56     */
     57    public function check_woocommerce_active() {
     58        if ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
     59            $class   = 'error';
     60            $message = sprintf(
     61                // Translators: %s is the URL to the WooCommerce plugin.
     62                __( '<strong>WC Hide Shipping Methods is inactive.</strong> The <a href="%s" target="_blank">WooCommerce plugin</a> must be active for this plugin to work.', 'wc-hide-shipping-methods' ),
     63                esc_url( 'https://wordpress.org/plugins/woocommerce/' )
     64            );
     65            printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), wp_kses_post( $message ) );
     66        }
     67    }
     68
     69    /**
     70     * Adds custom settings to WooCommerce shipping settings.
     71     *
     72     * @param array $settings WooCommerce shipping settings.
     73     * @return array Updated WooCommerce shipping settings.
     74     */
     75    public function add_settings( $settings ) {
     76
     77        $settings[] = array(
     78            'title' => __( 'Shipping Method Visibility', 'wc-hide-shipping-methods' ),
     79            'type'  => 'title',
     80            'id'    => 'wc_hide_shipping',
     81        );
     82
     83        $settings[] = array(
     84            'title'    => __( 'Free Shipping: ', 'wc-hide-shipping-methods' ),
     85            'desc'     => '',
     86            'id'       => 'wc_hide_shipping_options',
     87            'type'     => 'radio',
     88            'desc_tip' => true,
     89            'options'  => array(
     90                'hide_all'          => __( 'Show "Free Shipping" only (if available). Hide all the other methods', 'wc-hide-shipping-methods' ),
     91                'hide_except_local' => __( 'Show "Free Shipping" and "Local Pickup" only (if available). Hide all the other methods.', 'wc-hide-shipping-methods' ),
     92            ),
     93        );
     94
     95        $settings[] = array(
     96            'type' => 'sectionend',
     97            'id'   => 'wc_hide_shipping',
     98        );
     99        return $settings;
     100    }
     101
     102    /**
     103     * Apply filters based on the selected shipping method option.
     104     */
     105    private function apply_shipping_method_filters() {
     106        $option = get_option( 'wc_hide_shipping_options', 'hide_all' ); // Default to 'hide_all' if option is not set.
     107
     108        if ( 'hide_all' === $option ) {
     109            add_filter( 'woocommerce_package_rates', array( $this, 'hide_shipping_when_free_is_available' ), 10, 2 );
     110        } elseif ( 'hide_except_local' === $option ) {
     111            add_filter( 'woocommerce_package_rates', array( $this, 'hide_shipping_when_free_is_available_keep_local' ), 10, 2 );
     112        }
     113    }
    71114
    72115    /**
     
    76119     * @return array Filtered array of shipping rates.
    77120     */
    78     function wchfsm_hide_all_methods( $rates ) {
    79         $free = array_filter(
    80             $rates,
    81             function ( $rate ) {
    82                 return 'free_shipping' === $rate->method_id;
     121    public function hide_shipping_when_free_is_available( $rates ) {
     122        $free = array();
     123        foreach ( $rates as $rate_id => $rate ) {
     124            if ( 'free_shipping' === $rate->method_id ) {
     125                $free[ $rate_id ] = $rate;
    83126            }
    84         );
    85 
     127        }
    86128        return ! empty( $free ) ? $free : $rates;
    87129    }
    88     add_filter( 'woocommerce_package_rates', 'wchfsm_hide_all_methods', 10, 2 );
    89 
    90 } elseif ( 'hide_except_local' === $hide_shipping_option ) {
    91130
    92131    /**
     
    97136     * @return array Filtered array of shipping rates.
    98137     */
    99     function wchfsm_hide_except_local( $rates, $package ) { // phpcs:ignore -- The $package parameter is retained for potential future use.
    100         $new_rates = array_filter(
    101             $rates,
    102             function ( $rate ) {
    103                 return 'free_shipping' === $rate->method_id || 'local_pickup' === $rate->method_id;
     138    public function hide_shipping_when_free_is_available_keep_local( $rates, $package ) { //phpcs:ignore  -- $package is retained for possible future use.
     139        $new_rates = array();
     140        foreach ( $rates as $rate_id => $rate ) {
     141            if ( 'free_shipping' === $rate->method_id ) {
     142                $new_rates[ $rate_id ] = $rate;
    104143            }
    105         );
     144        }
    106145
    107         return ! empty( $new_rates ) ? $new_rates : $rates;
     146        if ( ! empty( $new_rates ) ) {
     147            foreach ( $rates as $rate_id => $rate ) {
     148                if ( 'local_pickup' === $rate->method_id ) {
     149                    $new_rates[ $rate_id ] = $rate;
     150                }
     151            }
     152            return $new_rates;
     153        }
     154
     155        return $rates;
    108156    }
    109     add_filter( 'woocommerce_package_rates', 'wchfsm_hide_except_local', 10, 2 );
     157
     158    /**
     159     * Update the default option when the plugin is activated.
     160     */
     161    public function update_default_option() {
     162        update_option( 'wc_hide_shipping_options', 'hide_all' );
     163    }
     164
     165    /**
     166     * Declare plugin compatibility with WooCommerce HPOS and Cart & Checkout Blocks.
     167     */
     168    public function declare_woocommerce_compatibility() {
     169        if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
     170            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
     171            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
     172        }
     173    }
     174
     175    /**
     176     * Adds a settings link to the plugins page.
     177     *
     178     * @param array $links Array of action links.
     179     * @return array Modified array of action links.
     180     */
     181    public function plugin_action_links( $links ) {
     182        $settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=shipping&section=options' ) ) . '">' . __( 'Settings', 'wc-hide-shipping-methods' ) . '</a>';
     183        array_unshift( $links, $settings_link ); // Add the settings link to the beginning of the array.
     184        return $links;
     185    }
    110186}
    111187
    112 /**
    113  * Update the default option when the plugin is activated.
    114  */
    115 function wchfsm_set_default_option() {
    116     update_option( 'wc_hide_shipping_options', 'hide_all' );
    117 }
    118 register_activation_hook( __FILE__, 'wchfsm_set_default_option' );
    119 
    120 /**
    121  * Declare plugin compatibility with WooCommerce HPOS.
    122  */
    123 function wchfsm_declare_woocommerce_hpos_compatibility() {
    124     if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
    125         \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
    126     }
    127 }
    128 add_action( 'before_woocommerce_init', 'wchfsm_declare_woocommerce_hpos_compatibility' );
    129 
    130 /**
    131  * Declare plugin compatibility with WooCommerce Cart & Checkout Blocks.
    132  */
    133 function wchfsm_declare_woocommerce_block_compatibility() {
    134     if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
    135         \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
    136     }
    137 }
    138 add_action( 'before_woocommerce_init', 'wchfsm_declare_woocommerce_block_compatibility' );
     188// Initialize the plugin.
     189new WC_Hide_Shipping_Methods();
  • wc-hide-shipping-methods/trunk/readme.txt

    r3136447 r3142103  
    55Requires at least: 6.5.0
    66Tested up to: 6.5
    7 Stable tag: 1.7
     7Stable tag: 1.8
    88WC requires at least: 3.9.4
    99WC tested up to: 8.1
     
    2323- Easy integration with WooCommerce settings.
    2424- Compatible with WooCommerce shipping zones.
     25- Compatible with both classic and modern block-based checkout methods (i.e. Gutenberg & WooCommerce blocks).
    2526
    2627== Installation ==
     
    4041
    4142= Q: Where can I go if I find an issue or want to recommend a feature? =
    42 A: You can submit issues or feature requests on the [Public GitHub Repository](https://github.com/rynaldos/wc-hide-shipping-methods-free).
     43A: You can submit issues or feature requests on the [Public GitHub Repository](https://github.com/riaanknoetze/wc-hide-shipping-methods-free/issues).
    4344
    4445== Screenshots ==
     
    46471. Plugin settings.
    47482. Checkout showing only "Free Shipping".
    48 3. Checkout showing "Free Shipping" and "Local Pickup".
    4949
    5050== Changelog ==
     51
     52= 1.8 =
     53* New - Refactor code to use classes [#2](https://github.com/RiaanKnoetze/wc-hide-shipping-methods/issues/2)
     54* New - Add admin dependency notice when WooCommerce is disabled [#8](https://github.com/RiaanKnoetze/wc-hide-shipping-methods/issues/8)
     55* New - Add plugin action links for easy access to settings page [#1](https://github.com/RiaanKnoetze/wc-hide-shipping-methods/issues/1)
     56* Fix - Display all free shipping methods instead of just the first matched one [#3](https://github.com/RiaanKnoetze/wc-hide-shipping-methods/issues/3)
     57* Tweak - Add fallback values when checking plugin options [#2](https://github.com/RiaanKnoetze/wc-hide-shipping-methods/issues/7)
     58* Tweak - Add Local Pickup compatibility notice [#9](https://github.com/RiaanKnoetze/wc-hide-shipping-methods/issues/9)
    5159
    5260= 1.7 =
Note: See TracChangeset for help on using the changeset viewer.