Magento Product Stock status changes automatically - magento

We are facing a issue in Magento 2.4. We are using MSI and we have 3 warehouses. All products have assigned 3 warehouse and we manage stock using MSI. A product can be available on one warehouse and can't be available on another, The issue is when we get product out of stock one day it get automatically in stock after 1 or 2 days and its random behavior not with specific products or warehouse.
Initially we thought may be its done by someone in team if they worked on same product so we have setup an alert when ever product get changed by admin we get notification on Email. But the strange thing is without any alert still status get changed.
I have used this event controller_action_catalog_product_save_entity_after to trigger an alert when ever product get changed and its working as well we have tested it.
Although we have restrict import functionality for other users, but we think product may be changed through csv or api although we have restricted but may be it can be done from any user.
Can any one please help if someone faced similar issue or any model function which always trigger when ever product stock status get changed from any action like API or from CSV or from Admin Edit or even frontend ??

I believe you can use Magento event catalog_product_save_after. Create an observer method that does the following on event catalog_product_save_after.
public function catalog_product_save_after($observer) {
$product = $observer->getProduct();
$stockData = $product->getStockData();
if ( $product && $stockData['qty'] ) {
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getEntityId()); // Load the stock for this product
$stock->setData('is_in_stock', 1); // Set the Product to InStock
$stock->save(); // Save
}
}

Related

product show in stock in admin order when it is in out of stock Magento

Hi i am trapped in a very strange situation, my client has enable the customers to order the products which are out of stock , the problem is when the customer place the order it shows the product in stock in order . I need to show the product status out of stock in the order so he come to know about the order item status .
Below is the settings i have in catalog-> inventory in admin
please suggest me where i am doing mistake or how can i do this
thanks
You need to modify your product.phtml inside app/design/frontend/{yourpackage}/{yourtheme}/template/catalog/product/view.phtml
Add following in your code scope for testing and checking if its not already showing out of stock .
$stockItem = Mage::getModel('cataloginventory/stock_item');
$resource = $stockItem->loadByProduct($_product->getId());
if(round($resource->getQty(),0) < 1)
{
echo "product is out of stock";
}
The above code actually tests if there is any quantity available in stock and is greater than 1. Also make a note that this will not work for the products setup as not to be managed by stock under inventory tab in product add/modify case.
Thanks

Products are not displaying in Frontend after import in Magento

I have imported products using System - Import/Export - Import option.
I can see my products in Admin panel, but not in Frontend.
I tried Re-indexing, cache clear, cache refresh, physical cache remove.
also checked product stock status, availability etc. Everything is ok.
But they are not displaying in frontend. I marked onething, If I open product that I imported using csv, just saved without any change, it starts displaying. But I have 100s of products. So I can't use this solution.
So please help me where I am going wrong in csv. below is my csv screenshot.
You need to ensure that the products are attached to a website.
This can be done via a bulk update:
Access the product listm select all
Alter attribute
Products information -> websites
It can also be done in the csv by using a "_product_websites" field and setting it to the website name or 'base'.
I forgot which CSV columns are mandatory but I do remember that if some are missing the importer does not tell you that but instead you get the behaviour that you are describing.
The simplest way to find out what is mandatory is to:
Fix bugs in Magento ImportExport module
Create a new product in admin
Check that the new product is visible on front-end
Export the new product with Magento ImportExport exporter
Delete created product
Import the previously exported product csv
Clear cache and reindex data
Check that product is visible on front-end (it should be)
Compare your CSV with the exported one and try to figure out what is missing from it
Try to import your CSV with added/fixed columns and add data untill the product is imported so that it is visible on front-end
This requires allot of trial and error...
The missing columns in my case had always the same value so if this will be the case with your problem as well you can simply extend CSV importer and hard code those values in there instead of fixing your CSV manually.
Since your product gets saved correctly if you open it in admin and save it you can also:
Import a product
Export that product
Open that product in admin and save it
Export newly saved product
Compare exported CSV-s where they differ
Fixing Magento ImportExport bugs:
First bug is that if you import multiple products the quantity informatino from the first product is used for all the products. To fix this you have to add $row = array(); right before $row['product_id'] = $this->_newSku[$rowData[self::COL_SKU]]['entity_id']; in Mage_ImportExport_Model_Import_Entity_Product::_saveStockItem() function.
The second bug causes Magento ImportExport module to return a foreign key constraint error when importing multiple products. Error happens because magento splits products data into multiple segments and if one product is located in two segments importer will delete data that was imported for the product in first segment before importing the second segment thereby causing database corruption (see this link for detailed explanation - this is where I got the solution below).
Note that removing foreign key constraint will not fix the problem but will instead make it worse since the database will contain corrupt data.
To fix it you have to change the code in Mage_ImportExport_Model_Import_Entity_Abstract::_saveValidatedBunches() function:
After if ($startNewBunch || !$source->valid()) { add
if ($startNewBunch && count($bunchRows) > 1) {
$arrKeys = array_keys($bunchRows);
$arrNew = array();
while(($tRow = array_pop($bunchRows))) {
$tKey = array_pop($arrKeys);
$arrNew[$tKey] = $tRow;
if ($tRow['sku']) {
break;
}
}
$nextRowBackup = array_reverse($arrNew, TRUE) + $nextRowBackup;
}
Hope this helps.
I had the same problem recently and I spent some time tring to figure it out...
Looks like magento needs a status flag for every product otherwise magento will not show it in the dashboard.
Solution : add a "status" column to your CSV file and set all the statuses to "Enabled"
(yes it's not a boolean. Just use the string inside the quotes as it is :)
I was stuck with the same problem, i then visited my var/export/export_all_products file, downloaded it again and uploaded the same back through import, logged out of my account and logged-in back, all products were back. This worked as a backup for me and i could see all the products back at the back-end.
Import as you have. If they are enabled, but not showing...
then you can fix this by clicking "select all" products in the "manage products" table and then "change status" and then select "enabled." This process might take a minute.
Visit your store and you should see your frontend with products.
Some kind of bug with the status/enabled setting.
You must have next fields in CSV
sku
_attribute_set
_type
_category
description
image
name
price
short_description
status
tax_class_id
thumbnail
visibility
weight
qty
_product_websites
is_in_stock
Please note that field is_in_stock is mandatory even if qty more then 1
In magento 2 we need to reindex in index management to display the products in frontend.
We can reindex through cmd.like the given example we need to enter magento file directory after that cmd php -f bin/magento indexer:reindex to reindex .
When I first started importing products via csv although I set the product to enabled, I figured out that even though it shows in the magento back end as enabled, it actually isnt - think its to do with setting the field contents as "1" or "0" rather than "enabled" or "disabled/null".
To get around this, after a import I simply selected all the products in the back end and change status to enabled - it fixes issue.
But, I do think if I simply changed the data in the csv import it would save me this minor in-convenience.

Invoice date is in GMT/UTC when programmatically creating an invoice

We have a process that generates shipments every 4 hours. Only the items that are shipped should be invoiced, so invoice creation is part of this process.
This process runs in an external PHP script that loads the Magento environment and then processes the pending orders.
When the invoices are created, the create at time is off by 4 hours, so that looks like it's not using the store time. I've reviewed the Magento code and searched the web, but, I don't see any indication that you should do something to set the correct store time when creating an invoice.
At the top of the script the timezone to the store's timezone -
date_default_timezone_set('America/Boston');
The magento environment based on the order's store id -
//Start environment emulation of the specified store
$storeId = $order->getStoreId();
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
Later, for order that shipment, an invoice is created -
// create an invoice for the invoiceable items
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($toInvoice);
if (!$invoice->getTotalQty()) {
Mage::throwException(Mage::helper('core')->__('Invoice generation failed - no items to invoice.'));
}
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
$invoiceID = $invoice->getIncrementId();
Any suggestions what should be done differently?
Thanks!
As far as I know all dates in Magento are stored as GMT.
Check this file for methods that will help you when working with dates in Magento app/code/core/Mage/Core/Model/Date.php
I read inchoo article from above and I make conclusion:
Just use formatDate() and formatTime() methods when you want to display dates and times and don't worry about anything else.

Magento : Sales report without invoice?

I work with a client that is using purchase orders, and a custom order's process (custom statuses). So there is no invoice, and only custom statuses for the orders. The problem is, with no "admin actions" (like invoices), correct me if i'm wrong, the orders doesn't appears in the reports (even if in reports->sales->orders, I select any status (including my custom ones and any date, nothing appears), I also tried to change the timezone and refreshing lifetime statistics.
So I guess I need to modify the way magento handle these reports to include all the orders, even if it has no admin actions(no invoice or anything).
Any help on this?
Thanks!
----Edit----
Thanks for the answer, I tried changing this
if ($paymentHelper->isZeroSubTotal($storeId)
&& $this->_getOrder()->getGrandTotal() == 0
&& $zeroSubTotalPaymentAction == Mage_Payment_Model_Method_Abstract::ACTION_AUTHORIZE_CAPTURE
&& $paymentHelper->getZeroSubTotalOrderStatus($storeId) == 'pending') {
$invoice = $this->_initInvoice();
$invoice->getOrder()->setIsInProcess(true);
$invoice->save();
}
for this
$invoice = $this->_initInvoice();
$invoice->getOrder()->setIsInProcess(true);
$invoice->save();
in onepagecontroller.php so it create an invoice for each orders,
but it still doesn't appear in the sales report.
Change your process such that a complete order still generates an invoice OR rewrite all the reports. You can change your process by training your client, or you can change it by writing automated cron scripts to examine order states and programmatically generate the invoices.

Creating checkout sessions at store level

I'm trying to create different checkout sessions for the logged in customer at a store level, rather than at the site level.
We have a site with 2 stores. When a user adds a product to the cart, we want to show that product in the cart only for that store. But currently switching between stores will show up products of other stores previously added to the cart.
After some research I found that whenever a user logs in to a store, a record is created in the sales_flat_quote table for the logged in customer, which is common for all the stores in that site. So I need to create a new record in the sales_flat_quote table when the user switches stores.
I found that the loadByCustomerId() function in class Mage_Sales_Model_Mysql4_Quote is what creates the new record in the sales_flat_quote table. But all I see is a select query:
$read = $this->_getReadAdapter();
$select = $this->_getLoadSelect('customer_id', $customerId, $quote)
->where('is_active=1')
->order('updated_at desc')
->limit(1);
$data = $read->fetchRow($select);
I don't understand how the select query is creating the record in the sales_flat_quote table.
Could someone guide as to how to create a new record in this table for the logged in customer when he/she switches to an other store under the same site?
We have a site with 2 stores. When a user adds a product to the cart,
we want to show that product in the cart only for that store. But
currently switching between stores will show up products of other
stores previously added to the cart.
Which is correct.
It looks like you want to separate carts for store definitions, which both are connected to the very same website definition in Magento.
In Magento, all stores defined within the same website do always share the very same cart.
That's by-design.
To achieve cart separation you would need to change your Magento setup to have two websites and one store per website. Otherwise you'll run into endless issues separating the two stores (like the issue you're having now), I guess.
That said, I'll nevertheless try to answer your concrete question:
I don't understand how the select query is creating the record in the
sales_flat_quote table.
A SELECT query usually never ever will create a record; it's thought to read data.
Have a look at Mage_Checkout_Model_Session::loadCustomerQuote():
$customerQuote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore()->getId())
->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());
This is what leads to the call of Mage_Sales_Model_Mysql4_Quote::loadByCustomerId() you're talking about. It just loads the customers quote (if any), nothing else.
Creation/update of the record happens by calling the quotes save() method.
Magento EE 1.11.0.0 for example saves the record this way:
if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
if ($this->getQuoteId()) {
$customerQuote->merge($this->getQuote())
->collectTotals()
->save(); // <- either this
}
$this->setQuoteId($customerQuote->getId());
if ($this->_quote) {
$this->_quote->delete();
}
$this->_quote = $customerQuote;
} else {
$this->getQuote()->getBillingAddress();
$this->getQuote()->getShippingAddress();
$this->getQuote()->setCustomer(Mage::getSingleton('customer/session')->getCustomer())
->setTotalsCollectedFlag(false)
->collectTotals()
->save(); // <- or that does the save
}
You didn't mention the Magento version you use, so your mileage may vary, but the principle will still remain the same.

Resources