Ship In Cart

get_shipping_total - Ship In Cart

get_shipping_total

by GetSirius » Sat Sep 21, 2024 10:07 pm

I made a module that allows for flat rate shipping if an order is over a set total cost. It works, but an error comes up with the Ship In Cart addon.

At first, everything works just fine. Shipping estimates come up as expected, with my flat rate module showing if the order is over the total specified. I notice that if I click on a Delivery Method the Shopping Cart page reloads. That is OK, unless I click on my new Flat Rate module. When that happens, the Shopping Cart page no longer works (blank screen) and I get an error in the server log as follows:

PHP Fatal error: Cannot redeclare get_shippable_total() (previously declared in /shop/includes/modules/shipping/siriusflat.php:42) in /shop/includes/modules/shipping/siriusflat.php on line 41

It does not make sense to me (I'm not good at code) that the declaration can happen one line after the line that throws the error.
Here is the code where the error happens, lines 41-44:

Code: Select all

41     function get_shippable_total() { 
42     global $order, $currencies;
43
44      $order_total = (('physical' === $order->content_type) ? $_SESSION['cart']->show_total() : 0);
Full code of my flat rate shipping module:

Code: Select all

<?php
/*
  $Id$

  CE Phoenix, E-Commerce made Easy
  https://phoenixcart.org

  Copyright (c) 2021 Phoenix Cart

  Released under the GNU General Public License
*/

  class siriusflat extends abstract_shipping_module {

    const CONFIG_KEY_BASE = 'MODULE_SHIPPING_SIRIUSFLAT_';

// class methods
    public function quote($method = '') {
      global $order;
 
      $this->quotes = [
        'id' => $this->code,
        'module' => MODULE_SHIPPING_SIRIUSFLAT_TEXT_TITLE,
        'methods' => [[
          'id' => $this->code,
          'title' => MODULE_SHIPPING_SIRIUSFLAT_TEXT_WAY,	
          'cost' => (float)$this->base_constant('COST') + $this->calculate_handling(),
        ]],
      ];

      $this->quote_common();


     function get_shippable_total() { 
      global $order, $currencies;

      $order_total = (('physical' === $order->content_type) ? $_SESSION['cart']->show_total() : 0);

      if ('mixed' === $order->content_type) {
        foreach ($order->products as $product) {
          foreach (($product['attributes'] ?? []) as $option => $value) {
            $virtual_check = $GLOBALS['db']->query(sprintf(<<<'EOSQL'
SELECT COUNT(*) AS total
  FROM products_attributes pa
    INNER JOIN products_attributes_download pad
      ON pa.products_attributes_id = pad.products_attributes_id
  WHERE pa.products_id = %d AND pa.options_values_id = %d
EOSQL
              , (int)$product['id'], (int)$value['value_id']))->fetch_assoc();

            if ($virtual_check['total'] > 0) {
              // if any attribute is downloadable, the product is virtual
              // and doesn't count; so skip to the next product
              // without adding the line total
              continue 2;
            }
          }

          $order_total += $currencies->calculate_price($product['final_price'], $product['tax'], $product['qty']);
        }
      }

      return $order_total;
    }

if ((get_shippable_total()) > $this->base_constant('ORDERSOVER')) {	  
      return $this->quotes;
}

    }



    protected function get_parameters() {

      return [
        $this->config_key_base . 'STATUS' => [
          'title' => 'Enable Flat Shipping',
          'value' => 'True',
          'desc' => 'Do you want to offer flat rate shipping?',
          'set_func' => "Config::select_one(['True', 'False'], ",
        ],
        $this->config_key_base . 'ORDERSOVER' => [
          'title' => 'Orders over ',
          'value' => '0',
          'desc' => 'Use flat rate for order totals over this amount.',
        ],
        $this->config_key_base . 'COST' => [
          'title' => 'Shipping Cost',
          'value' => '5.00',
          'desc' => 'The shipping cost for all orders using this shipping method.',
        ],
        $this->config_key_base . 'TAX_CLASS' => [
          'title' => 'Tax Class',
          'value' => '0',
          'desc' => 'Use the following tax class on the shipping fee.',
          'use_func' => 'Tax::get_class_title',
          'set_func' => 'Config::select_tax_class(',
        ],
        $this->config_key_base . 'ZONE' => [
          'title' => 'Shipping Zone',
          'value' => '0',
          'desc' => 'If a zone is selected, only enable this shipping method for that zone.',
          'use_func' => 'geo_zone::fetch_name',
          'set_func' => 'Config::select_geo_zone(',
        ],
        $this->config_key_base . 'SORT_ORDER' => [
          'title' => 'Sort Order',
          'value' => '0',
          'desc' => 'Sort order of display.',
        ],
      ];
    }

  }

The Flat Rate shipping will work fine if I do not use the function that looks for and does not use any virtual products in the order total. That is, if I replace the function with the following and re-work the if statement that controls the Return of this module, it works perfect. I would like to keep using your great Shipping in Cart addon, but need this flat rate shipping for larger orders.

[code}
$order_total = (('physical' === $order->content_type) ? $_SESSION['cart']->show_total() : 0);
[/code]

Any idea of what is going on here? Maybe the action of clicking on Shipping Options be eliminated some way?

CE Phoenix v1.0.9.1
PHP Version 7.4.33

Thanks for any help offered!
GetSirius
Member
Posts: 50
Joined: Wed Mar 17, 2021 7:15 pm
Phoenix Version:
Contact:

Re: get_shipping_total

by bonbec » Sun Sep 22, 2024 6:06 am

Hello,
Choose another name for this function and in this line too : if ((get_shippable_total()) > $this->base_constant('ORDERSOVER')) {
I think that you used too the /shop/includes/modules/shipping/table.php where there is the same function.
Old MS2.2 PHP7.4 site being converted to CE Phoenix v1.1.0.6 PHP 8.3
User avatar
bonbec
Contributor
Posts: 190
Joined: Mon Oct 26, 2020 12:23 pm
Phoenix Version: V1.1.0.7
Contact:

Re: get_shipping_total

by raiwa » Sun Sep 22, 2024 3:31 pm

You should move the function out of the quote method and call it then as a class method.
Meanwhile, this is not related to my ship in cart addon. You will get the same error on the core checkout shipping page. Therefore I`ll lock the thread now.
Public Phoenix Change Log Cheat Set on Google Sheets
https://docs.google.com/spreadsheets/d/ ... sp=sharing

Need Help?viewtopic.php?f=10&t=27
raiwa
Certified Developer
Posts: 1640
Joined: Sat Dec 21, 2019 8:08 am
Phoenix Version: 1.1.0.6
Contact: