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

getDefaultVariant() returning null in a EVENT_BEFORE_SYNCHRONIZE_PRODUCT event during the sync #117

Closed
bleepsandblops opened this issue Jun 2, 2024 · 3 comments
Labels
bug Something isn't working Plugins → Shopify For Linear sync Plugins For Linear sync

Comments

@bleepsandblops
Copy link

Description

It looks like some products do not have a default variant, as I'm getting this error message when running the sync with an event (see below):

Exception 'TypeError' with message 'craft\shopify\elements\Product::getDefaultVariant(): Return value must be of type array, null returned'

in /var/www/html/vendor/craftcms/shopify/src/elements/Product.php:320

My code of the event:

Event::on(
            Products::class,
            Products::EVENT_BEFORE_SYNCHRONIZE_PRODUCT,
            function(ShopifyProductSyncEvent $event) {
                // Example 1: Cancel the sync if a flag is set via a Shopify metafield:
                $metafields = $event->element->getMetaFields();
                $product = $event->element;

                if (
                    ($product->shopifyStatus == 'archived') ||
                    ($product->publishedAt == null)
                ) {
                    $event->isValid = false;
                }

                // Example 2: Set a field value from metafield data:
                if (($event->isValid) && ($product->getDefaultVariant() !== null)) {
                    echo $product->title . "\n";
                    echo $product->publishedScope . "\n";
                    $event->element->setFieldValue('shopifyPrice', $product->getDefaultVariant()['price']);
                }
            }
        );

The only way to make it work is to remove the return type in the getDefaultVariant() function definition here.

It's a shop with thousands of products, but I can't quite find the reason why some go through and some don't.

Steps to reproduce

  1. Run the ./craft shopify/sync/products command
  2. Get the error message on some products

Additional info

  • Craft version: 4.9.5
  • PHP version: 8.2.3
  • Database driver & version: MariaDB 10.3.38
  • Plugins & versions: Shopify 4.1.2
@bleepsandblops bleepsandblops added bug Something isn't working Plugins For Linear sync Plugins → Shopify For Linear sync labels Jun 2, 2024
@bleepsandblops
Copy link
Author

Apologies, this looks like a duplicate of #112

@nfourtythree
Copy link
Contributor

Information updated in #112 .

Closing this as a duplicate.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Plugins → Shopify For Linear sync Plugins For Linear sync
Projects
None yet
Development

No branches or pull requests

2 participants