Matteo Enna
Matteo Enna

Reputation: 1301

insertion amount of a newly created product

I'm creating a product on prestashop, I enter the amount with this function:

echo "1";
StockAvailable::setQuantity($id_prod, 0, $quantity,1);
echo "2";

the result is:

1fatal error

going on the backend, the product is and the amount set correctly. How can I avoid the "fatal error"? Report me error here: classes/Product.php in line 2582: if (!$id_cart && !isset($context->employee)) die(Tools::displayError());

This code is in the function "getPriceStatic". It is normal that it is pulled to perform "StockAvailable::setQuantity"?

I have not changed classes and I do not override

Upvotes: 1

Views: 128

Answers (1)

marsaldev
marsaldev

Reputation: 3349

Usually I use this snippet (adapt it to your needs):

$shop_is_feature_active = Shop::isFeatureActive();
$shops = Shop::getContextListShopID();
if ($shop_is_feature_active) {
    foreach ($shops as $shop) {
        StockAvailable::setQuantity((int)$product->id, 0, (int)$product->quantity, (int)$shop);
    }
} else {
    StockAvailable::setQuantity((int)$product->id, 0, (int)$product->quantity, (int)$this->context->shop->id);
}

However, I always suggest to check if the product is in that shop.

It is important to check if

Context::getContext()->employee

is not empty, otherwise enter:

$psContext = Context::getContext();
if (!$psContext->employee) {
    $psContext->employee = new Employee(PS_DEFAULT_EMPLOYEE);
}

Upvotes: 1

Related Questions