zencart / zencart

Zen Cart® is a full-function e-commerce application for your website.
https://github.com/zencart/zencart/releases
Other
374 stars 230 forks source link

category restrictions on coupons only work for master category #6057

Closed proseLA closed 10 months ago

proseLA commented 10 months ago

in the test data, product Hewlett Packard LaserJet 1100Xi Linked, has a master category of Printers, but is also in the category of Big Linked (at least it is in my test data...).

if one were to set up a coupon that was ONLY valid for Big Linked products, this product would not be valid.

is this behavior intended/expected?

it seems wrong to me.

lat9 commented 10 months ago

It seems wrong to me as well. I'm envisioning (especially in this season) a "Black Friday" category of products that is some percent-off.

lat9 commented 10 months ago

Running with the current master repository, I've set that category restriction: image

Placing Hewlett Packard LaserJet 1100Xi Linked in the cart, navigating to the checkout_payment page and entering the associated coupon-code, I'm seeing that the coupon's deduction is applied.

What's different between my setup and yours?

proseLA commented 10 months ago

i believe your coupon is setup incorrectly. try adding anything else to your cart. does the coupon also apply to something else not in that category?

proseLA commented 10 months ago

in my experience, the coupon needs to be setup like so to restrict to a category:

image

proseLA commented 10 months ago

wow, that is even listed in the docs!

proseLA commented 10 months ago

you can see where it validates the category here:

https://github.com/zencart/zencart/blob/f07e2e6e726a8e2db486066502e3d81a0ed42ea1/includes/functions/functions_gvcoupons.php#L212-L216

the code only checks the categories that come back from the zen_get_product_path method.

my solution would be to change that section of code and add the following method:

function validate_for_category(int $product_id, int $coupon_id)
{
    global $db;
    $productCatPath = zen_get_product_path($product_id) . getLinkedCategories($product_id);
    $catPathArray = array_unique(array_reverse(explode('_', $productCatPath)));
...
function getLinkedCategories(int $productId): string
{
    global $db;
    $sql = 'SELECT categories_id FROM ' . TABLE_PRODUCTS_TO_CATEGORIES . "
                           WHERE products_id =  $productId";
    $result = $db->Execute($sql);

    $catString = '';
    if ($result->EOF) {
        return $catString;
    }

    foreach ($result as $value) {
        $catString .= '_' . $value['categories_id'];
    }
    return $catString;
}
lat9 commented 10 months ago

i believe your coupon is setup incorrectly. try adding anything else to your cart. does the coupon also apply to something else not in that category?

With that coupon setup, the coupon only applies to a product in the Big Linked category:

image

Although I do see that adding (per the docs reference) that 'start by excluding all categories' restriction causes the coupon to no longer be applied to the order.

proseLA commented 10 months ago

i am having trouble reproducing, but here is a thought. for the coupon that you have set up, add another valid category. my theory is that the linked product will now not be valid. the code looks for only a single restriction and applies the logic differently with 1 restriction as opposed to more than 1 restriction.

i have confirmed this theory.