Jump to content

ProductController Override Working - but not as expected. Am I using wrong syntax?


protechtrader

Recommended Posts

Hello,

 

I use a custom ProductController Override, and it is working, as in I know to delete class index and update and code does execute, but I am experiencing some errors when using it.   So for troubleshooting I cut out my code changes and just left the Override file with a copy and paste of the initContent() function from my original ProductController.php from PS 1.6.1.5 and I still have the same issue, so I have to assume I am doing something wrong.   My testing override file is below.  Is their a reason why having this override active would cause anything to change in Prestashop?  I guess I am just doing something wrong with how I made this, so If anyone can tell me I appreciate it.

 

/public_html/override/controllers/front/ProductController.php

<?php


class ProductController extends ProductControllerCore
{


  public function initContent()
  {
    parent::initContent();
    
        if (!$this->errors) {
            if (Pack::isPack((int)$this->product->id) && !Pack::isInStock((int)$this->product->id)) {
                $this->product->quantity = 0;
            }


            $this->product->description = $this->transformDescriptionWithImg($this->product->description);


            // Assign to the template the id of the virtual product. "0" if the product is not downloadable.
            $this->context->smarty->assign('virtual', ProductDownload::getIdFromIdProduct((int)$this->product->id));


            $this->context->smarty->assign('customizationFormTarget', Tools::safeOutput(urldecode($_SERVER['REQUEST_URI'])));


            if (Tools::isSubmit('submitCustomizedDatas')) {
                // If cart has not been saved, we need to do it so that customization fields can have an id_cart
                // We check that the cookie exists first to avoid ghost carts
                if (!$this->context->cart->id && isset($_COOKIE[$this->context->cookie->getName()])) {
                    $this->context->cart->add();
                    $this->context->cookie->id_cart = (int)$this->context->cart->id;
                }
                $this->pictureUpload();
                $this->textRecord();
                $this->formTargetFormat();
            } elseif (Tools::getIsset('deletePicture') && !$this->context->cart->deleteCustomizationToProduct($this->product->id, Tools::getValue('deletePicture'))) {
                $this->errors[] = Tools::displayError('An error occurred while deleting the selected picture.');
            }


            $pictures = array();
            $text_fields = array();
            if ($this->product->customizable) {
                $files = $this->context->cart->getProductCustomization($this->product->id, Product::CUSTOMIZE_FILE, true);
                foreach ($files as $file) {
                    $pictures['pictures_'.$this->product->id.'_'.$file['index']] = $file['value'];
                }


                $texts = $this->context->cart->getProductCustomization($this->product->id, Product::CUSTOMIZE_TEXTFIELD, true);


                foreach ($texts as $text_field) {
                    $text_fields['textFields_'.$this->product->id.'_'.$text_field['index']] = str_replace('<br />', "\n", $text_field['value']);
                }
            }


            $this->context->smarty->assign(array(
                'pictures' => $pictures,
                'textFields' => $text_fields));


            $this->product->customization_required = false;
            $customization_fields = $this->product->customizable ? $this->product->getCustomizationFields($this->context->language->id) : false;
            if (is_array($customization_fields)) {
                foreach ($customization_fields as $customization_field) {
                    if ($this->product->customization_required = $customization_field['required']) {
                        break;
                    }
                }
            }


            // Assign template vars related to the category + execute hooks related to the category
            $this->assignCategory();
            // Assign template vars related to the price and tax
            $this->assignPriceAndTax();


            // Assign template vars related to the images
            $this->assignImages();
            // Assign attribute groups to the template
            $this->assignAttributesGroups();


            // Assign attributes combinations to the template
            $this->assignAttributesCombinations();


            // Pack management
            $pack_items = Pack::isPack($this->product->id) ? Pack::getItemTable($this->product->id, $this->context->language->id, true) : array();
            $this->context->smarty->assign('packItems', $pack_items);
            $this->context->smarty->assign('packs', Pack::getPacksTable($this->product->id, $this->context->language->id, true, 1));


            if (isset($this->category->id) && $this->category->id) {
                $return_link = Tools::safeOutput($this->context->link->getCategoryLink($this->category));
            } else {
                $return_link = 'javascript: history.back();';
            }


            $accessories = $this->product->getAccessories($this->context->language->id);
            if ($this->product->cache_is_pack || count($accessories)) {
                $this->context->controller->addCSS(_THEME_CSS_DIR_.'product_list.css');
            }
            if ($this->product->customizable) {
                $customization_datas = $this->context->cart->getProductCustomization($this->product->id, null, true);
            }


            $this->context->smarty->assign(array(
                'stock_management' => Configuration::get('PS_STOCK_MANAGEMENT'),
                'customizationFields' => $customization_fields,
                'id_customization' => empty($customization_datas) ? null : $customization_datas[0]['id_customization'],
                'accessories' => $accessories,
                'return_link' => $return_link,
                'product' => $this->product,
                'product_manufacturer' => new Manufacturer((int)$this->product->id_manufacturer, $this->context->language->id),
                'token' => Tools::getToken(false),
                'features' => $this->product->getFrontFeatures($this->context->language->id),
                'attachments' => (($this->product->cache_has_attachments) ? $this->product->getAttachments($this->context->language->id) : array()),
                'allow_oosp' => $this->product->isAvailableWhenOutOfStock((int)$this->product->out_of_stock),
                'last_qties' =>  (int)Configuration::get('PS_LAST_QTIES'),
                'HOOK_EXTRA_LEFT' => Hook::exec('displayLeftColumnProduct'),
                'HOOK_EXTRA_RIGHT' => Hook::exec('displayRightColumnProduct'),
                'HOOK_PRODUCT_OOS' => Hook::exec('actionProductOutOfStock', array('product' => $this->product)),
                'HOOK_PRODUCT_ACTIONS' => Hook::exec('displayProductButtons', array('product' => $this->product)),
                'HOOK_PRODUCT_TAB' =>  Hook::exec('displayProductTab', array('product' => $this->product)),
                'HOOK_PRODUCT_TAB_CONTENT' =>  Hook::exec('displayProductTabContent', array('product' => $this->product)),
                'HOOK_PRODUCT_CONTENT' =>  Hook::exec('displayProductContent', array('product' => $this->product)),
                'display_qties' => (int)Configuration::get('PS_DISPLAY_QTIES'),
                'display_ht' => !Tax::excludeTaxeOption(),
                'jqZoomEnabled' => Configuration::get('PS_DISPLAY_JQZOOM'),
                'ENT_NOQUOTES' => ENT_NOQUOTES,
                'outOfStockAllowed' => (int)Configuration::get('PS_ORDER_OUT_OF_STOCK'),
                'errors' => $this->errors,
                'body_classes' => array(
                    $this->php_self.'-'.$this->product->id,
                    $this->php_self.'-'.$this->product->link_rewrite,
                    'category-'.(isset($this->category) ? $this->category->id : ''),
                    'category-'.(isset($this->category) ? $this->category->getFieldByLang('link_rewrite') : '')
                ),
                'display_discount_price' => Configuration::get('PS_DISPLAY_DISCOUNT_PRICE'),
            ));
        }
        $this->setTemplate(_PS_THEME_DIR_.'product.tpl');
    }


}

 

Link to comment
Share on other sites

well, my first observation is that you are calling the parent function, and then executing your code which is the parent code.  So doesn't that mean the parent code is executing twice?

 

you also fail to mention what error you are receiving.

Link to comment
Share on other sites

well, my first observation is that you are calling the parent function, and then executing your code which is the parent code.  So doesn't that mean the parent code is executing twice?

 

you also fail to mention what error you are receiving.

 

Thanks for the reply, I appreciate the response to talk this out.  I was thinking I was replacing that function with the override code, but I may have been mistaken there.  So assuming the code is executed twice, which in this example with no change it wouldn't make much sense to do so, but I was still thinking their would be no change in the result.  Obviously I am wrong, but what makes this code running twice change the resulting prestashop output?

 

The specific issue I have resulting from this test override only is with the product customization file upload.  When you attempt to use the file upload field on any product with any picture valid picture file after clicking the save button a error is given stating it was a unrecognized file format.  If you disable the test override the file upload with the same file completes normally.  I'm trying to understand what caused that result.

 

Thanks again.

Link to comment
Share on other sites

So you are telling prestashop to save this file to the product.  The file gets uploaded to a temporary location, associated to product and moved to a permanent location.  The file in the temp location is removed.

 

Its anybody's guess as to allowing that to occur twice would work or not, but my guess is that the upload would only occur once, and then the temp file gets deleted.  Then the second time it runs, the temp file does not exist.

    parent::initContent();

This code executes the parent code, so if you don't want it to execute twice, then remove this line, or remove your duplicate code.  If you remove this line, then it will prevent the execution of the parent controller (which includes the Product Controller and the FrontController).  You likely do not want that.

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...