Plugin Directory

Changeset 3442292


Ignore:
Timestamp:
01/19/2026 08:19:22 AM (5 weeks ago)
Author:
jneshipping
Message:

Update to 1.5.0

Location:
jne-shipping-official
Files:
10 edited
35 copied

Legend:

Unmodified
Added
Removed
  • jne-shipping-official/tags/1.5.0/admin/class-jne-woocommerce-admin.php

    r3426854 r3442292  
    598598      'origin_phone' => sanitize_text_field($data['origin_phone'] ?? ''),
    599599      'shipping_insurance_enabled' => isset($data['shipping_insurance_enabled']) ? 1 : 0,
     600      'shipping_insurance_charged_to' => isset($data['shipping_insurance_charged_to']) && in_array($data['shipping_insurance_charged_to'], ['buyer', 'seller']) ? $data['shipping_insurance_charged_to'] : 'buyer',
    600601      'mask_awb_name' => isset($data['mask_awb_name']) ? 1 : 0,
    601602      'mask_awb_phone' => 1,
  • jne-shipping-official/tags/1.5.0/admin/includes/class-jne-admin-order-helper.php

    r3426854 r3442292  
    1515  const JNESHOF_ORDER_META_KEY_PICKUP_DATE = 'jneshof_shipping_pickup_date';
    1616  const JNESHOF_ORDER_META_KEY_IS_LABEL_PRINTED = 'jneshof_is_label_printed';
     17  const JNESHOF_ORDER_META_KEY_INSURANCE_COST = 'jneshof_shipping_insurance_cost';
     18  const JNESHOF_ORDER_META_KEY_INSURANCE_CHARGED_TO = 'jneshof_shipping_insurance_charged_to';
    1719
    1820  /** @var WC_Order|null */
     
    159161  /**
    160162   * Get shipping insurance cost.
     163   * Checks order meta first (for seller-charged insurance), then fees (for buyer-charged insurance).
     164   * If not found, calculates based on order subtotal if insurance is enabled.
    161165   * @return float|null
    162166   */
    163167  public function getShippingInsuranceCost(): ?float
    164168  {
     169    // First check order meta (for seller-charged insurance)
     170    $insurance_cost = $this->order->get_meta(self::JNESHOF_ORDER_META_KEY_INSURANCE_COST);
     171    if ($insurance_cost !== '' && $insurance_cost !== null && $insurance_cost > 0) {
     172      return (float) $insurance_cost;
     173    }
     174   
     175    // Fallback to fees (for buyer-charged insurance - backward compatibility)
    165176    $fees = $this->order->get_fees();
    166177    foreach($fees as $fee) {
    167178      if($fee->get_name() === 'Shipping Insurance') {
    168         return $fee->get_amount();
     179        $insurance_cost = $fee->get_amount();
     180        // Save to meta for consistency
     181        if ($insurance_cost > 0) {
     182          $this->updateOrderMeta(self::JNESHOF_ORDER_META_KEY_INSURANCE_COST, $insurance_cost);
     183        }
     184        return $insurance_cost;
    169185      }
    170186    }
     187   
     188    // Last resort: Calculate if insurance is enabled and order uses JNE shipping
     189    $store_settings = get_option(JNESHOF_OPTION_STORE_SETTINGS, []);
     190    $insurance_enabled = $store_settings['shipping_insurance_enabled'] ?? false;
     191   
     192    if ($insurance_enabled && $this->isJNEShippingMethod()) {
     193      $goods_value = $this->order->get_subtotal();
     194      $insurance_cost = ($goods_value * 0.002) + 5000;
     195     
     196      // Save calculated cost to meta
     197      $this->updateOrderMeta(self::JNESHOF_ORDER_META_KEY_INSURANCE_COST, $insurance_cost);
     198     
     199      return $insurance_cost;
     200    }
     201   
    171202    return null;
     203  }
     204
     205  /**
     206   * Save shipping insurance cost to order meta.
     207   * @param float $insurance_cost
     208   * @return void
     209   */
     210  public function saveShippingInsuranceCost(float $insurance_cost): void
     211  {
     212    $this->updateOrderMeta(self::JNESHOF_ORDER_META_KEY_INSURANCE_COST, $insurance_cost);
    172213  }
    173214
  • jne-shipping-official/tags/1.5.0/admin/includes/class-jne-admin-store-helper.php

    r3387194 r3442292  
    9696    {
    9797        return $this->store_settings['shipping_insurance_enabled'] ?? false;
     98    }
     99
     100    /**
     101     * Get shipping insurance charged to (buyer or seller)
     102     * @return string 'buyer' or 'seller' (default: 'buyer')
     103     */
     104    public function getShippingInsuranceChargedTo()
     105    {
     106        $charged_to = $this->store_settings['shipping_insurance_charged_to'] ?? 'buyer';
     107        return in_array($charged_to, ['buyer', 'seller']) ? $charged_to : 'buyer';
    98108    }
    99109
  • jne-shipping-official/tags/1.5.0/admin/partials/jne-woocommerce-admin-setting-display.php

    r3387194 r3442292  
    1717    if (isset($_POST['shipping_insurance_enabled']) && sanitize_text_field(wp_unslash($_POST['shipping_insurance_enabled']))) {
    1818      $sanitized_data['shipping_insurance_enabled'] = 1;
     19    }
     20    // Handle shipping insurance charged to option
     21    if (isset($_POST['shipping_insurance_charged_to'])) {
     22      $charged_to = sanitize_text_field(wp_unslash($_POST['shipping_insurance_charged_to']));
     23      $sanitized_data['shipping_insurance_charged_to'] = in_array($charged_to, ['buyer', 'seller']) ? $charged_to : 'buyer';
    1924    }
    2025    if (isset($_POST['mask_awb_name']) && sanitize_text_field(wp_unslash($_POST['mask_awb_name']))) {
     
    3641$store_settings = Jneshof_Woocommerce_Configs::get_store_settings();
    3742$shipping_insurance_enabled = isset($store_settings['shipping_insurance_enabled']) ? (int) $store_settings['shipping_insurance_enabled'] : 0;
     43$shipping_insurance_charged_to = isset($store_settings['shipping_insurance_charged_to']) ? $store_settings['shipping_insurance_charged_to'] : 'buyer';
    3844$origin_code = $store_settings['origin_code'] ?? '';
    3945$origin_name = $store_settings['origin_name'] ?? '';
     
    113119                </label>
    114120              </div>
     121             
     122              <!-- Insurance Charged To Option -->
     123              <?php if ($shipping_insurance_enabled): ?>
     124                <div class="mt-3 ms-4">
     125                  <label class="form-label fw-medium text-dark small" for="shipping_insurance_charged_to">
     126                    <?php esc_html_e('Biaya Asuransi Dibebankan ke', 'jne-shipping-official'); ?>
     127                  </label>
     128                  <div class="form-check">
     129                    <input class="form-check-input" type="radio" id="shipping_insurance_charged_to_seller"
     130                      name="shipping_insurance_charged_to" value="seller" <?php checked($shipping_insurance_charged_to, 'seller'); ?> />
     131                    <label class="form-check-label" for="shipping_insurance_charged_to_seller">
     132                      <?php esc_html_e('Penjual', 'jne-shipping-official'); ?>
     133                    </label>
     134                  </div>
     135                  <div class="form-check">
     136                    <input class="form-check-input" type="radio" id="shipping_insurance_charged_to_buyer"
     137                      name="shipping_insurance_charged_to" value="buyer" <?php checked($shipping_insurance_charged_to, 'buyer'); ?> />
     138                    <label class="form-check-label" for="shipping_insurance_charged_to_buyer">
     139                      <?php esc_html_e('Pembeli (Default)', 'jne-shipping-official'); ?>
     140                    </label>
     141                  </div>
     142                  <small class="form-text text-muted d-block mt-2">
     143                    <?php esc_html_e('Pilih siapa yang akan membayar biaya asuransi. Jika dipilih Penjual, biaya tidak akan ditambahkan ke total pembayaran pelanggan.', 'jne-shipping-official'); ?>
     144                  </small>
     145                </div>
     146              <?php endif; ?>
    115147            </div>
    116148
  • jne-shipping-official/tags/1.5.0/composer.json

    r3439605 r3442292  
    22    "name": "jne-shipping/jne-shipping-official",
    33    "description": "WordPress plugin integrated with WooCommerce for JNE shipping services",
    4     "version": "1.4.2",
     4    "version": "1.5.0",
    55    "type": "wordpress-plugin",
    66    "license": "GPL-2.0+",
  • jne-shipping-official/tags/1.5.0/jne-shipping-official.php

    r3439605 r3442292  
    2020 * Plugin Name:       JNE Shipping Official
    2121 * Description:       WordPress plugin integrated with WooCommerce for JNE shipping services
    22  * Version:           1.4.2
     22 * Version:           1.5.0
    2323 * Author:            PT. Tiki Jalur Nugraha Ekakurir
    2424 * Author URI:        https://jne.co.id/
     
    4848 * Rename this for your plugin and update it as you release new versions.
    4949 */
    50 define('JNESHOF_PLUGIN_VERSION', '1.4.2');
     50define('JNESHOF_PLUGIN_VERSION', '1.5.0');
    5151
    5252/**
  • jne-shipping-official/tags/1.5.0/public/includes/jne-woocommerce-public-insurance.php

    r3391256 r3442292  
    2323    if ($all_required_checklist_checked) {
    2424      add_action('woocommerce_cart_calculate_fees', [$this, 'calculate_shipping_insurance']);
     25      add_action('woocommerce_checkout_create_order', [$this, 'save_insurance_cost_to_order'], 10, 2);
    2526    }
    2627  }
     
    5859    }
    5960
    60     $insurance_enabled = get_option(JNESHOF_OPTION_STORE_SETTINGS, [])['shipping_insurance_enabled'] ?? false;
     61    $store_settings = get_option(JNESHOF_OPTION_STORE_SETTINGS, []);
     62    $insurance_enabled = $store_settings['shipping_insurance_enabled'] ?? false;
     63    $insurance_charged_to = $store_settings['shipping_insurance_charged_to'] ?? 'buyer';
     64   
     65    // Clear session if insurance is disabled or shipping method is not JNE
     66    if (!$insurance_enabled || !$is_shipping_method_jne) {
     67      if (WC()->session) {
     68        WC()->session->__unset('jneshof_insurance_cost');
     69      }
     70      return;
     71    }
     72   
    6173    if ($insurance_enabled && $is_shipping_method_jne) {
    6274      $goods_value = $cart->get_subtotal();
    6375      $insurance_fee = ($goods_value * 0.002) + 5000;
    64       $cart->add_fee('Shipping Insurance', $insurance_fee, true, '');
     76     
     77      // Only add as cart fee if charged to buyer
     78      if ($insurance_charged_to === 'buyer') {
     79        $cart->add_fee('Shipping Insurance', $insurance_fee, true, '');
     80        // Clear session if switching from seller to buyer
     81        if (WC()->session) {
     82          WC()->session->__unset('jneshof_insurance_cost');
     83        }
     84      } else {
     85        // Store insurance cost in session for later saving to order meta
     86        if (WC()->session) {
     87          WC()->session->set('jneshof_insurance_cost', $insurance_fee);
     88        }
     89      }
     90    }
     91  }
     92
     93  /**
     94   * Save insurance cost to order meta (for both buyer and seller charged)
     95   * This ensures insurance cost is always available for tracking and AWB label
     96   *
     97   * @param WC_Order $order
     98   * @param array $data
     99   */
     100  public function save_insurance_cost_to_order($order, $data)
     101  {
     102    $store_settings = get_option(JNESHOF_OPTION_STORE_SETTINGS, []);
     103    $insurance_enabled = $store_settings['shipping_insurance_enabled'] ?? false;
     104    $insurance_charged_to = $store_settings['shipping_insurance_charged_to'] ?? 'buyer';
     105   
     106    if (!$insurance_enabled) {
     107      return;
     108    }
     109   
     110    // Check if order uses JNE shipping
     111    $is_jne_shipping = false;
     112    $shipping_methods = $order->get_shipping_methods();
     113    foreach ($shipping_methods as $shipping_method) {
     114      if (method_exists($shipping_method, 'get_method_id') &&
     115          $shipping_method->get_method_id() === self::JNESHOF_SHIPPING_METHOD_ID) {
     116        $is_jne_shipping = true;
     117        break;
     118      }
     119    }
     120   
     121    if (!$is_jne_shipping) {
     122      return;
     123    }
     124   
     125    $insurance_cost = null;
     126   
     127    // Get insurance cost based on who is charged
     128    if ($insurance_charged_to === 'buyer') {
     129      // Get from order fees (already added to order as cart fee)
     130      $fees = $order->get_fees();
     131      foreach($fees as $fee) {
     132        if($fee->get_name() === 'Shipping Insurance') {
     133          $insurance_cost = $fee->get_amount();
     134          break;
     135        }
     136      }
     137    } else {
     138      // Get from session (seller-charged)
     139      if (WC()->session) {
     140        $insurance_cost = WC()->session->get('jneshof_insurance_cost');
     141        // Clear session data
     142        WC()->session->__unset('jneshof_insurance_cost');
     143      }
     144     
     145      // Fallback: Calculate if not in session
     146      if (!$insurance_cost || $insurance_cost <= 0) {
     147        $goods_value = $order->get_subtotal();
     148        $insurance_cost = ($goods_value * 0.002) + 5000;
     149      }
     150    }
     151   
     152    // Save to order meta for tracking and AWB label (for both buyer and seller)
     153    if ($insurance_cost && $insurance_cost > 0) {
     154      $order->update_meta_data('jneshof_shipping_insurance_cost', $insurance_cost);
     155      $order->update_meta_data('jneshof_shipping_insurance_charged_to', $insurance_charged_to);
    65156    }
    66157  }
  • jne-shipping-official/tags/1.5.0/readme.txt

    r3439605 r3442292  
    66Tested up to: 6.9
    77Requires PHP: 7.4
    8 Stable tag: 1.4.2
     8Stable tag: 1.5.0
    99License: GPL-2.0+
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.txt
     
    9090
    9191== Changelog ==
     92
     93= 1.5.0 =
     94* Added: Insurance cost charge option - Choose whether insurance cost is charged to buyer or seller (default: buyer)
     95* Added: Insurance cost is now always saved to order meta for tracking and AWB label display, regardless of who pays
    9296
    9397= 1.4.2 =
     
    222226== Upgrade Notice ==
    223227
     228= 1.5.0 =
     229Major feature update: Added insurance cost charge option allowing you to choose whether insurance is charged to buyer or seller. Default is buyer. Insurance cost is now always saved to order meta for proper tracking and AWB label display. Includes automatic fallback calculation to ensure insurance cost is always available. Fixed issue where insurance cost didn't display in AWB label when charged to seller. Recommended for all users.
     230
    224231= 1.4.2 =
    225232Bug fix update: Fixed critical issue where JNE Actions column, No. Resi column, and bulk actions were not appearing on WooCommerce orders page for users with HPOS disabled or using legacy order screen. Plugin now fully supports both HPOS and legacy CPT order screens. Recommended for all users experiencing missing columns or bulk actions.
  • jne-shipping-official/trunk/admin/class-jne-woocommerce-admin.php

    r3426854 r3442292  
    598598      'origin_phone' => sanitize_text_field($data['origin_phone'] ?? ''),
    599599      'shipping_insurance_enabled' => isset($data['shipping_insurance_enabled']) ? 1 : 0,
     600      'shipping_insurance_charged_to' => isset($data['shipping_insurance_charged_to']) && in_array($data['shipping_insurance_charged_to'], ['buyer', 'seller']) ? $data['shipping_insurance_charged_to'] : 'buyer',
    600601      'mask_awb_name' => isset($data['mask_awb_name']) ? 1 : 0,
    601602      'mask_awb_phone' => 1,
  • jne-shipping-official/trunk/admin/includes/class-jne-admin-order-helper.php

    r3426854 r3442292  
    1515  const JNESHOF_ORDER_META_KEY_PICKUP_DATE = 'jneshof_shipping_pickup_date';
    1616  const JNESHOF_ORDER_META_KEY_IS_LABEL_PRINTED = 'jneshof_is_label_printed';
     17  const JNESHOF_ORDER_META_KEY_INSURANCE_COST = 'jneshof_shipping_insurance_cost';
     18  const JNESHOF_ORDER_META_KEY_INSURANCE_CHARGED_TO = 'jneshof_shipping_insurance_charged_to';
    1719
    1820  /** @var WC_Order|null */
     
    159161  /**
    160162   * Get shipping insurance cost.
     163   * Checks order meta first (for seller-charged insurance), then fees (for buyer-charged insurance).
     164   * If not found, calculates based on order subtotal if insurance is enabled.
    161165   * @return float|null
    162166   */
    163167  public function getShippingInsuranceCost(): ?float
    164168  {
     169    // First check order meta (for seller-charged insurance)
     170    $insurance_cost = $this->order->get_meta(self::JNESHOF_ORDER_META_KEY_INSURANCE_COST);
     171    if ($insurance_cost !== '' && $insurance_cost !== null && $insurance_cost > 0) {
     172      return (float) $insurance_cost;
     173    }
     174   
     175    // Fallback to fees (for buyer-charged insurance - backward compatibility)
    165176    $fees = $this->order->get_fees();
    166177    foreach($fees as $fee) {
    167178      if($fee->get_name() === 'Shipping Insurance') {
    168         return $fee->get_amount();
     179        $insurance_cost = $fee->get_amount();
     180        // Save to meta for consistency
     181        if ($insurance_cost > 0) {
     182          $this->updateOrderMeta(self::JNESHOF_ORDER_META_KEY_INSURANCE_COST, $insurance_cost);
     183        }
     184        return $insurance_cost;
    169185      }
    170186    }
     187   
     188    // Last resort: Calculate if insurance is enabled and order uses JNE shipping
     189    $store_settings = get_option(JNESHOF_OPTION_STORE_SETTINGS, []);
     190    $insurance_enabled = $store_settings['shipping_insurance_enabled'] ?? false;
     191   
     192    if ($insurance_enabled && $this->isJNEShippingMethod()) {
     193      $goods_value = $this->order->get_subtotal();
     194      $insurance_cost = ($goods_value * 0.002) + 5000;
     195     
     196      // Save calculated cost to meta
     197      $this->updateOrderMeta(self::JNESHOF_ORDER_META_KEY_INSURANCE_COST, $insurance_cost);
     198     
     199      return $insurance_cost;
     200    }
     201   
    171202    return null;
     203  }
     204
     205  /**
     206   * Save shipping insurance cost to order meta.
     207   * @param float $insurance_cost
     208   * @return void
     209   */
     210  public function saveShippingInsuranceCost(float $insurance_cost): void
     211  {
     212    $this->updateOrderMeta(self::JNESHOF_ORDER_META_KEY_INSURANCE_COST, $insurance_cost);
    172213  }
    173214
  • jne-shipping-official/trunk/admin/includes/class-jne-admin-store-helper.php

    r3387194 r3442292  
    9696    {
    9797        return $this->store_settings['shipping_insurance_enabled'] ?? false;
     98    }
     99
     100    /**
     101     * Get shipping insurance charged to (buyer or seller)
     102     * @return string 'buyer' or 'seller' (default: 'buyer')
     103     */
     104    public function getShippingInsuranceChargedTo()
     105    {
     106        $charged_to = $this->store_settings['shipping_insurance_charged_to'] ?? 'buyer';
     107        return in_array($charged_to, ['buyer', 'seller']) ? $charged_to : 'buyer';
    98108    }
    99109
  • jne-shipping-official/trunk/admin/partials/jne-woocommerce-admin-setting-display.php

    r3387194 r3442292  
    1717    if (isset($_POST['shipping_insurance_enabled']) && sanitize_text_field(wp_unslash($_POST['shipping_insurance_enabled']))) {
    1818      $sanitized_data['shipping_insurance_enabled'] = 1;
     19    }
     20    // Handle shipping insurance charged to option
     21    if (isset($_POST['shipping_insurance_charged_to'])) {
     22      $charged_to = sanitize_text_field(wp_unslash($_POST['shipping_insurance_charged_to']));
     23      $sanitized_data['shipping_insurance_charged_to'] = in_array($charged_to, ['buyer', 'seller']) ? $charged_to : 'buyer';
    1924    }
    2025    if (isset($_POST['mask_awb_name']) && sanitize_text_field(wp_unslash($_POST['mask_awb_name']))) {
     
    3641$store_settings = Jneshof_Woocommerce_Configs::get_store_settings();
    3742$shipping_insurance_enabled = isset($store_settings['shipping_insurance_enabled']) ? (int) $store_settings['shipping_insurance_enabled'] : 0;
     43$shipping_insurance_charged_to = isset($store_settings['shipping_insurance_charged_to']) ? $store_settings['shipping_insurance_charged_to'] : 'buyer';
    3844$origin_code = $store_settings['origin_code'] ?? '';
    3945$origin_name = $store_settings['origin_name'] ?? '';
     
    113119                </label>
    114120              </div>
     121             
     122              <!-- Insurance Charged To Option -->
     123              <?php if ($shipping_insurance_enabled): ?>
     124                <div class="mt-3 ms-4">
     125                  <label class="form-label fw-medium text-dark small" for="shipping_insurance_charged_to">
     126                    <?php esc_html_e('Biaya Asuransi Dibebankan ke', 'jne-shipping-official'); ?>
     127                  </label>
     128                  <div class="form-check">
     129                    <input class="form-check-input" type="radio" id="shipping_insurance_charged_to_seller"
     130                      name="shipping_insurance_charged_to" value="seller" <?php checked($shipping_insurance_charged_to, 'seller'); ?> />
     131                    <label class="form-check-label" for="shipping_insurance_charged_to_seller">
     132                      <?php esc_html_e('Penjual', 'jne-shipping-official'); ?>
     133                    </label>
     134                  </div>
     135                  <div class="form-check">
     136                    <input class="form-check-input" type="radio" id="shipping_insurance_charged_to_buyer"
     137                      name="shipping_insurance_charged_to" value="buyer" <?php checked($shipping_insurance_charged_to, 'buyer'); ?> />
     138                    <label class="form-check-label" for="shipping_insurance_charged_to_buyer">
     139                      <?php esc_html_e('Pembeli (Default)', 'jne-shipping-official'); ?>
     140                    </label>
     141                  </div>
     142                  <small class="form-text text-muted d-block mt-2">
     143                    <?php esc_html_e('Pilih siapa yang akan membayar biaya asuransi. Jika dipilih Penjual, biaya tidak akan ditambahkan ke total pembayaran pelanggan.', 'jne-shipping-official'); ?>
     144                  </small>
     145                </div>
     146              <?php endif; ?>
    115147            </div>
    116148
  • jne-shipping-official/trunk/composer.json

    r3439605 r3442292  
    22    "name": "jne-shipping/jne-shipping-official",
    33    "description": "WordPress plugin integrated with WooCommerce for JNE shipping services",
    4     "version": "1.4.2",
     4    "version": "1.5.0",
    55    "type": "wordpress-plugin",
    66    "license": "GPL-2.0+",
  • jne-shipping-official/trunk/jne-shipping-official.php

    r3439605 r3442292  
    2020 * Plugin Name:       JNE Shipping Official
    2121 * Description:       WordPress plugin integrated with WooCommerce for JNE shipping services
    22  * Version:           1.4.2
     22 * Version:           1.5.0
    2323 * Author:            PT. Tiki Jalur Nugraha Ekakurir
    2424 * Author URI:        https://jne.co.id/
     
    4848 * Rename this for your plugin and update it as you release new versions.
    4949 */
    50 define('JNESHOF_PLUGIN_VERSION', '1.4.2');
     50define('JNESHOF_PLUGIN_VERSION', '1.5.0');
    5151
    5252/**
  • jne-shipping-official/trunk/public/includes/jne-woocommerce-public-insurance.php

    r3391256 r3442292  
    2323    if ($all_required_checklist_checked) {
    2424      add_action('woocommerce_cart_calculate_fees', [$this, 'calculate_shipping_insurance']);
     25      add_action('woocommerce_checkout_create_order', [$this, 'save_insurance_cost_to_order'], 10, 2);
    2526    }
    2627  }
     
    5859    }
    5960
    60     $insurance_enabled = get_option(JNESHOF_OPTION_STORE_SETTINGS, [])['shipping_insurance_enabled'] ?? false;
     61    $store_settings = get_option(JNESHOF_OPTION_STORE_SETTINGS, []);
     62    $insurance_enabled = $store_settings['shipping_insurance_enabled'] ?? false;
     63    $insurance_charged_to = $store_settings['shipping_insurance_charged_to'] ?? 'buyer';
     64   
     65    // Clear session if insurance is disabled or shipping method is not JNE
     66    if (!$insurance_enabled || !$is_shipping_method_jne) {
     67      if (WC()->session) {
     68        WC()->session->__unset('jneshof_insurance_cost');
     69      }
     70      return;
     71    }
     72   
    6173    if ($insurance_enabled && $is_shipping_method_jne) {
    6274      $goods_value = $cart->get_subtotal();
    6375      $insurance_fee = ($goods_value * 0.002) + 5000;
    64       $cart->add_fee('Shipping Insurance', $insurance_fee, true, '');
     76     
     77      // Only add as cart fee if charged to buyer
     78      if ($insurance_charged_to === 'buyer') {
     79        $cart->add_fee('Shipping Insurance', $insurance_fee, true, '');
     80        // Clear session if switching from seller to buyer
     81        if (WC()->session) {
     82          WC()->session->__unset('jneshof_insurance_cost');
     83        }
     84      } else {
     85        // Store insurance cost in session for later saving to order meta
     86        if (WC()->session) {
     87          WC()->session->set('jneshof_insurance_cost', $insurance_fee);
     88        }
     89      }
     90    }
     91  }
     92
     93  /**
     94   * Save insurance cost to order meta (for both buyer and seller charged)
     95   * This ensures insurance cost is always available for tracking and AWB label
     96   *
     97   * @param WC_Order $order
     98   * @param array $data
     99   */
     100  public function save_insurance_cost_to_order($order, $data)
     101  {
     102    $store_settings = get_option(JNESHOF_OPTION_STORE_SETTINGS, []);
     103    $insurance_enabled = $store_settings['shipping_insurance_enabled'] ?? false;
     104    $insurance_charged_to = $store_settings['shipping_insurance_charged_to'] ?? 'buyer';
     105   
     106    if (!$insurance_enabled) {
     107      return;
     108    }
     109   
     110    // Check if order uses JNE shipping
     111    $is_jne_shipping = false;
     112    $shipping_methods = $order->get_shipping_methods();
     113    foreach ($shipping_methods as $shipping_method) {
     114      if (method_exists($shipping_method, 'get_method_id') &&
     115          $shipping_method->get_method_id() === self::JNESHOF_SHIPPING_METHOD_ID) {
     116        $is_jne_shipping = true;
     117        break;
     118      }
     119    }
     120   
     121    if (!$is_jne_shipping) {
     122      return;
     123    }
     124   
     125    $insurance_cost = null;
     126   
     127    // Get insurance cost based on who is charged
     128    if ($insurance_charged_to === 'buyer') {
     129      // Get from order fees (already added to order as cart fee)
     130      $fees = $order->get_fees();
     131      foreach($fees as $fee) {
     132        if($fee->get_name() === 'Shipping Insurance') {
     133          $insurance_cost = $fee->get_amount();
     134          break;
     135        }
     136      }
     137    } else {
     138      // Get from session (seller-charged)
     139      if (WC()->session) {
     140        $insurance_cost = WC()->session->get('jneshof_insurance_cost');
     141        // Clear session data
     142        WC()->session->__unset('jneshof_insurance_cost');
     143      }
     144     
     145      // Fallback: Calculate if not in session
     146      if (!$insurance_cost || $insurance_cost <= 0) {
     147        $goods_value = $order->get_subtotal();
     148        $insurance_cost = ($goods_value * 0.002) + 5000;
     149      }
     150    }
     151   
     152    // Save to order meta for tracking and AWB label (for both buyer and seller)
     153    if ($insurance_cost && $insurance_cost > 0) {
     154      $order->update_meta_data('jneshof_shipping_insurance_cost', $insurance_cost);
     155      $order->update_meta_data('jneshof_shipping_insurance_charged_to', $insurance_charged_to);
    65156    }
    66157  }
  • jne-shipping-official/trunk/readme.txt

    r3439605 r3442292  
    66Tested up to: 6.9
    77Requires PHP: 7.4
    8 Stable tag: 1.4.2
     8Stable tag: 1.5.0
    99License: GPL-2.0+
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.txt
     
    9090
    9191== Changelog ==
     92
     93= 1.5.0 =
     94* Added: Insurance cost charge option - Choose whether insurance cost is charged to buyer or seller (default: buyer)
     95* Added: Insurance cost is now always saved to order meta for tracking and AWB label display, regardless of who pays
    9296
    9397= 1.4.2 =
     
    222226== Upgrade Notice ==
    223227
     228= 1.5.0 =
     229Major feature update: Added insurance cost charge option allowing you to choose whether insurance is charged to buyer or seller. Default is buyer. Insurance cost is now always saved to order meta for proper tracking and AWB label display. Includes automatic fallback calculation to ensure insurance cost is always available. Fixed issue where insurance cost didn't display in AWB label when charged to seller. Recommended for all users.
     230
    224231= 1.4.2 =
    225232Bug fix update: Fixed critical issue where JNE Actions column, No. Resi column, and bulk actions were not appearing on WooCommerce orders page for users with HPOS disabled or using legacy order screen. Plugin now fully supports both HPOS and legacy CPT order screens. Recommended for all users experiencing missing columns or bulk actions.
Note: See TracChangeset for help on using the changeset viewer.