concretecms-community-store / community_store

An open, free and community developed eCommerce system for Concrete CMS
https://concretecms-community-store.github.io/community_store/
MIT License
105 stars 66 forks source link

Setting Product::pQtyUnlim to 1 in Entity ends up being 0 in Database #885

Closed QuentindevePro closed 3 months ago

QuentindevePro commented 3 months ago

I have a simple code that creates a Product entity and then persists it to database:

function createCsProduct(ImportProduits $produit, EntityManager $em)
{
    $csProduct = new Product();
    $csProduct->setName($produit->getNom_Produit());
    $csProduct->setSKU($produit->getReference());
    $csProduct->setBarcode($produit->getEAN13());
    $csProduct->setPrice($produit->getPrix());
    $csProduct->setIsActive(true);
    $csProduct->setSalePrice($produit->getPrix());
    $csProduct->setQty(1000);
    $csProduct->setCustomerPrice(false);
    $csProduct->setQuantityPrice(false);
    $csProduct->setIsFeatured(false);
    $csProduct->setNoQty(true);
    $csProduct->setAllowDecimalQty(false);
    $csProduct->setIsTaxable(false);
    $csProduct->setImageID(0);
    $csProduct->setIsShippable(true);
    $csProduct->setCreatesUserAccount(false);
    $csProduct->setAutoCheckout(false);
    $csProduct->setIsExclusive(false);
    $csProduct->setNotificationEmails(false);
    $csProduct->setDateAdded(new \DateTime());
    $csProduct->setHasVariations(true);
    $csProduct->setIsUnlimited(true);

    $group = $em->getRepository(Group::class)->findOneBy(["groupName" => $produit->getCategorie()]);
    ProductGroup::add($csProduct, $group->getID());
    $csProduct->save();
    $csProduct->generatePage();
    return $csProduct;
}

However, even with $csProduct->setIsUnlimited(true) called, the resulting record in database ends up with pQtyUnlim on zero. I have the same issue with ProductVariation::setVariationIsUnlimited(true).

Mesuva commented 3 months ago

I just tried this myself, and my unlimited box ended up checked

But I had to change this to: $csProduct->setHasVariations(false);

What is missing is a bunch of sensible defaults for products, so you don't have to specifically set a bunch of options you aren't interested in. I'm just about to push up a fix for that.

Mesuva commented 3 months ago

Try applying this, then doing a doctrine refresh and cache clear, you should be able to then omit lots of these setters:

https://github.com/concretecms-community-store/community_store/commit/c12436c7a30033dcbea5e9402e28b57134f861f4

QuentindevePro commented 3 months ago

Thanks ! Now the value isn't magically changed anymore. But I wonder: my products actually have some variations. pVariations is set to 0 but products still display correctly on /products. Shouldn't I have some issues ?

Mesuva commented 3 months ago

It might depend on what you are wanting to do in terms of where the quantities are actually managed.

In the store Settings, there's an option here:

Screenshot 2024-07-01 at 13 37 36

Perhaps try setting that to No, and then in your API call setting setHasVariations to true.

QuentindevePro commented 3 months ago

I see. Thanks for your reactivity !