0

I've developed a module to allow some minor changes to products from the frontend.

It works but the quantity management because the qty value is added and not replaced in cataloginventory_stock_item table.

I enabled query logging and in fact the sql query is:

UPDATE `cataloginventory_stock_item` SET `product_id` = ?, `stock_id` = ?, `qty` = qty+1488,  WHERE (item_id='5')

as you can see the qty value is added to the new qty value.

How to just update the value without adding it?

thanks

UPDATE

Here is the function to save the product:

protected function _initProductSave()
    {
        $product    = $this->_initProduct();
        $productData = $this->getRequest()->getPost('product');
        $dealData = $this->getRequest()->getPost('deal');

        /** setup default required values */

        $productData['visibility'] = 4;
        $productData['status'] = $dealData['deal_status'];
        $productData['is_deal'] = 1;
        //$productData['deal_type'] = $dealData['deal_type'];
        $productData['deal_from_date'] = $dealData['date_from'];
        $productData['deal_to_date'] = $dealData['date_to'];
        $productData['deal_target'];
        $productData['merchant_id'] = Mage::getModel('dailydeals/merchant')->getSessionMerchantId();


         /*** Inventory  */


            $productData['stock_data']['use_config_manage_stock'] = 1;


        /*** Websites   */
        if (!isset($productData['website_ids'])) {
            $productData['website_ids'] = array();
        }

        if(empty($productData['website_ids'])){
            $websites = Mage::app()->getWebsites();
            foreach($websites as $_website){
                $productData['website_ids'][] = $_website->getId();
            }
        }   

        /*** Media      */      
        $wasLockedMedia = false;
        if ($product->isLockedAttribute('media')) {
            $product->unlockAttribute('media');
            $wasLockedMedia = true;
        }

        $product->addData($productData);

        if ($wasLockedMedia) {
            $product->lockAttribute('media');
        }

        if (Mage::app()->isSingleStoreMode()) {
            $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
        }

        /*** Check "Use Default Value" checkboxes values       */
        if ($useDefaults = $this->getRequest()->getPost('use_default')) {
            foreach ($useDefaults as $attributeCode) {
                $product->setData($attributeCode, false);
            }
        }

        /**
         * Initialize product categories - added in v0.1.6
         */
        $categoryIds = $this->getRequest()->getPost('deal_categories');
        if (null !== $categoryIds) {
            if (empty($categoryIds)) {
                $categoryIds = array();
            }
            $product->setCategoryIds($categoryIds);
        }


        /**
         * Initialize data for configurable product
         */
        if (($data = $this->getRequest()->getPost('configurable_products_data'))
            && !$product->getConfigurableReadonly()
        ) {
            $product->setConfigurableProductsData(Mage::helper('core')->jsonDecode($data));
        }
        if (($data = $this->getRequest()->getPost('configurable_attributes_data'))
            && !$product->getConfigurableReadonly()
        ) {
            $product->setConfigurableAttributesData(Mage::helper('core')->jsonDecode($data));
        }

        $product->setCanSaveConfigurableAttributes(
            (bool) $this->getRequest()->getPost('affect_configurable_product_attributes')
                && !$product->getConfigurableReadonly()
        );

        /**
         * Initialize product options
         */
        if (isset($productData['options']) && !$product->getOptionsReadonly()) {
            $product->setProductOptions($productData['options']);
        }

        $product->setCanSaveCustomOptions(
            (bool)$this->getRequest()->getPost('affect_product_custom_options')
            && !$product->getOptionsReadonly()
        );      

        Mage::dispatchEvent(
            'catalog_product_prepare_save',
            array('product' => $product, 'request' => $this->getRequest())
        );

        return $product;
    }   
  • Please paste the code that you are using for updating qty. – michael Jan 5 '15 at 22:02
  • added. thanks. I think is a standard magento behaviour because I just use $product->save(). There should be some option to avoid the increment. – Antonio Pedicini Jan 5 '15 at 22:09
1

Very interesting, I didn't know this. :)

The function is in app/code/core/Mage/CatalogInventory/Model/Observer.php -> protected function _prepareItemForSave and more specifically this code:

$item->setQtyCorrection($item->getQty()-$originalQty);

you can remove the code above.

Or on more lower level it calls app/code/core/Mage/CatalogInventory/Model/Resource/Stock/Item.php -> protected function _prepareDataForTable and you can replace this code:

    $qty = abs($object->getQtyCorrection());
    if ($object->getQtyCorrection() < 0) {
        $data['qty'] = new Zend_Db_Expr('qty-' . $qty);
    } else {
        $data['qty'] = new Zend_Db_Expr('qty+' . $object->getQtyCorrection());
    }

with this:

$data['qty'] = $object->getQty();
0

I solved saving the stock after product:

$product->save();
$stock=Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->setQty($data['product']['stock_data']['qty']);
$stock->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.