我是PrestaShop的新手。我遇到了一个问题,在代码中找不到PrestaShop从库存中减去项目。
当客户创建订单并选择付款时,项目将从库存中减去,但如果客户不付款并返回,商品就不会返回库存。所以我需要了解它在代码中发生的位置,因为我需要编写自动完成它的函数。
发布于 2017-03-30 08:38:36
在PaymentModule::validateOrder
中,有一个调用OrderDetail->createList
的方法:
// Insert new Order detail list using cart for the current order
$order_detail = new OrderDetail(null, null, $this->context);
$order_detail->createList($order, $this->context->cart, $id_order_state, $order->product_list, 0, true, $package_list[$id_address][$id_package]['id_warehouse']);
$order_detail_list[] = $order_detail;
在OrderDetail->createList
中,我们在列表中调用了$this->create
foreach产品。这就是“计算”订单详细数据的地方,并且有一个$this->checkProductStock($product, $id_order_state);
来检查订单是否被取消,而不是错误,以及该产品是否依赖于库存:
protected function checkProductStock($product, $id_order_state)
{
if ($id_order_state != Configuration::get('PS_OS_CANCELED') && $id_order_state != Configuration::get('PS_OS_ERROR')) {
$update_quantity = true;
if (!StockAvailable::dependsOnStock($product['id_product'])) {
$update_quantity = StockAvailable::updateQuantity($product['id_product'], $product['id_product_attribute'], -(int)$product['cart_quantity']);
}
if ($update_quantity) {
$product['stock_quantity'] -= $product['cart_quantity'];
}
if ($product['stock_quantity'] < 0 && Configuration::get('PS_STOCK_MANAGEMENT')) {
$this->outOfStock = true;
}
Product::updateDefaultAttribute($product['id_product']);
}
}
顺便说一句,如果你取消了一个订单,股票就会用这个定单量来重置。
但是,如果您只想在付款后减少库存,在您的情况下,我将添加一个配置PS_WAITING_PAYMENT,其值如果订单没有确认,则重写最后一个函数以添加等待付款的状态。
protected function checkProductStock($product, $id_order_state)
{
if ($id_order_state != Configuration::get('PS_OS_CANCELED') && $id_order_state != Configuration::get('PS_OS_ERROR') && $id_order_state != Configuration::get('PS_WAITING_PAYMENT')) {
$update_quantity = true;
if (!StockAvailable::dependsOnStock($product['id_product'])) {
$update_quantity = StockAvailable::updateQuantity($product['id_product'], $product['id_product_attribute'], -(int)$product['cart_quantity']);
}
if ($update_quantity) {
$product['stock_quantity'] -= $product['cart_quantity'];
}
if ($product['stock_quantity'] < 0 && Configuration::get('PS_STOCK_MANAGEMENT')) {
$this->outOfStock = true;
}
Product::updateDefaultAttribute($product['id_product']);
}
}
如果将等待付款订单状态设置为“不日志”,而新状态为“可日志”,则当您将状态更改为“确认付款”时,应该会减少库存,因为在OrderHistory->changeIdOrderState中有:
...
foreach ($order->getProductsDetail() as $product) {
if (Validate::isLoadedObject($old_os)) {
// if becoming logable => adds sale
if ($new_os->logable && !$old_os->logable) {
ProductSale::addProductSale($product['product_id'], $product['product_quantity']);
// @since 1.5.0 - Stock Management
if (!Pack::isPack($product['product_id']) &&
in_array($old_os->id, $error_or_canceled_statuses) &&
!StockAvailable::dependsOnStock($product['id_product'], (int)$order->id_shop)) {
StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], -(int)$product['product_quantity'], $order->id_shop);
}
}
...
https://stackoverflow.com/questions/43096797
复制相似问题