public function ProductHandler::buildProductVariants in Mailchimp E-Commerce 8
Returns product variant data formatted for use with Mailchimp.
Parameters
\Drupal\commerce_product\Entity\Product $product: The Commerce Product.
Return value
array Array of product variant data.
File
- src/
ProductHandler.php, line 246
Class
- ProductHandler
- Product handler.
Namespace
Drupal\mailchimp_ecommerceCode
public function buildProductVariants(Product $product) {
$variants = [];
$product_variations = $product
->get('variations')
->getValue();
if (!empty($product_variations)) {
foreach ($product_variations as $variation_data) {
/** @var ProductVariation $product_variation */
$product_variation = ProductVariation::load($variation_data['target_id']);
$url = $this
->buildProductUrl($product);
$variant = [
'id' => $product_variation
->id(),
'title' => $product
->getTitle(),
'url' => $url,
'sku' => $product_variation
->getSku(),
'stock' => 100,
];
$price = $product_variation
->getPrice();
if (!empty($price)) {
$variant['price'] = $price
->getNumber();
}
else {
$variant['price'] = 0;
}
// Product variations contain a currency code, but Mailchimp requires
// store currency to be set at the point when the store is created, so
// the variation currency is ignored here.
// TODO: Make sure the user knows this through a form hint.
$variants[] = $variant;
}
}
return $variants;
}