<p>I've got a module set up to add discounts based on criteria defined in a category group (handle 'productPacks') -> category (which is a "pack").</p>

<p>Now, everything works great for one type of pack, and subsequent counts on that single pack, but as soon as there's other parts in the cart that correspond to other packs from the category group, no discounts are applied.
I'm missing some logic in the code for how to distinguish and override a more expensive pack based on related products and which variants of that product are allowed to apply the larger discount based on said criteria.</p>

<p>I can't seem to get it to work so that each category is pushed as an adjustment into the adjustments array.</p>

<p>What am I missing? I'm checking cart contents against a category’s "related products" commerce product field (handle 'relatedProducts'), and then against which variants in those products are allowed (a commerce variants field with handle 'productVariants');</p>

<p><a href="https://github.com/23d1/productcombos/blob/master/src/adjusters/OrderAdjuster.php" rel="nofollow noreferrer">https://github.com/23d1/productcombos/blob/master/src/adjusters/OrderAdjuster.php</a></p>

<p>I'm simply checking the contents of the cart against the contents of a products field and a variants field in a set of categories in a specific category group. So no real relational stuff, simply comparing things to apply a discount amount that is a simple number field on the category it finds inside the specific category group (with the handle 'productPacks').</p>

CC BY-SA 4.0
2019-09-03T02:53:29.417Z
Are you trying to have a list of variants/product relations ON the category custom field? https://github.com/craftcms/commerce/issues/984
Luke Holder
CC BY-SA 4.0
2019-09-03T02:55:54.273Z
Yeah, essentially all the categories in the category group with the handle "productPacks", has fields like amount, related products and related variants. The title of the category then shows up as the description in the applied adjuster. It works perfectly fine for one single pack category, and if one adds more of the variants to the cart that correspond to the pack, it's multiplied by the lowest quantity of all those corresponding variants, however it doesn't work when adding variants that make up other packs.
Anders
CC BY-SA 4.0
2019-09-03T03:36:10.747Z
Not sure if that's related to the issue you linked, as I'm just doing some hard-coded checks against a specific category. Could be entries or something else, if that makes sense?
Anders
CC BY-SA 4.0
2019-09-03T06:44:53.907Z
Do you need those`unset`'s? it might be removing the adjustment from the array?
Luke Holder
CC BY-SA 4.0
2019-09-03T07:00:09.303Z
I get the same issue without. It’s essentially just resetting the variable for every item in the foreach loop, which is then eventually pushed into the $adjustments variable. Or at least that's the intent. So don’t need them I guess. Spaghetti code buildup. ;-)
Anders
CC BY-SA 4.0
2019-09-03T23:25:12.877Z
Here's a link to a github repo; https://github.com/23d1/productcombos (also added to the main post). PHP has got the better of me. It seems that it should work pushing an adjuster for each instance of a category into the $adjusters[] array variable... So confused.
Anders
<craft3><php><plugin-craftcommerce2><modules>
Craft Commerce Adjuster API/PHP questions
# Craft Commerce Adjuster API/PHP questions

(Asked by: username_0 on 2019-09-03)

I've got a module set up to add discounts based on criteria defined in a category group (handle 'productPacks') -> category (which is a "pack").

Now, everything works great for one type of pack, and subsequent counts on that single pack, but as soon as there's other parts in the cart that correspond to other packs from the category group, no discounts are applied.
I'm missing some logic in the code for how to distinguish and override a more expensive pack based on related products and which variants of that product are allowed to apply the larger discount based on said criteria.

I can't seem to get it to work so that each category is pushed as an adjustment into the adjustments array.

What am I missing? I'm checking cart contents against a category’s "related products" commerce product field (handle 'relatedProducts'), and then against which variants in those products are allowed (a commerce variants field with handle 'productVariants');

<https://github.com/23d1/productcombos/blob/master/src/adjusters/OrderAdjuster.php>

I'm simply checking the contents of the cart against the contents of a products field and a variants field in a set of categories in a specific category group. So no real relational stuff, simply comparing things to apply a discount amount that is a simple number field on the category it finds inside the specific category group (with the handle 'productPacks').

**Tags:** craft3, php, plugin-craftcommerce2, modules

> **Comments:**
> **username_1**: Are you trying to have a list of variants/product relations ON the category custom field? https://github.com/craftcms/commerce/issues/984  
> **username_0**: Yeah, essentially all the categories in the category group with the handle "productPacks", has fields like amount, related products and related variants. The title of the category then shows up as the description in the applied adjuster. It works perfectly fine for one single pack category, and if one adds more of the variants to the cart that correspond to the pack, it's multiplied by the lowest quantity of all those corresponding variants, however it doesn't work when adding variants that make up other packs.  
> **username_0**: Not sure if that's related to the issue you linked, as I'm just doing some hard-coded checks against a specific category. Could be entries or something else, if that makes sense?  
> **username_1**: Do you need those`unset`'s? it might be removing the adjustment from the array?  
> **username_0**: I get the same issue without. It’s essentially just resetting the variable for every item in the foreach loop, which is then eventually pushed into the $adjustments variable. Or at least that's the intent. So don’t need them I guess. Spaghetti code buildup. ;-)  
> **username_0**: Here's a link to a github repo; https://github.com/23d1/productcombos (also added to the main post). PHP has got the better of me. It seems that it should work pushing an adjuster for each instance of a category into the $adjusters[] array variable... So confused.  

---

## 0 answers

