Plugin Directory

Changeset 3317070


Ignore:
Timestamp:
06/24/2025 02:38:48 PM (10 months ago)
Author:
bostateam
Message:

add flexship option

Location:
bosta-woocommerce
Files:
25 added
2 edited

Legend:

Unmodified
Added
Removed
  • bosta-woocommerce/trunk/bosta-woocommerce.php

    r3262320 r3317070  
    66 * Author: Bosta
    77 * Author URI: https://www.bosta.co/
    8  * Version: 4.0.1
     8 * Version: 4.0.3
    99 * Requires at least: 5.0
    1010 * php version 7.0
     
    3333    $main_css_file = plugin_dir_path(__FILE__) . 'Css/main.css';
    3434    $pickups_css_file = plugin_dir_path(__FILE__) . 'components/pickups/pickups.css';
     35    $flexship_css_file = plugin_dir_path(__FILE__) . 'components/settings/flexship/flexship.css';
    3536
    3637    $main_css_version = filemtime($main_css_file);
    3738    $pickups_css_version = filemtime($pickups_css_file);
    38 
     39    $flexship_css_version = filemtime($flexship_css_file);
    3940
    4041    wp_enqueue_style(
     
    5051        $pickups_css_version
    5152    );
     53    wp_enqueue_style(
     54        'flexshipCSS',
     55        plugins_url('components/settings/flexship/flexship.css', __FILE__),
     56        array(),
     57        $flexship_css_version
     58    );
    5259}
    5360
    5461const BOSTA_ENV_URL_V0 = 'https://app.bosta.co/api/v0';
    5562const BOSTA_ENV_URL_V2 = 'https://app.bosta.co/api/v2';
    56 const PLUGIN_VERSION = '4.0.1';
     63const PLUGIN_VERSION = '4.0.3';
    5764const bosta_cache_duration = 86400;
    5865const bosta_country_id_duration = 604800;
    5966const BOSTA_EGYPT_COUNTRY_ID = "60e4482c7cb7d4bc4849c4d5";
    60 
     67const FLEX_SHIPPING_DEFAULT_VALUE = 75;
    6168//region Bosta Utils Functions
    6269
     
    587594function bosta_render_error_notice($error_message)
    588595{
    589     echo '<div class="notice notice-error is-dismissible">';
     596    echo '<div class="notice notice-error is-dismissible" style="padding: 10px;">';
    590597    echo $error_message;
    591598    echo '</div>';
     
    811818
    812819        if (!$response['success']) {
    813             bosta_render_error_notice($response['error']);
     820            $error_message = $response['error']? $response['error'] : $response['message'];
     821            bosta_set_transient('bosta_errors', $error_message);
    814822            return;
    815823        }
     
    9981006        $product = $item->get_product();
    9991007        $itemsCount += $item->get_quantity();
    1000         $descArray[] = bosta_format_order_description($productDescription, $index, $product->get_sku(), $product->get_name(), $item->get_quantity());
     1008        $descArray[] = bosta_format_order_description($productDescription, $product->get_sku(), $product->get_name(), $item->get_quantity());
    10011009        $index++;
    10021010    }
     
    10091017}
    10101018
    1011 function bosta_format_order_description($productDescription, $index, $sku, $name, $quantity)
    1012 {
    1013     $desc = "Product_$index: ";
    1014     if ($productDescription === 'yes') $desc .= $name;
    1015     if (!empty($sku)) $desc .= " [$sku]";
    1016     $desc .= " (" . $quantity . ")";
    1017 
     1019function bosta_format_order_description($productDescription, $sku, $name, $quantity)
     1020{
     1021    $desc = "";
     1022    if (is_array($productDescription)) {
     1023        if (in_array('name', $productDescription)) {
     1024            $desc .= $name;
     1025        }
     1026        if (in_array('sku', $productDescription) && !empty($sku)) {
     1027            $desc .= " [$sku]";
     1028        }
     1029        $desc .= " (" . $quantity . ")";
     1030       
     1031    }
    10181032    return $desc;
    10191033}
     
    14211435                $this->init_form_fields();
    14221436                $this->init_settings();
     1437                $this->auto_update_flex_ship_on_settings_page();
    14231438                add_action('woocommerce_update_options_shipping_' . $this->id, array(
    14241439                    $this,
    14251440                    'process_admin_options',
    14261441                ));
     1442                add_action('admin_footer', array($this, 'handle_flex_shipping_toggle'));
     1443            }
     1444
     1445            private function is_bosta_shipping_settings_screen() {
     1446                if (!is_admin()) {
     1447                    return false;
     1448                }
     1449                if (!isset($_GET['page'], $_GET['tab'], $_GET['section'])) {
     1450                    return false;
     1451                }
     1452                return $_GET['page'] === 'wc-settings' && $_GET['tab'] === 'shipping' && $_GET['section'] === 'bosta';
     1453            }
     1454
     1455
     1456            function handle_flex_shipping_toggle()
     1457            {
     1458                if (!$this->is_bosta_shipping_settings_screen()) {
     1459                    return;
     1460                }
     1461                $settings_html_file = plugin_dir_path(__FILE__) . 'components/settings/flexship/flexship.html';
     1462                if (file_exists($settings_html_file)) {
     1463                    include $settings_html_file;
     1464                }
     1465            }
     1466
     1467            function auto_update_flex_ship_on_settings_page()
     1468            {
     1469                if ($this->is_bosta_shipping_settings_screen()) {
     1470                    $apiKey = $this->get_option('APIKey');
     1471                    if (!empty($apiKey)) {
     1472                        $this->update_plugin_flex_ship_settings($apiKey);
     1473                    }
     1474                }
    14271475            }
    14281476
     
    14351483                    ),
    14361484                    'ProductDescription' => array(
    1437                         'label' => 'Display Woocomerce product description in AWB',
     1485                        'label' => 'Select product info to display in AWB',
    14381486                        'title' => __('Product description', 'bosta'),
    1439                         'type' => 'checkbox',
    1440                         'default' => 'yes',
     1487                        'type' => 'multiselect',
     1488                        'class' => 'wc-enhanced-select',
     1489                        'options' => array(
     1490                            'name' => __('Product Name', 'bosta'),
     1491                            'sku' => __('Product SKU', 'bosta'),
     1492                        ),
     1493                        'default' => array('name'),
     1494                        'description' => __('Select one or more product fields to display in the AWB.', 'bosta'),
    14411495                    ),
    14421496                    'AllowToOpenPackage' => array(
     
    14451499                        'type' => 'checkbox',
    14461500                        'default' => 'no',
     1501                    ),
     1502                    'EnableFlexShipping' => array(
     1503                        'label' => 'Enable FlexShip',
     1504                        'title' => __('Enable FlexShip', 'bosta'),
     1505                        'type' => 'checkbox',
     1506                        'default' => 'no',
     1507                        'description' => 'Charge your customer a service fee in case of package refusal to reduce your refund costs',
     1508                    ),
     1509                    'FlexShippingValue' => array(
     1510                        'type' => 'hidden',
     1511                        'default' => FLEX_SHIPPING_DEFAULT_VALUE,
    14471512                    ),
    14481513                    'OrderRef' => array(
     
    14811546            public function process_admin_options()
    14821547            {
     1548                $oldAPIKey = $this->get_option('APIKey');
     1549                $oldFlexship = $this->get_option('EnableFlexShipping');
     1550                $oldFlexshipValue = $this->get_option('FlexShippingValue');
     1551                $oldProductDescription = $this->get_option('ProductDescription');
     1552
     1553                $productDescription = isset($_POST['woocommerce_bosta_ProductDescription']) ? $_POST['woocommerce_bosta_ProductDescription'] : array();
     1554                if (empty($productDescription) || (is_array($productDescription) && count($productDescription) === 0)) {
     1555                    WC_Admin_Settings::add_error(__('Please select at least one product info field for AWB.', 'bosta'));
     1556                    $this->settings['ProductDescription'] = $oldProductDescription;
     1557                    return false;
     1558                }
     1559
    14831560                $settings_saved = parent::process_admin_options();
    14841561                $this->bosta_check_reset_zoning_cache_toggle();
    14851562
    1486                 $apikey = $this->get_option('APIKey');
    1487                 if (empty($apikey)) {
     1563                $newAPIKey = $this->get_option('APIKey');
     1564                $newFlexship = $this->get_option('EnableFlexShipping');
     1565                $newFlexshipValue = $this->get_option('FlexShippingValue');
     1566
     1567                if (empty($newAPIKey)) {
    14881568                    WC_Admin_Settings::add_error(__('Error: API Key is required.', 'bosta'));
    1489                     $settings_saved = false;
    1490                 } elseif (!bosta_validate_api_key($apikey)) {
     1569                    return false;
     1570                }
     1571                if (!bosta_validate_api_key($newAPIKey)) {
    14911572                    WC_Admin_Settings::add_error(__('Error: API Key is invalid.', 'bosta'));
    1492                     $settings_saved = false;
     1573                    return false;
    14931574                }
    1494 
    1495                 return $settings_saved;
     1575               
     1576                if (!$settings_saved) {
     1577                    return false;
     1578                }
     1579
     1580                if ($newAPIKey !== $oldAPIKey) {
     1581                    if ($newFlexship === 'yes') {
     1582                        $this->update_bosta_flex_ship_settings($newAPIKey, $newFlexship, $newFlexshipValue);
     1583                    } else {
     1584                        $this->update_plugin_flex_ship_settings($newAPIKey);
     1585                    }
     1586                    return true;
     1587                }
     1588
     1589                if ($newFlexship !== $oldFlexship || $newFlexshipValue !== $oldFlexshipValue) {
     1590                    $this->update_bosta_flex_ship_settings($newAPIKey, $newFlexship, $newFlexshipValue);
     1591                }
     1592
     1593                return true;
     1594            }
     1595
     1596            private function update_plugin_flex_ship_settings($apikey)
     1597            {
     1598                $url = BOSTA_ENV_URL_V0 . '/businesses/' . esc_html($apikey) . '/info';
     1599                $response = bosta_send_api_request('GET', $url);
     1600
     1601                if ($response['success'] && isset($response['body']['flexShip'])) {
     1602                    $flexShip = $response['body']['flexShip'];
     1603                    $isSubscribed = !empty($flexShip['isSubscribed']);
     1604                    $isAppliedToAllOrders = !empty($flexShip['isAppliedToAllOrders']);
     1605                    if ($isSubscribed && $isAppliedToAllOrders) {
     1606                        $this->update_option('EnableFlexShipping', 'yes');
     1607                        $this->update_option('FlexShippingValue', $flexShip['amountToBeCollected'] ?? FLEX_SHIPPING_DEFAULT_VALUE);
     1608                        $this->update_option('AllowToOpenPackage', 'yes');
     1609                    } else {
     1610                        $this->update_option('EnableFlexShipping', 'no');
     1611                        $this->update_option('FlexShippingValue', FLEX_SHIPPING_DEFAULT_VALUE);
     1612                    }
     1613                } else {
     1614                    $this->update_option('EnableFlexShipping', 'no');
     1615                    $this->update_option('FlexShippingValue', FLEX_SHIPPING_DEFAULT_VALUE);
     1616                }
     1617            }
     1618
     1619            private function update_bosta_flex_ship_settings($apikey, $flexship, $flexship_value)
     1620            {
     1621                $url = BOSTA_ENV_URL_V2 . '/businesses/flex-ship';
     1622                $body = [
     1623                    'isSubscribed' => ($flexship === 'yes'),
     1624                    'amountToBeCollected' => intval($flexship_value),
     1625                    'isAppliedToAllOrders' => true
     1626                ];
     1627                $response = bosta_send_api_request('PUT', $url, $apikey, $body);
     1628
     1629                if (!$response['success']) {
     1630                    $error_message = 'Failed to update FlexShip settings: ' . $response['error'];
     1631                    WC_Admin_Settings::add_error(__($error_message, 'bosta'));
     1632                } else {
     1633                    $success_message = $flexship === 'yes'
     1634                        ? 'FlexShip service has been activated successfully.'
     1635                        : 'FlexShip service has been deactivated successfully.';
     1636                    WC_Admin_Settings::add_message(__($success_message, 'bosta'));
     1637                }
    14961638            }
    14971639        }
     
    15351677}
    15361678
     1679add_action('admin_enqueue_scripts', function($hook) {
     1680    if ($hook === 'woocommerce_page_wc-settings' && isset($_GET['section']) && $_GET['section'] === 'bosta') {
     1681        wp_enqueue_script(
     1682            'bosta-settings-js',
     1683            plugins_url('components/settings/flexship/flexship.js', __FILE__),
     1684            array('jquery'),
     1685            filemtime(plugin_dir_path(__FILE__) . 'components/settings/flexship/flexship.js'),
     1686            true
     1687        );
     1688    }
     1689});
    15371690//endregion
    15381691
  • bosta-woocommerce/trunk/readme.txt

    r3262320 r3317070  
    77Requires PHP: 7.0
    88Tested up to: 6.6.1
    9 Stable tag: 4.0.1
     9Stable tag: 4.0.3
    1010WC requires at least: 2.6
    1111WC tested up to: 9.3.3
     
    206206= 4.0.1 =
    207207* Send order items value
     208
     209= 4.0.2 =
     210* Show notice error for blocked to order businesses
     211
     212= 4.0.3 =
     213* Add FlexShip option to settings page
Note: See TracChangeset for help on using the changeset viewer.