Prestashop product update hook is not updating pro

2019-07-25 06:12发布

I am using hookActionProductUpdate. I am getting all data updated but not attributes.

This is the code inside hook function:

public function hookActionProductUpdate($params) {
     $prestaObject = new ProductCore($params['id_product'], false, Context::getContext()->language->id);
     $arrrs = $prestaObject->getFrontFeatures(1); 
}

Everything else is updated but the front features I am getting are the older one. Any IDEA?


EDIT: I tried this too, here is my new function:

public function hookActionProductUpdate($params) {
    $product = $params['product'];
    $arrrs = $product->getFrontFeatures(1);
    pr($arrrs);die("No updating :(");
}

2条回答
倾城 Initia
2楼-- · 2019-07-25 07:09

Yeah I got it why, Its a bug in prestashop, It calls the hook AdminProductsController

Hook::exec('actionProductSave', array('id_product' => (int)$this->id, 'product' => $this));
Hook::exec('actionProductUpdate', array('id_product' => (int)$this->id, 'product' => $this));

from an update method which is called first then it executes the feature update code.

INSIDE processupdate function

I found this code

//this update method calls the HOOK and when this hook get executed it updates features in the database.
if ($object->update()) {
              // If the product doesn't exist in the current shop but exists in another shop
                if (Shop::getContext() == Shop::CONTEXT_SHOP && !$existing_product->isAssociatedToShop($this->context->shop->id)) {
                    $out_of_stock = StockAvailable::outOfStock($existing_product->id, $existing_product->id_shop_default);
                    $depends_on_stock = StockAvailable::dependsOnStock($existing_product->id, $existing_product->id_shop_default);
                    StockAvailable::setProductOutOfStock((int)$this->object->id, $out_of_stock, $this->context->shop->id);
                    StockAvailable::setProductDependsOnStock((int)$this->object->id, $depends_on_stock, $this->context->shop->id);
                }


                PrestaShopLogger::addLog(sprintf($this->l('%s modification', 'AdminTab', false, false), $this->className), 1, null, $this->className, (int)$this->object->id, true, (int)$this->context->employee->id);
                if (in_array($this->context->shop->getContext(), array(Shop::CONTEXT_SHOP, Shop::CONTEXT_ALL))) {
                    if ($this->isTabSubmitted('Shipping')) {
                        $this->addCarriers();
                    }
                    if ($this->isTabSubmitted('Associations')) {
                        $this->updateAccessories($object);
                    }
                    if ($this->isTabSubmitted('Suppliers')) {
                        $this->processSuppliers();
                    }
                    if ($this->isTabSubmitted('Features')) {
                        $this->processFeatures();
                    }
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-25 07:17

You don't need to instantiate a new Object. The product Object should already be contained in $params['product'].

Here is the update() method of Product Class where this Hook is called:

public function update($null_values = false)
{
    $return = parent::update($null_values);
    $this->setGroupReduction();

    // Sync stock Reference, EAN13 and UPC
    if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && StockAvailable::dependsOnStock($this->id, Context::getContext()->shop->id)) {
        Db::getInstance()->update('stock', array(
            'reference' => pSQL($this->reference),
            'ean13'     => pSQL($this->ean13),
            'upc'        => pSQL($this->upc),
        ), 'id_product = '.(int)$this->id.' AND id_product_attribute = 0');
    }

    Hook::exec('actionProductSave', array('id_product' => (int)$this->id, 'product' => $this));
    Hook::exec('actionProductUpdate', array('id_product' => (int)$this->id, 'product' => $this));
    if ($this->getType() == Product::PTYPE_VIRTUAL && $this->active && !Configuration::get('PS_VIRTUAL_PROD_FEATURE_ACTIVE')) {
        Configuration::updateGlobalValue('PS_VIRTUAL_PROD_FEATURE_ACTIVE', '1');
    }

    return $return;
}

You should then use this code instead:

public function hookActionProductUpdate($params) {
    $product = $params['product'];
    $arrrs = $product->getFrontFeatures(1); 
}
查看更多
登录 后发表回答