craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
217 stars 170 forks source link

[4.x]: Sales are not applied to variants, works only when all purchasables and all categories are activated #3097

Closed stefanfriedrich closed 1 year ago

stefanfriedrich commented 1 year ago

What happened?

Description

I want to add a sale to certain variants, not to all. This doesn't work. There are no other discounts/sales active. All products are promotable. If I chose "all purchasables" and "all categories" - and only then - it works as expected. I have no categories so far. If I chose all purchasables by hand - also doesn't work.

Steps to reproduce

  1. Create a product with variants
  2. Creat a sale
  3. Chose a variant for the sale
  4. variant.onSale will be false

Expected behavior

variant.onSale should be true

Actual behavior

variant.onSale should is false

Craft CMS version

4.3.10

Craft Commerce version

4.2.5.1

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

stefanfriedrich commented 1 year ago

correct purchasableId shows up in "_purchasableIds" from the sale.

lukeholder commented 1 year ago

@stefanfriedrich can you confirm the product is marked as 'promotable' on the product edit page?

stefanfriedrich commented 1 year ago

@stefanfriedrich can you confirm the product is marked as 'promotable' on the product edit page?

@lukeholder Yes

stefanfriedrich commented 1 year ago

@lukeholder Hope that helps.

Product:

product

I also can't add the sale to product itself on the product-site. (Maybe because there are variants, but that's not the main-problem now.)

Variant:

variant

Sale:

sale

Template:

template

(removed the biggest part of the other products that are related to that sale, that's why it shows up as 0, 1, 24 in the screenshot)

stefanfriedrich commented 1 year ago

@lukeholder problem seems to be here: src/services/Sales.php

// Category match if (!$sale->allCategories) { $relatedTo = [$sale->categoryRelationshipType => $purchasable->getPromotionRelationSource()]; $saleCategories = $sale->getCategoryIds(); $relatedCategories = Category::find()->id($saleCategories)->relatedTo($relatedTo)->ids();

if (empty($relatedCategories)) {
    return false;
}

}

returns false also if no categories exist. quickfix right now is to set all categories active.

pdaleramirez commented 1 year ago

Screen Shot 2023-02-28 at 2 38 48 PM The problem is that you are not selecting any categories in the matching items setting. You should turn on the All categories lightswitch input if you are not selecting a category.

Let us know if that works. @stefanfriedrich

stefanfriedrich commented 1 year ago

@pdaleramirez => returns false also if no categories exist. not sure if this should be the case. works as written before.

pdaleramirez commented 1 year ago

@stefanfriedrich, if no categories exist, the value of the $sale->allCategories attribute returns to true, making all purchasable on sale. Turning on the All categories light switch should resolve the issue.

stefanfriedrich commented 1 year ago

@pdaleramirez @lukeholder turning it on resolves the issue, but not the problem: if no categories exist it also should return true, not false.

philipboomy commented 1 year ago

This definitely need some work. Confusing. If I have selected a purchasable why do I need to select a category? That would lead me to believe that it now applies to all purchasable in that category.