google-shopping-feed icon indicating copy to clipboard operation
google-shopping-feed copied to clipboard

Variants should support unique MPN

Open LetsPrint3D opened this issue 6 years ago • 3 comments

According to the Google documentation, it says "Typically, each variant of a product (different colors or sizes) has its own MPN, so make sure to submit the correct value."

The function variant() in this library calls getGroupIdentifier() which requires an MPN (or GTIN) already be set before it can create the variant, meaning it is using the same one for all variants.

How can this be fixed?

LetsPrint3D avatar Jul 01 '18 18:07 LetsPrint3D

getGroupIdentifier() is just used for the group of products, you can still use $variant->mpn('blabla'); to set the variants mpn value.

lukesnowden avatar Jul 03 '18 09:07 lukesnowden

Thanks for the feedback. What I am referring to is the exception MissingIdentifierException: Please define a GTIN or MPN value before creating a variant. which is thrown if I try to create the variant without setting the MPN first. I may just be overlooking something but this is what my code looks like...

foreach ($products as $product)
{
	$product_link = $store['domain'] . '/products/' . $product['handle'];
	$product_image = $product['image']['src'];

	$item = GoogleShopping::createItem();

	$item->id($product['id']);
	$item->title($product['title']);
	$item->description($product['body_html']);

	$item->link($product_link);
	$item->image_link($product_image);
	
	foreach ($product['variants'] as $variant)
	{
		$v = $item->variant();
		$v->mpn($variant->sku);
	}
}

LetsPrint3D avatar Jul 03 '18 14:07 LetsPrint3D

If you are creating variants, then just call the MPN anything, it is only used for the group identifier as the initial object should be destroyed;

foreach($products as $product)
{
	$product_link = $store['domain'] . '/products/' . $product['handle'];
	$product_image = $product['image']['src'];

	$item = GoogleShopping::createItem();

	$item->id($product['id']);
	$item->title($product['title']);
	$item->description($product['body_html']);

	$item->link($product_link);
	$item->image_link($product_image);
        $item->mpn(substr(0, 8, sha1($product['id'])));
	
	foreach ($product['variants'] as $variant)
	{
		$v = $item->variant();
		$v->mpn($variant->sku);
	}

        $item->delete();

}

lukesnowden avatar Jul 03 '18 14:07 lukesnowden