Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product defaultPrice is incorrect (uses wrong variant) #830

Closed
jlawrence-yellostudio opened this issue Apr 18, 2019 · 2 comments
Closed

Comments

@jlawrence-yellostudio
Copy link

jlawrence-yellostudio commented Apr 18, 2019

Within the beforeSave method of the elements/Product.php (Line 882)


        $defaultVariant = null;
        foreach ($this->getVariants() as $variant) {
            // Make the first variant (or the last one that isDefault) the default.
            if ($defaultVariant === null || $variant->isDefault) {
                $this->_defaultVariant = $variant;
            }
        }

$defaultVariant variable never gets set to non NULL resulting in the last variant returned by getVariants() becoming the default variant.

Solution is to either change $defaultVariant to $this->_defaultVariant
or
$this->_defaultVariant = $defaultVariant = $variant;

Using Craft - 3.1.22 with Commerce - 2.1.3.1

Issue exists in latest version at time of this message.

@lukeholder
Copy link
Member

Thanks, fixed for the next release.

@jlawrence-yellostudio
Copy link
Author

jlawrence-yellostudio commented Apr 18, 2019

Thanks Luke

For anyone wanting to know how to work around this until the release:

In your module use the namespace:

use craft\commerce\records\Product as ProductRecord;

In your module init method (where SomeModule and someService are the respective names of your module and service):

Event::on(ProductRecord::class, ProductRecord::EVENT_BEFORE_INSERT, [SomeModule::$instance->someService, 'eventProductRecordBeforeInsert']);
Event::on(ProductRecord::class, ProductRecord::EVENT_BEFORE_UPDATE, [SomeModule::$instance->someService, 'eventProductRecordBeforeUpdate']);

Then in your service use the namespace:

use craft\commerce\elements\Product;

And add the methods:

public function eventProductRecordBeforeInsert(Event $e)
{
	// NOTE: This is to work around bug: https://github.com/craftcms/commerce/issues/830
	$product = new Product($e->sender->getAttributes());
	$defaultVariant = NULL;
	foreach ($product->getVariants() as $variant) {
        if (null === $defaultVariant || $variant->isDefault) {
            $defaultVariant = $variant;
			break;
        }
    }
	if($defaultVariant) {
		$e->sender->defaultPrice = $defaultVariant->price;
	}
}
public function eventProductRecordBeforeUpdate(Event $e)
{
	// NOTE: This is to work around bug: https://github.com/craftcms/commerce/issues/830
	$product = new Product($e->sender->getAttributes());
	$defaultVariant = NULL;
	foreach ($product->getVariants() as $variant) {
        if (null === $defaultVariant || $variant->isDefault) {
            $defaultVariant = $variant;
			break;
        }
    }
	if($defaultVariant) {
		$e->sender->defaultPrice = $defaultVariant->price;
	}

}

EDIT: Restored the insert event as it actually IS needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants