Plugin Directory

Changeset 3441322


Ignore:
Timestamp:
01/17/2026 12:24:23 AM (7 weeks ago)
Author:
aisimpleplugin
Message:

release 1.3.0 Added support for Free Shipping restricted by User ID and refactored plugin structure into multiple class files for improved maintainability.

Location:
percentage-fee-shipping/trunk
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • percentage-fee-shipping/trunk/includes/class-aispfs-shipping-percentage.php

    r3412961 r3441322  
    11<?php
    2 if (!defined('ABSPATH')) {
     2if ( ! defined( 'ABSPATH' ) ) {
    33    exit;
    44}
    55
    6 if (!class_exists('WC_Shipping_Method')) {
     6if ( ! class_exists( 'WC_Shipping_Method' ) ) {
    77    return;
    88}
    9 
    10 function aispfs_skip_min_for_admin() {
    11     // Doit être admin
    12     if ( ! ( is_user_logged_in() && current_user_can( 'manage_options' ) ) ) {
    13         return false;
    14     }
    15 
    16     // Doit avoir Order Tools activé
    17     if ( ! function_exists( 'is_plugin_active' ) ) {
    18         require_once ABSPATH . 'wp-admin/includes/plugin.php';
    19     }
    20 
    21     return is_plugin_active( 'aisp-order-tools/aisp-order-tools.php' );
    22 }
    23 
    24 
    25 /**
    26  * Méthode 1 : Shipping avec pourcentage + minimum par zone
    27  */
    28 if (!class_exists('AISPFS_Shipping_Percentage')) :
    299
    3010class AISPFS_Shipping_Percentage extends WC_Shipping_Method {
     
    3414    public $min_cost;
    3515
    36     public function __construct($instance_id = 0) {
     16    public function __construct( $instance_id = 0 ) {
    3717
    38         $this->id                 = 'aispfs_percentage_fee_shipping'; 
    39         $this->instance_id        = absint($instance_id);
    40         $this->method_title       = __('AISPFS Percentage Shipping', 'percentage-fee-shipping'); 
    41         $this->method_description = __('Shipping cost based on cart percentage with optional minimum per zone.', 'percentage-fee-shipping');
     18        $this->id                 = 'aispfs_percentage_fee_shipping';
     19        $this->instance_id        = absint( $instance_id );
     20        $this->method_title       = __( 'AISPFS Percentage Shipping', 'percentage-fee-shipping' );
     21        $this->method_description = __( 'Shipping cost based on cart percentage with optional minimum per zone.', 'percentage-fee-shipping' );
    4222
    4323        $this->supports = array(
    4424            'shipping-zones',
    4525            'instance-settings',
    46             'instance-settings-modal'
     26            'instance-settings-modal',
    4727        );
    4828
     
    5535        $this->init_settings();
    5636
    57         $this->title       = $this->get_instance_option('title');
    58         $this->percentage  = floatval( $this->get_instance_option('percentage', '5') );
    59         $this->enabled     = $this->get_instance_option('enabled');
    60 
    61         // ✅ NEW OPTIONS
    62         $this->min_enabled = $this->get_instance_option('min_enabled', 'no');
    63         $this->min_cost    = floatval( $this->get_instance_option('min_cost', 25) );
     37        $this->title       = $this->get_instance_option( 'title' );
     38        $this->percentage  = floatval( $this->get_instance_option( 'percentage', '5' ) );
     39        $this->enabled     = $this->get_instance_option( 'enabled' );
     40        $this->min_enabled = $this->get_instance_option( 'min_enabled', 'no' );
     41        $this->min_cost    = floatval( $this->get_instance_option( 'min_cost', 25 ) );
    6442
    6543        add_action(
     
    7452
    7553            'enabled' => array(
    76                 'title'   => __('Enable/Disable', 'percentage-fee-shipping'),
     54                'title'   => __( 'Enable/Disable', 'percentage-fee-shipping' ),
    7755                'type'    => 'checkbox',
    78                 'label'   => __('Enable this shipping method', 'percentage-fee-shipping'),
    79                 'default' => 'yes'
     56                'label'   => __( 'Enable this shipping method', 'percentage-fee-shipping' ),
     57                'default' => 'yes',
    8058            ),
    8159
    8260            'title' => array(
    83                 'title'       => __('Method Title', 'percentage-fee-shipping'),
     61                'title'       => __( 'Method Title', 'percentage-fee-shipping' ),
    8462                'type'        => 'text',
    85                 'description' => __('This controls the title displayed at checkout.', 'percentage-fee-shipping'),
    86                 'default'     => __('Percentage Shipping', 'percentage-fee-shipping'),
    87                 'desc_tip'    => true
     63                'default'     => __( 'Percentage Shipping', 'percentage-fee-shipping' ),
     64                'desc_tip'    => true,
    8865            ),
    8966
    9067            'percentage' => array(
    91                 'title'       => __('Shipping Percentage (%)', 'percentage-fee-shipping'),
     68                'title'       => __( 'Shipping Percentage (%)', 'percentage-fee-shipping' ),
    9269                'type'        => 'number',
    93                 'css'         => 'width: 80px;',
    94                 'description' => __('Percentage of cart subtotal used for shipping.', 'percentage-fee-shipping'),
     70                'css'         => 'width:80px;',
    9571                'default'     => '9',
    96                 'custom_attributes' => array(
    97                     'min'  => '0',
    98                     'step' => '1'
    99                 )
    10072            ),
    10173
    10274            'min_enabled' => array(
    103                 'title'   => __('Enable Minimum Shipping', 'percentage-fee-shipping'),
     75                'title'   => __( 'Enable Minimum Shipping', 'percentage-fee-shipping' ),
    10476                'type'    => 'checkbox',
    105                 'label'   => __('Apply a minimum shipping cost for this zone', 'percentage-fee-shipping'),
    10677                'default' => 'no',
    10778            ),
    10879
    10980            'min_cost' => array(
    110                 'title'       => __('Minimum Shipping Cost', 'percentage-fee-shipping'),
    111                 'type'        => 'number',
    112                 'css'         => 'width: 80px;',
    113                 'description' => __('Minimum shipping fee for this zone.', 'percentage-fee-shipping'),
    114                 'default'     => '25',
    115                 'custom_attributes' => array(
    116                     'min'  => '0',
    117                     'step' => '0.01'
    118                 )
    119             )
    120 
     81                'title'   => __( 'Minimum Shipping Cost', 'percentage-fee-shipping' ),
     82                'type'    => 'number',
     83                'css'     => 'width:80px;',
     84                'default' => '25',
     85            ),
    12186        );
    12287    }
    12388
    124     public function calculate_shipping($package = array()) {
     89    public function calculate_shipping( $package = array() ) {
    12590
    12691        if ( 'no' === $this->enabled ) {
     
    12893        }
    12994
    130         $cart_subtotal = WC()->cart->get_subtotal();
    131         $cost = ($cart_subtotal * $this->percentage) / 100;
     95        $subtotal = WC()->cart->get_subtotal();
     96        $cost     = ( $subtotal * $this->percentage ) / 100;
    13297
    133         // ✅ APPLY MINIMUM PER ZONE (skip for admin when Order Tools active)
    13498        if (
    135             $this->min_enabled === 'yes'
    136             && $this->min_cost > 0
    137             && $cost < $this->min_cost
    138             && ! aispfs_skip_min_for_admin()
     99            'yes' === $this->min_enabled &&
     100            $this->min_cost > 0 &&
     101            $cost < $this->min_cost &&
     102            ! aispfs_skip_min_for_admin()
    139103        ) {
    140104            $cost = $this->min_cost;
    141105        }
    142106
    143         // ✅ MISSING PART (NOW FIXED)
    144         $rate = array(
     107        $this->add_rate( array(
    145108            'id'    => $this->get_rate_id(),
    146109            'label' => $this->title,
    147             'cost'  => round($cost, 2)
    148         );
    149 
    150         $this->add_rate( $rate );
     110            'cost'  => round( $cost, 2 ),
     111        ) );
    151112    }
    152113}
    153 
    154 endif;
    155 
    156 
    157 /**
    158  * Méthode 2 : Free Shipping
    159  */
    160 if (!class_exists('AISPFS_Shipping_Free')) :
    161 
    162 class AISPFS_Shipping_Free extends WC_Shipping_Method {
    163 
    164     public $min_amount;
    165 
    166     public function __construct($instance_id = 0) {
    167 
    168         $this->id                 = 'aispfs_free_shipping';
    169         $this->instance_id        = absint($instance_id);
    170         $this->method_title       = __('AISPFS Free Shipping', 'percentage-fee-shipping');
    171 
    172         $this->supports = array(
    173             'shipping-zones',
    174             'instance-settings',
    175             'instance-settings-modal'
    176         );
    177 
    178         $this->init();
    179     }
    180 
    181     public function init() {
    182 
    183         $this->init_form_fields();
    184         $this->init_settings();
    185 
    186         $this->title      = $this->get_instance_option('title', __('Free Shipping', 'percentage-fee-shipping'));
    187         $this->min_amount = floatval( $this->get_instance_option('min_amount', 100) );
    188         $this->enabled    = $this->get_instance_option('enabled');
    189 
    190         add_action(
    191             'woocommerce_update_options_shipping_' . $this->id,
    192             array( $this, 'process_admin_options' )
    193         );
    194     }
    195 
    196     public function init_form_fields() {
    197 
    198         $this->instance_form_fields = array(
    199 
    200             'enabled' => array(
    201                 'title'   => __('Enable/Disable', 'percentage-fee-shipping'),
    202                 'type'    => 'checkbox',
    203                 'label'   => __('Enable Free Shipping', 'percentage-fee-shipping'),
    204                 'default' => 'yes'
    205             ),
    206 
    207             'title' => array(
    208                 'title'    => __('Method Title', 'percentage-fee-shipping'),
    209                 'type'     => 'text',
    210                 'default'  => __('Free Shipping', 'percentage-fee-shipping'),
    211                 'desc_tip' => true
    212             ),
    213 
    214             'min_amount' => array(
    215                 'title'       => __('Minimum Amount', 'percentage-fee-shipping'),
    216                 'type'        => 'number',
    217                 'description' => __('Cart subtotal required to offer free shipping.', 'percentage-fee-shipping'),
    218                 'default'     => 100,
    219                 'desc_tip'    => true,
    220             )
    221 
    222         );
    223     }
    224 
    225     public function calculate_shipping($package = array()) {
    226 
    227         if ('no' === $this->enabled) {
    228             return;
    229         }
    230 
    231         $cart_subtotal = WC()->cart->get_subtotal();
    232 
    233         if ($cart_subtotal >= $this->min_amount) {
    234 
    235             $rate = array(
    236                 'id'    => $this->get_rate_id(),
    237                 'label' => $this->title,
    238                 'cost'  => 0
    239             );
    240 
    241             $this->add_rate($rate);
    242         }
    243     }
    244 }
    245 
    246 endif;
    247 
    248 
    249 /**
    250  * Register both shipping methods
    251  */
    252 add_filter('woocommerce_shipping_methods', function ($methods) {
    253 
    254     $methods['aispfs_percentage_fee_shipping'] = 'AISPFS_Shipping_Percentage';
    255     $methods['aispfs_free_shipping']          = 'AISPFS_Shipping_Free';
    256 
    257     return $methods;
    258 });
    259 
    260 
    261 /**
    262  * Remove percentage method when free shipping is active
    263  */
    264 add_filter( 'woocommerce_package_rates', function( $rates ) {
    265 
    266     $has_free = false;
    267 
    268     foreach ( $rates as $rate_id => $rate ) {
    269         if ( strpos( $rate_id, 'aispfs_free_shipping' ) !== false ) {
    270             $has_free = true;
    271             break;
    272         }
    273     }
    274 
    275     if ( $has_free ) {
    276         foreach ( $rates as $rate_id => $rate ) {
    277             if ( strpos( $rate_id, 'aispfs_percentage_fee_shipping' ) !== false ) {
    278                 unset( $rates[$rate_id] );
    279             }
    280         }
    281     }
    282 
    283     return $rates;
    284 
    285 }, 200 );
  • percentage-fee-shipping/trunk/percentage-fee-shipping.php

    r3412961 r3441322  
    55 * Author URI: https://aispdev.com/
    66 * Description: Adds a shipping method where the cost is calculated as a percentage of the cart subtotal.
    7  * Version: 1.2.1
    8  * Requires at least: 5.0 
    9  * Requires PHP: 7.2 
     7 * Version: 1.3.0
     8 * Requires at least: 5.0
     9 * Requires PHP: 7.2
    1010 * Author: AISP Dev
    1111 * License: GPL-2.0+
     
    1515 */
    1616
    17 if (!defined('ABSPATH')) {
    18     exit; // Exit if accessed directly
     17if ( ! defined( 'ABSPATH' ) ) {
     18    exit;
    1919}
    2020
    21 // Check if WooCommerce is active
    22 if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')), true)) {
    23     add_action('admin_notices', function () {
     21if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {
     22    add_action( 'admin_notices', function () {
    2423        ?>
    2524        <div class="notice notice-error">
    26             <p><?php esc_html_e('Percentage Fee Shipping requires WooCommerce to be installed and active.', 'percentage-fee-shipping'); ?></p>
     25            <p><?php esc_html_e( 'Percentage Fee Shipping requires WooCommerce to be installed and active.', 'percentage-fee-shipping' ); ?></p>
    2726        </div>
    2827        <?php
    29     });
     28    } );
    3029    return;
    3130}
    3231
    33 // Load delivery method only when needed
    34 add_action('woocommerce_shipping_init', 'aispfs_load_shipping_method');
    3532
    36 function aispfs_load_shipping_method() {
    37     require_once plugin_dir_path(__FILE__) . 'includes/class-aispfs-shipping-percentage.php';
     33add_action( 'woocommerce_shipping_init', 'aispfs_load_shipping_methods' );
    3834
    39 }
     35function aispfs_load_shipping_methods() {
    4036
    41 // Save the delivery method
    42 add_filter('woocommerce_shipping_methods', 'aispfs_add_shipping_method');
     37    require_once plugin_dir_path( __FILE__ ) . 'includes/functions.php';
     38    require_once plugin_dir_path( __FILE__ ) . 'includes/class-aispfs-shipping-percentage.php';
     39    require_once plugin_dir_path( __FILE__ ) . 'includes/class-aispfs-shipping-free.php';
     40    }
    4341
    44 function aispfs_add_shipping_method($methods) {
    45     $methods['percentage_fee_shipping'] = 'AISPFS_Shipping_Percentage';
     42
     43add_filter( 'woocommerce_shipping_methods', 'aispfs_register_shipping_methods' );
     44
     45function aispfs_register_shipping_methods( $methods ) {
     46
     47    $methods['aispfs_percentage_fee_shipping'] = 'AISPFS_Shipping_Percentage';
     48    $methods['aispfs_free_shipping']           = 'AISPFS_Shipping_Free';
     49   
    4650    return $methods;
    4751}
    48 
    49 
    50 
  • percentage-fee-shipping/trunk/readme.txt

    r3412961 r3441322  
    66WC tested up to: 10.2 
    77Requires PHP: 7.2 
    8 Stable tag: 1.2.1
     8Stable tag: 1.3.0
    99License: GPLv2 or later 
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html 
     
    4545== Changelog ==
    4646
     47= 1.3.0 =
     48*Added support for Free Shipping restricted by User ID. Free shipping can now be limited to specific customers via shipping method settings.
     49*Refactored plugin structure into multiple class files for improved maintainability.
     50*Improved: shipping logic to ensure correct availability based on user eligibility and cart subtotal.
     51
    4752= 1.2.1 =
    4853* Enhancement: Added integration with **AISP Order Tools** (future plugin) support to skip shipping minimum fees for administrators when managing orders manually. No action required at this time.
     
    6671== Upgrade Notice ==
    6772
     73= 1.3.0 =
     74This update introduces Free Shipping restricted by User ID.
     75You can now limit free shipping availability to specific customers directly from the shipping method settings.
     76Recommended update for stores offering VIP or customer-specific shipping benefits.
     77
    6878= 1.2.1 =
    6979This version improves how admin users are detected during shipping calculations and lays the foundation for future integration with the upcoming **AISP Order Tools** plugin. 
    70 The feature to bypass minimum shipping fees for admins is now **capability-based** and will work seamlessly once AISP Order Tools is released. 
    7180No setup or action is required at this time.. 
    7281
Note: See TracChangeset for help on using the changeset viewer.