Jump to content

problem with override function (RESOLVED)


Recommended Posts

Hello to all
I have two modules that are in conflict because they override the same function "public function getTotalWeight ()"

how could i solve it is possible to unify them?

-----------------------------------------------------

    MODULE ONE --------------------------------------------------------
    public function getTotalWeight($products = null)
    {
        $total_weight = parent::getTotalWeight($products);
        if (!Module::isEnabled('dynamicproduct')) {
            return $total_weight;
        }
        
        $module = Module::getInstanceByName('dynamicproduct');
        return $total_weight + $module->calculator->getTotalWeight($this->id);
    }

    MODULE TWO ----------------------------------------
    public function getTotalWeight($products = null, $id_carrier = null)
    {
        if (is_null($id_carrier)) {
            $id_carrier = $this->id_carrier;
        }
        
        include_once(_PS_MODULE_DIR_ . 'dimensionalweight/classes/dimensionalweightcarrierrule.php');
        include_once(_PS_MODULE_DIR_ . 'dimensionalweight/classes/dimensionalweightproductattribute.php');
        
        if (! Module::isEnabled("dimensionalweight")) {
            return parent::getTotalWeight($products);
        }
        
        $carrier_rule = DimensionalWeightCarrierRule::getRuleByIdCarrier($id_carrier);
        
        if (! is_null($products)) {
            $total_weight = 0;
            
            foreach ($products as $product) {
                $dim_attributes = DimensionalWeightProductAttribute::getAttributeCombinationsById($product['id_product_attribute']);
                $real_weight = (! isset($product['weight_attribute']) || is_null($product['weight_attribute']) ? $product['weight'] : $product['weight_attribute']);
                
                if (! empty($carrier_rule) && $carrier_rule['active']) {
                    $dim_weight = (((! isset($dim_attributes[0]['width']) || is_null($dim_attributes[0]['width']) ? $product['width'] : ($product['width'] + $dim_attributes[0]['width'])) * (! isset($dim_attributes[0]['height']) || is_null($dim_attributes[0]['height']) ? $product['height'] : ($product['height'] + $dim_attributes[0]['height'])) * (! isset($dim_attributes[0]['depth']) || is_null($dim_attributes[0]['depth']) ? $product['depth'] : ($product['depth'] + $dim_attributes[0]['depth']))) / $carrier_rule['factor']);
                    
                    $weight = ($dim_weight > $real_weight ? $dim_weight : $real_weight);
                    $total_weight += $weight * $product['cart_quantity'];
                } else {
                    $total_weight += $real_weight * $product['cart_quantity'];
                }
            }
            return $total_weight;
        }
        
        $products = Db::getInstance()->executeS('
        SELECT p.`weight` as weight, pa.`weight` as weight_attribute, p.`width` as width, p.`height` as height, p.`depth` as depth, cp.`quantity` as cart_quantity, dpa.`width` as width_attribute, dpa.`height` as height_attribute, dpa.`depth` as depth_attribute
        FROM `' . _DB_PREFIX_ . 'cart_product` cp
        LEFT JOIN `' . _DB_PREFIX_ . 'product` p ON (cp.`id_product` = p.`id_product`)
        LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON (cp.`id_product_attribute` = pa.`id_product_attribute`)
        LEFT JOIN `' . _DB_PREFIX_ . 'dimensionalweight_product_attribute` dpa ON (cp.`id_product_attribute` = dpa.`id_product_attribute`)
        WHERE  cp.`id_cart` = ' . (int) $this->id);
        
        $total_weight = 0;
        
        foreach ($products as $product) {
            $real_weight = (! isset($product['weight_attribute']) || is_null($product['weight_attribute']) ? $product['weight'] : ($product['weight'] + $product['weight_attribute']));
            
            if (! empty($carrier_rule)  && $carrier_rule['active']) {
                $dim_weight = (((! isset($product['width_attribute']) || is_null($product['width_attribute']) ? $product['width'] : ($product['width'] + $product['width_attribute'])) * (! isset($product['height_attribute']) || is_null($product['height_attribute']) ? $product['height'] : ($product['height'] + $product['height_attribute'])) * (! isset($product['depth_attribute']) || is_null($product['depth_attribute']) ? $product['depth'] : ($product['depth'] + $product['depth_attribute']))) / $carrier_rule['factor']);
                $weight = ($dim_weight > $real_weight ? $dim_weight : $real_weight);
                $total_weight += $weight * $product['cart_quantity'];
            } else {
                $total_weight += $real_weight * $product['cart_quantity'];
            }
        }
        
        if (version_compare(_PS_VERSION_, '1.6.1.2', '<')) {
            self::$_totalWeight[$this->id] = round((float) $total_weight, 3);
        } else {
            self::$_totalWeight[$this->id] = round((float) $total_weight, 6);
        }
        return self::$_totalWeight[$this->id];
    }

Edited by overbags
RESOLVED (see edit history)
Link to comment
Share on other sites

  • overbags changed the title to problem with override function (RESOLVED)

Hi all,

a general question:

It could be I have to change a module code.
It is correct, that I have to copy the module-File into my child-folder and change the code there?

Example: I like to change the category-tree.
I see in classic/modules/ps_cate..tree/views/templates/hook/ a file called  ps_categorytree.tpl.
Should I copy it to my child-folder (in the same folder-structur) ?

On more question:
In the file I find HTML and programming Code.
Where I find the associated js-Code (if existing)? 

Yours
Daniel

Link to comment
Share on other sites

13 minutes ago, danielsaar said:

Hi all,

a general question:

It could be I have to change a module code.
It is correct, that I have to copy the module-File into my child-folder and change the code there?

Example: I like to change the category-tree.
I see in classic/modules/ps_cate..tree/views/templates/hook/ a file called  ps_categorytree.tpl.
Should I copy it to my child-folder (in the same folder-structur) ?

On more question:
In the file I find HTML and programming Code.
Where I find the associated js-Code (if existing)? 

Yours
Daniel

Hi,

Indeed to override a tpl file we have to copy it in our theme under the same folder structure.
(theme/modules /...)

For the JS, if the theme has one, it's in the /asset/js folder of the theme, then we can look at what scripts are called in the "Network" tab of the element inspector.
The JS can also be called via the asset folder of a module in case of using a module.

To find it there is also the method to search all the files in the FTP but it requires to know what the JS is doing.

If there is no JS then it is enough to create one 😀 !

I hope to have helped you

Link to comment
Share on other sites

Hi JulienPct,

thanks.

In Classic I find: /assets/js/
error.js and
theme.js

This means, that where is only one file (theme.js)?
For parts of the template we have a lot of seperations to make it easier to find a special part of the whole theme.
In the future in the "section" js-files you also will make it more seperated?
Example: Suppose we have a js-function for the category-tree why not have a category-tree.js?

yours,
DAniel

Link to comment
Share on other sites

In most cases, the js file created in the /assets/js folder of our theme is directly taken into account, i.e. there is no need to do a "registerJavascript()" or others, it is taken into account on the site as soon as it is created.

Personally this file is enough for me because I never have many JS to add. However, I think that if we create one or more other JS in this same folder (or child folder), as long as we add it to the site via a register (often in the FrontController.php class), there should be no problem to separate everything in order to facilitate the understanding of the code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...