Jump to content

Quantity Update Function


Luigi Donato

Recommended Posts

If we don't know a prestashop version it's always difficult to help.

I would look at the files Order.php and OrderHistory.php located in the order path of the classes directory if V 1.6. is in question.

 

Search for "stock" and you'll find the methods and classes used.

Edited by Scully (see edit history)
Link to comment
Share on other sites

Yes, is version 1.6
I've find this id Order.php, but I suppose that is to get the quantity.. Is correct?:
 

$this->setProductCurrentStock($row);
    protected function setProductCurrentStock(&$product)
    {
        if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')
            && (int)$product['advanced_stock_management'] == 1
            && (int)$product['id_warehouse'] > 0) {
            $product['current_stock'] = StockManagerFactory::getManager()->getProductPhysicalQuantities($product['product_id'], $product['product_attribute_id'], (int)$product['id_warehouse'], true);
        } else {
            $product['current_stock'] = StockAvailable::getQuantityAvailableByProduct($product['product_id'], $product['product_attribute_id'], (int)$this->id_shop);
        }
    }
Edited by LuigiD (see edit history)
  • Like 1
Link to comment
Share on other sites

I don't know what ACP is. Furthermore we cannot guess how you implemented changes and what exactely the should do. You can try to write debug messages to your logfile. The method

PrestaShopLogger::addLog

Would be the one to write logfiles to the prestashop database. You can see them from backoffice - advanced - log.

Link to comment
Share on other sites

I don't know what ACP is. Furthermore we cannot guess how you implemented changes and what exactely the should do. You can try to write debug messages to your logfile. The method

PrestaShopLogger::addLog

Would be the one to write logfiles to the prestashop database. You can see them from backoffice - advanced - log.

Admin Control Panel.. I've try to insert a simple fwrite into this 2 funtions but not work..!

Edited by LuigiD (see edit history)
Link to comment
Share on other sites

Admin Control Panel.. I've try to insert a simple fwrite into this 2 funtions but not work..!

 

I get the impression that you don't use the classes correctly. So it should help if you showed some code that shows how you handle this. 

 

I would almost never use fwrite for debug. You never know in which directory it will write something. In 99% of the case an echo will work much better.

Link to comment
Share on other sites

I get the impression that you don't use the classes correctly. So it should help if you showed some code that shows how you handle this. 

 

I would almost never use fwrite for debug. You never know in which directory it will write something. In 99% of the case an echo will work much better.

I've tried with echo but don't work..

Example:

    public static function updateQuantity($id_product, $id_product_attribute, $delta_quantity, $id_shop = null)
    {
        if (!Validate::isUnsignedId($id_product)) {
            return false;
        }
        $product = new Product((int)$id_product);
        if (!Validate::isLoadedObject($product)) {
            return false;
        }

        $stockManager = Adapter_ServiceLocator::get('Core_Business_Stock_StockManager');
        $stockManager->updateQuantity($product, $id_product_attribute, $delta_quantity, $id_shop);
        return true;

        echo "dsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsa";
    }

And this:

 

    public static function setQuantity($id_product, $id_product_attribute, $quantity, $id_shop = null)
    {
        if (!Validate::isUnsignedId($id_product)) {
            return false;
        }

        $context = Context::getContext();

        // if there is no $id_shop, gets the context one
        if ($id_shop === null && Shop::getContext() != Shop::CONTEXT_GROUP) {
            $id_shop = (int)$context->shop->id;
        }

        $depends_on_stock = StockAvailable::dependsOnStock($id_product);

        //Try to set available quantity if product does not depend on physical stock
        if (!$depends_on_stock) {
            $id_stock_available = (int)StockAvailable::getStockAvailableIdByProductId($id_product, $id_product_attribute, $id_shop);
            if ($id_stock_available) {
                $stock_available = new StockAvailable($id_stock_available);
                $stock_available->quantity = (int)$quantity;
                $stock_available->update();
            } else {
                $out_of_stock = StockAvailable::outOfStock($id_product, $id_shop);
                $stock_available = new StockAvailable();
                $stock_available->out_of_stock = (int)$out_of_stock;
                $stock_available->id_product = (int)$id_product;
                $stock_available->id_product_attribute = (int)$id_product_attribute;
                $stock_available->quantity = (int)$quantity;

                if ($id_shop === null) {
                    $shop_group = Shop::getContextShopGroup();
                } else {
                    $shop_group = new ShopGroup((int)Shop::getGroupFromShop((int)$id_shop));
                }

                // if quantities are shared between shops of the group
                if ($shop_group->share_stock) {
                    $stock_available->id_shop = 0;
                    $stock_available->id_shop_group = (int)$shop_group->id;
                } else {
                    $stock_available->id_shop = (int)$id_shop;
                    $stock_available->id_shop_group = 0;
                }
                $stock_available->add();
            }

            Hook::exec('actionUpdateQuantity',
                array(
                    'id_product' => $id_product,
                    'id_product_attribute' => $id_product_attribute,
                    'quantity' => $stock_available->quantity
                    )
                );
        }

        Cache::clean('StockAvailable::getQuantityAvailableByProduct_'.(int)$id_product.'*');

        echo "dsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsadsafdsafffdsdsdafsadffsa";
    }
Link to comment
Share on other sites

Everybody roll back a bit.

 

In my very early post I stated that I have guessed the mentionned method StockAvailable::updateQuantity is used. Why: The very first post gave only little information on what was intended. Only some posts later we got the information that the update should be triggered by the backoffice edit product / update quantity function.

 

So by using the normal order flow with order states and quantity management enabled, the changes should work. However I still wouldn't use neither echo nor fwrite. Some functions reload pages after processing. If the do so, you loose your debug information.

 

In terms of cross checking the update mechanisms from update products I recommend to investigate the controller AdminProductsController.php and also the Products.php class.

Edited by Scully (see edit history)
Link to comment
Share on other sites

Everybody roll back a bit.

 

In my very early post I stated that I have guessed the mentionned method StockAvailable::updateQuantity is used. Why: The very first post gave only little information on what was intended. Only some posts later we got the information that the update should be triggered by the backoffice edit product / update quantity function.

 

So by using the normal order flow with order states and quantity management enabled, the changes should work. However I still wouldn't use neither echo nor fwrite. Some functions reload pages after processing. If the do so, you loose your debug information.

 

In terms of cross checking the update mechanisms from update products I recommend to investigate the controller AdminProductsController.php and also the Products.php class.

In the AdminProductsController.php I've find the StockAvailable::setQuantity function.. Nothing else caught my attention. Please help me  :(

Edited by LuigiD (see edit history)
Link to comment
Share on other sites

Sorry but unfortunately I do not understand English to perfection, as I do not write it to perfection.

What I need is to find the function that takes care of updating product quantities when they are modified or when a sale is made.

This is why I have external software to manage the warehouse and I would like to keep the quantities synchronized, so that if a product is sold on the e-commerce then it is not resold to the counter

Link to comment
Share on other sites

You english is good enough - don't worry about that. You should rather worry about your development skills an approaches. I opend the class file StockAvailable.php which I mentionned in my very first post. I then quickly determined that there are two functions coming into question.

StockAvailable::updateQuantity and StockAvailable::setQuantity

I then added very simple debugs to database commands to both functions right after their init.

 

It turned out that setQuantity is triggered from products quantity manual update by back end and updateQuantity is triggered by placing or cancelling orders. Why don't you simply follow the advices you have been given ??

 

If you need a debug statements - they are for free because it's friday:

For the Update:

PrestaShopLogger::addLog("DEBUG updateQuantity : $id_product : $delta_quantity : ", 1, null);

For the Set:

PrestaShopLogger::addLog("DEBUG setQuantity : $id_product : $id_product_attribute : $quantity : ", 1, null);       
Edited by Scully (see edit history)
  • Like 1
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...