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

Scout breaks with craft commerce v3 #178

Closed
jornwildenbeest opened this issue Oct 28, 2020 · 4 comments
Closed

Scout breaks with craft commerce v3 #178

jornwildenbeest opened this issue Oct 28, 2020 · 4 comments
Assignees
Labels
📦 commerce Craft Commerce related

Comments

@jornwildenbeest
Copy link

jornwildenbeest commented Oct 28, 2020

Versions:

craft-scout: 2.3.1
craft/commerce: 3.2.8.1
craft:/cms: 3.5.14

Issue:

Scout doesn't work with commerce after updating to commerce v3, so it appears.

When I try to import craft commerce products into Algolia with this code:

    \rias\scout\ScoutIndex::create('dev_producten')
    // Scout uses this by default, so this is optional
    ->elementType(Product::class)
    // If you don't define a siteId, the primary site is used
    ->criteria(function (\craft\commerce\elements\db\ProductQuery $query) {
        return $query;
        })
    /*
    * The element gets passed into the transform function, you can omit this
    * and Scout will use the \rias\scout\ElementTransformer class instead
    */
    ->transformer(function (Product $product) {

It returns this error:


Error: Call to a member function getPaymentCurrencies() on null in /home/vagrant/Sites/site/vendor/craftcms/commerce/src/elements/Product.php:177
Stack trace:
#0 /home/vagrant/Sites/site/vendor/yiisoft/yii2/base/Component.php(733): craft\commerce\elements\Product->behaviors()
#1 /home/vagrant/Sites/site/vendor/yiisoft/yii2/base/Component.php(465): yii\base\Component->ensureBehaviors()
#2 /home/vagrant/Sites/site/vendor/craftcms/cms/src/base/Model.php(75): yii\base\Component->hasEventHandlers()
#3 /home/vagrant/Sites/site/vendor/craftcms/cms/src/base/Element.php(1503): craft\base\Model->init()
#4 /home/vagrant/Sites/site/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\base\Element->init()
#5 /home/vagrant/Sites/site/vendor/rias/craft-scout/src/ScoutIndex.php(45): yii\base\BaseObject->__construct()
#6 /home/vagrant/Sites/site/config/scout.php(14): rias\scout\ScoutIndex->elementType()
#7 /home/vagrant/Sites/site/vendor/craftcms/cms/src/services/Config.php(202): include('/home/vagrant/S...')
#8 /home/vagrant/Sites/site/vendor/craftcms/cms/src/services/Plugins.php(895): craft\services\Config->getConfigFromFile()
#9 /home/vagrant/Sites/site/vendor/craftcms/cms/src/services/Plugins.php(228): craft\services\Plugins->createPlugin()
#10 /home/vagrant/Sites/site/vendor/craftcms/cms/src/base/ApplicationTrait.php(1416): craft\services\Plugins->loadPlugins()
#11 /home/vagrant/Sites/site/vendor/craftcms/cms/src/web/Application.php(93): craft\web\Application->_postInit()
#12 /home/vagrant/Sites/site/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\web\Application->init()
#13 /home/vagrant/Sites/site/vendor/yiisoft/yii2/base/Application.php(206): yii\base\BaseObject->__construct()
#14 [internal function]: yii\base\Application->__construct()
#15 /home/vagrant/Sites/site/vendor/yiisoft/yii2/di/Container.php(406): ReflectionClass->newInstanceArgs()
#16 /home/vagrant/Sites/site/vendor/yiisoft/yii2/di/Container.php(165): yii\di\Container->build()
#17 /home/vagrant/Sites/site/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get()
#18 /home/vagrant/Sites/site/vendor/craftcms/cms/bootstrap/bootstrap.php(246): yii\BaseYii::createObject()
#19 /home/vagrant/Sites/site/vendor/craftcms/cms/bootstrap/web.php(51): require('/home/vagrant/S...')
#20 /home/vagrant/Sites/site/web/index.php(20): require('/home/vagrant/S...')
#21 {main}

FYI: the whole site throws this error.

For some reason the function Plugin::getInstance() inside the craft\commerce\elements\Product class behaviors function returns null.
Is it possible that the ScoutIndex::index function is called before the craft commerce product class is loaded or something?

@brambeekman
Copy link

Did you find a fix for this?

@jornwildenbeest
Copy link
Author

@brambeekman for me it was adding the following code in the beginnen of the scout.php config file.

<?php
use craft\commerce\elements\Product;
use craft\commerce\Plugin as CommercePlugin;
use craft\elements\Category;

$commerce = new CommercePlugin(0);

But I couldn't figure out what was causing the error..

@janhenckens
Copy link
Member

Instantiating the Commerce plugin before new Product() is called in Scout does indeed fix it.
I can fix it by using a different method to check wether something is an element or not, I'll try to have a fix out by the end of the week.

@janhenckens janhenckens modified the milestones: 2.5.0, 2.4.1 Aug 13, 2021
@janhenckens
Copy link
Member

janhenckens commented Aug 13, 2021

Fixed in today's release!

davist11 pushed a commit to vigetlabs/craft-scout that referenced this issue Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 commerce Craft Commerce related
Projects
None yet
Development

No branches or pull requests

3 participants