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.
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):
My code of the event:
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
./craft shopify/sync/products
commandAdditional info