You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Run the ./craft shopify/sync/products command
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
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: