cancel
Showing results for 
Search instead for 
Did you mean: 

How to update product programatically?

SOLVED

How to update product programatically?

I am trying to save a product after updating the price via a new module in admin. I can update and view the prices from the page, but saving always results in empty admin screen, (no data other than page tile is renedered) if I remove the save function from the chain on the template page everything works as expected. Any help whatsoever would be greatly appreciated. I have included what I believe to be the relevant code below including how far I can trace the error below.

 

I have followed the code through "Magento\Framework\Model\ResourceModel\Db\AbstractDB::updateObject()" which reports the data is saving, I havent figured out how to debug getConnection()->update() yet. But all I get on screen is the white page when ever I include save in the chain. I am under the assumption that because the abstract db model shows the error_log after it updates the connection that this is not causing the page to error out?

 

 

The block:

 

"Vendor\Module\Block\Adminhtml\Index"

protected $_PRODUCTMODEL = NULL;
protected $_OBJMANAGER = NULL;

public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
array $data = []
)
{
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context, $data);
$this->_OBJMANAGER = \Magento\Framework\App\ObjectManager::getInstance();
$this->_PRODUCTMODEL = $this->_OBJMANAGER->get('Magento\Catalog\Model\Product');
}
public function productModel(){
return $this->_PRODUCTMODEL;
}

public function productData($sku){
return $this->productModel()
->loadByAttribute("sku",trim($sku));
}

The template:

 adminhtml/template.phtml
    <?php
    foreach($this->getProducts() as $product){
        //$product->setPrice("19.11");
        //$product->save();
        $productModel = $this
            ->productData($product->getSku())
            ->setData("price","22.1111")
            ->save();
        echo "ID:<pre>".print_r($productModel->toArray(),1)."</pre>";
    }
    ?>

Output  of print_r() without ->save() in the chain:

Array
(
    [entity_id] => 2052
    [attribute_set_id] => 4
    [type_id] => simple
    [sku] => xxxbxnxnxnxnxnnx
    [has_options] => 0
    [required_options] => 0
    [created_at] => 2016-03-14 13:46:27
    [updated_at] => 2016-03-14 16:04:35
    [name] => Tests Sku
    [meta_title] => Tests Sku
    [meta_description] => Tests Sku 

hello

    [image] => 
    [small_image] => 
    [thumbnail] => 
    [custom_design] => 
    [page_layout] => 
    [options_container] => container2
    [country_of_manufacture] => 
    [url_key] => tests-asin
    [msrp_display_actual_price_type] => 0
    [gift_message_available] => 
    [swatch_image] => 
    [status] => 1
    [visibility] => 4
    [quantity_and_stock_status] => 1
    [tax_class_id] => 2
    [price] => 22.1111
    [special_price] => 
    [cost] => 
    [weight] => 
    [msrp] => 
    [special_from_date] => 
    [special_to_date] => 
    [news_from_date] => 
    [news_to_date] => 
    [custom_design_from] => 
    [custom_design_to] => 
    [description] => 

hello

    [short_description] => 
    [meta_keyword] => Tests Sku
    [custom_layout_update] => 
    [is_salable] => 1
)

 

How far I can trace the error:

 

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Framework\Model\ResourceModel\Db;

abstract class AbstractDb extends AbstractResource
{
    protected function updateObject(\Magento\Framework\Model\AbstractModel $object)
    {
        $condition = $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $object->getId());
        /**
         * Not auto increment primary key support
         */
        if ($this->_isPkAutoIncrement) {
            error_log("auto inc jamesk");
            $data = $this->prepareDataForUpdate($object);
            if (!empty($data)) {
                error_log("data not empty jamesk");
                error_log(json_encode($data));
                $this->getConnection()->update($this->getMainTable(), $data, $condition);
                error_log("done saving jamesk"); <<should be succesful if it displays in error_log?
            }
        }
}
3 REPLIES

Re: How to update product programatically?

How to diagnose Magento 2 errors: https://mage2.pro/t/307

Re: How to update product programatically?

Dmitry, I have actually come across your page regarding debugging Magento a few times. Are you trying to help with my problem or just posting your link,?

 

I did put magento back in developer mode and this is what is displaying on the screen now.

 

Wrong request parameters
#0 /var/www/practiceshop.net/html/vendor/magento/module-google-optimizer/Observer/AbstractSave.php(93): Magento\GoogleOptimizer\Observer\AbstractSave->_initRequestParams()
#1 /var/www/practiceshop.net/html/vendor/magento/module-google-optimizer/Observer/AbstractSave.php(62): Magento\GoogleOptimizer\Observer\AbstractSave->_processCode()
#2 /var/www/practiceshop.net/html/vendor/magento/framework/Event/Invoker/InvokerDefault.php(73): Magento\GoogleOptimizer\Observer\AbstractSave->execute(Object(Magento\Framework\Event\Observer))
#3 /var/www/practiceshop.net/html/vendor/magento/framework/Event/Invoker/InvokerDefault.php(61): Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod(Object(Magento\GoogleOptimizer\Observer\Product\SaveGoogleExperimentScriptObserver), Object(Magento\Framework\Event\Observer))
#4 /var/www/practiceshop.net/html/vendor/magento/framework/Event/Manager.php(66): Magento\Framework\Event\Invoker\InvokerDefault->dispatch(Array, Object(Magento\Framework\Event\Observer))
#5 /var/www/practiceshop.net/html/var/generation/Magento/Framework/Event/Manager/Proxy.php(95): Magento\Framework\Event\Manager->dispatch('catalog_product...', Array)
#6 /var/www/practiceshop.net/html/vendor/magento/framework/Model/AbstractModel.php(791): Magento\Framework\Event\Manager\Proxy->dispatch('catalog_product...', Array)
#7 /var/www/practiceshop.net/html/vendor/magento/module-catalog/Model/Product.php(918): Magento\Framework\Model\AbstractModel->afterSave()
#8 /var/www/practiceshop.net/html/var/generation/Magento/Catalog/Model/Product/Interceptor.php(362): Magento\Catalog\Model\Product->afterSave()
#9 /var/www/practiceshop.net/html/vendor/magento/module-eav/Model/Entity/AbstractEntity.php(1154): Magento\Catalog\Model\Product\Interceptor->afterSave()
#10 [internal function]: Magento\Eav\Model\Entity\AbstractEntity->save(Object(Magento\Catalog\Model\Product\Interceptor))
#11 /var/www/practiceshop.net/html/vendor/magento/framework/Interception/Interceptor.php(74): call_user_func_array(Array, Array)
#12 /var/www/practiceshop.net/html/vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Catalog\Model\ResourceModel\Product\Interceptor->___callParent('save', Array)
#13 /var/www/practiceshop.net/html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'save', Object(Magento\Catalog\Model\ResourceModel\Product\Interceptor), Array, 'catalogsearchFu...')
#14 /var/www/practiceshop.net/html/vendor/magento/module-catalog-search/Model/Indexer/Fulltext/Plugin/Product.php(28): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Catalog\Model\Product\Interceptor))
#15 [internal function]: Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product->aroundSave(Object(Magento\Catalog\Model\ResourceModel\Product\Interceptor), Object(Closure), Object(Magento\Catalog\Model\Product\Interceptor))
#16 /var/www/practiceshop.net/html/vendor/magento/framework/Interception/Chain/Chain.php(68): call_user_func_array(Array, Array)
#17 /var/www/practiceshop.net/html/vendor/magento/framework/Interception/Interceptor.php(136): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'save', Object(Magento\Catalog\Model\ResourceModel\Product\Interceptor), Array, 'apply_catalog_r...')
#18 /var/www/practiceshop.net/html/vendor/magento/module-catalog-rule/Plugin/Indexer/Product/Save/ApplyRules.php(40): Magento\Catalog\Model\ResourceModel\Product\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Catalog\Model\Product\Interceptor))
#19 [internal function]: Magento\CatalogRule\Plugin\Indexer\Product\Save\ApplyRules->aroundSave(Object(Magento\Catalog\Model\ResourceModel\Product\Interceptor), Object(Closure), Object(Magento\Catalog\Model\Product\Interceptor))
#20 /var/www/practiceshop.net/html/vendor/magento/framework/Interception/Interceptor.php(141): call_user_func_array(Array, Array)
#21 /var/www/practiceshop.net/html/var/generation/Magento/Catalog/Model/ResourceModel/Product/Interceptor.php(650): Magento\Catalog\Model\ResourceModel\Product\Interceptor->___callPlugins('save', Array, Array)
#22 /var/www/practiceshop.net/html/vendor/magento/framework/Model/AbstractModel.php(620): Magento\Catalog\Model\ResourceModel\Product\Interceptor->save(Object(Magento\Catalog\Model\Product\Interceptor))
#23 /var/www/practiceshop.net/html/var/generation/Magento/Catalog/Model/Product/Interceptor.php(2351): Magento\Framework\Model\AbstractModel->save()
#24 /var/www/practiceshop.net/html/app/code/Rcore/Test/view/adminhtml/templates/index.phtml(10): Magento\Catalog\Model\Product\Interceptor->save()
#25 /var/www/practiceshop.net/html/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/var/www/practi...')
#26 /var/www/practiceshop.net/html/vendor/magento/framework/View/Element/Template.php(255): Magento\Framework\View\TemplateEngine\Php->render(Object(Rcore\Test\Block\Adminhtml\Index), '/var/www/practi...', Array)
#27 /var/www/practiceshop.net/html/vendor/magento/framework/View/Element/Template.php(279): Magento\Framework\View\Element\Template->fetchView('/var/www/practi...')
#28 /var/www/practiceshop.net/html/vendor/magento/module-backend/Block/Template.php(104): Magento\Framework\View\Element\Template->_toHtml()
#29 /var/www/practiceshop.net/html/vendor/magento/framework/View/Element/AbstractBlock.php(657): Magento\Backend\Block\Template->_toHtml()
#30 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(542): Magento\Framework\View\Element\AbstractBlock->toHtml()
#31 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(518): Magento\Framework\View\Layout->_renderBlock('rcore_test_inde...')
#32 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('rcore_test_inde...')
#33 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('rcore_test_inde...')
#34 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('rcore_test_inde...', true)
#35 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('rcore_test_inde...')
#36 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('content')
#37 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('content')
#38 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('content')
#39 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('content', true)
#40 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('content')
#41 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('main.col')
#42 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('main.col')
#43 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('main.col')
#44 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('main.col', true)
#45 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('main.col')
#46 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('admin.scope.col...')
#47 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('admin.scope.col...')
#48 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('admin.scope.col...')
#49 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('admin.scope.col...', true)
#50 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('admin.scope.col...')
#51 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('page.main.conta...')
#52 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('page.main.conta...')
#53 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('page.main.conta...')
#54 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('page.main.conta...', true)
#55 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('page.main.conta...')
#56 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('page.content')
#57 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('page.content')
#58 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('page.content')
#59 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('page.content', true)
#60 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('page.content')
#61 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('page.wrapper')
#62 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('page.wrapper')
#63 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('page.wrapper')
#64 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('page.wrapper', true)
#65 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('page.wrapper')
#66 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('backend.page')
#67 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('backend.page')
#68 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('backend.page')
#69 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('backend.page', true)
#70 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('backend.page')
#71 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('root')
#72 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('root')
#73 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('root')
#74 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('root', true)
#75 /var/www/practiceshop.net/html/vendor/magento/framework/View/Layout.php(938): Magento\Framework\View\Layout\Interceptor->renderElement('root')
#76 /var/www/practiceshop.net/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(492): Magento\Framework\View\Layout->getOutput()
#77 /var/www/practiceshop.net/html/vendor/magento/framework/View/Result/Page.php(241): Magento\Framework\View\Layout\Interceptor->getOutput()
#78 /var/www/practiceshop.net/html/vendor/magento/framework/View/Result/Layout.php(162): Magento\Framework\View\Result\Page->render(Object(Magento\Framework\App\Response\Http\Interceptor))
#79 /var/www/practiceshop.net/html/var/generation/Magento/Backend/Model/View/Result/Page/Interceptor.php(193): Magento\Framework\View\Result\Layout->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
#80 /var/www/practiceshop.net/html/vendor/magento/framework/App/Http.php(119): Magento\Backend\Model\View\Result\Page\Interceptor->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
#81 /var/www/practiceshop.net/html/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#82 /var/www/practiceshop.net/html/index.php(52): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#83 {main}

 

 

Re: How to update product programatically?

O geez, I had google content experiments enabled because I was playing with the analytics api and it seems that was causing the error by throwing an exception, I confirmed this by commenting the throw on line #117 and then by disabling experiments via admin.

 

Not sure if I feel dumber because I spent 2 days banging my head off the wall trying to save a product or because it was something completely unrelated that I totally missed........

 

namespace Magento\GoogleOptimizer\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

abstract class AbstractSave implements ObserverInterface{

 /**
     * Init request params
     *
     * @return void
     * @throws \InvalidArgumentException
     */
    protected function _initRequestParams()
    {
        $params = $this->_request->getParam('google_experiment');
        if (!is_array($params) || !isset($params['experiment_script']) || !isset($params['code_id'])) {
            //throw new \InvalidArgumentException('Wrong request parameters');
        }
        $this->_params = $params;
    }

}