Magento – Magento 2 how to save product

magento2productsave

UPDATE SCRIPT WORKS The issue was with google experiments being enabled

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 white screen, 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 WSOD?

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>";
}
?>

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?
            }
        }
}

Best Answer

The problem was completely unrelated, and the code above in the original post works as expected after finding the actual problem I had google experiments api enabled and it was throwing an exception while saving the product.

This fix was enabling developer mode, analysing stack trace and disabling the experimental mode in admin (after confirming by commenting the throw)

Related Topic