Sylius: Missing addTaxon method on product

Created on 14 Feb 2017  路  10Comments  路  Source: Sylius/Sylius

The doc (http://docs.sylius.org/en/latest/book/products/taxons.html) explains how to add a taxon to a product ($product->addTaxon($taxon))
But the product class doesn't implement this method (and hasn't to, has it doesn't implement TaxonsAwareInterface).
Is this an error in the documentation, or something not yet implemented ?

I tried to add a taxon on a product using ProductTaxon, but how can I identify if a product already has a taxon ? (Same problem, Product doesn't implement TaxonsAwareInterface)

What am I missing ?

Thanks

Documentation Potential Bug

All 10 comments

/cc @TheMadeleine :)

Yo have to use addProductTaxon() because you're not adding a taxon to a product directly, you're adding a ProductTaxon to a Product. See Sylius/Component/Core/Model/Product.php

@tchapi : I agree with that (addProductTaxon() is the way to go), and I began to do this, but, in my context, I didn't find the way to avoid the duplication of ProductTaxon when I update the product (I make a regular import of the products from an external source, with a command)
I would be glad to make a PR to correct the documentation, but I don't want to provide a semi-solution :-)

There _might_ be a bug then

@cirdan Thanks for catching that bug! :) And... waiting for your PR with a fix then :+1:. Ping me if you need any help.

Hi @TheMadeleine. I will indeed need some help (not on the doc PR, but on the code before...)
There's need to perform a check around addProductTaxon() to avoid duplication. The first path I imagined was to make a query via the repository but there's no ProductTaxonRepository. (And I'm not sure that adding one is the "regular" way)
Maybe can you just point me in the right direction ?

Hi @cirdan !
Could you please provide more info on this? Like some code samples to reproduce the bug and so on.
Thanks.

@cirdan If I understand correctly you just want to check if the Taxon on the ProductTaxon is unique across existing Product::productTaxons? In this case, there is no need for a repository. In Product::hasProductTaxon, loop over all productTaxons and check if those have the taxon. If so, there is no need to add the new ProductTaxon and you have prevented duplication! Hope it helps 馃槃

Or you can just $product->filterProductTaxonsByTaxon($taxon)

@steffenbrem : I had thought of it (loop over productTaxons) but it seemed to me a bit overkill (maybe not for good reasons). I was thinking "this should already be implemented..." :-)
@okwinza : this seems to be a good one.

The following question will be about the duplication thing : is it a general rule that _we don't want to have twice the same taxon on a product_.
I haven't checked why, but on the other side, even if I have several lines in the database, the product is only listed once on a category page, so, someone must have done something about this...

Was this page helpful?
0 / 5 - 0 ratings