commerce icon indicating copy to clipboard operation
commerce copied to clipboard

[3.x]: Overpaid orders due to line item adjusters

Open iamtompickering opened this issue 2 years ago • 1 comments

What happened?

Description

Some orders have come through marked as overpaid when they shouldn't be.

We have a custom line item adjuster in place which add's percentages to certain products within specific categories. The prices in the cart and on the front end all show correctly, but then when the payment is actually taken, it looks like it only takes the original price - ignoring the adjustment.

Below the code for my adjustment, not sure if I'm missing something within this?!

$adjustments = [];
        foreach ($order->lineItems as $lineItem)
        {

            $adjustment = $this->_getEmptyOrderAdjustmentFor($order);
            $adjustment->lineItemId = $lineItem->id;

            $percentage = 0;

            $categoryId = null;

            $superTableBlocks = SuperTable::find()
                ->fieldId(149)
                ->all();

            $product = Product::find()
                ->id($lineItem->purchasable->product->id)
                ->one();

            $productCategoryIds = $product
                ->productCategories
                ->ids();

            foreach ($superTableBlocks as $block) {

                $categories = $block
                    ->getFieldValue('productSupplier')
                    ->all();

                foreach ($categories as $category) {
                    if ( $category->supplier == true ) {
                        $categoryId = $category->id;
                    }
                }

                if ( in_array($categoryId, $productCategoryIds) ) {
                    $percentage = $block
                        ->getFieldValue('productPriceAdjustment');
                }

                $basePrice = $lineItem->getSubtotal();
                $discountAmount = $basePrice * ($percentage / 100);

                $roundedDifference = round($discountAmount) - $discountAmount;

                $discountAmount = $discountAmount + $roundedDifference;

                if ( $percentage > 0 ) {

                    $adjustment->name = $product->title;
                    $adjustment->type = $product->id;
                    $adjustment->amount = $discountAmount;

                    $adjustments[] = $adjustment;

                }

            }

        }

        return $adjustments;

Craft CMS version

3.7.16

Craft Commerce version

2.2.22

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

iamtompickering avatar Sep 07 '22 16:09 iamtompickering

if you change the adjustment type from the product ID to 'discount' does that fix it?

$adjustment->type = $product->id;

lukeholder avatar Sep 15 '22 03:09 lukeholder

Please submit a new issue if this error persists after trying the suggestion above.

lukeholder avatar Nov 23 '22 13:11 lukeholder