You are here

public function ProductEventSubscriber::productInsert in Mailchimp E-Commerce 8

Respond to event fired after saving a new product.

File

modules/mailchimp_ecommerce_commerce/src/EventSubscriber/ProductEventSubscriber.php, line 37

Class

ProductEventSubscriber
Event Subscriber for Commerce Products.

Namespace

Drupal\mailchimp_ecommerce_commerce\EventSubscriber

Code

public function productInsert(ProductEvent $event) {

  /** @var Product $product */
  $product = $event
    ->getProduct();
  $product_id = $product
    ->get('product_id')->value;
  $title = !empty($product
    ->get('title')->value) ? $product
    ->get('title')->value : '';
  $description = !empty($product
    ->get('body')->value) ? $product
    ->get('body')->value : '';

  // TODO Fix Type
  $type = !empty($product
    ->get('type')->value) ? $product
    ->get('type')->value : '';
  $variants = $this->product_handler
    ->buildProductVariants($product);
  $url = $this->product_handler
    ->buildProductUrl($product);
  $image_url = $this->product_handler
    ->getProductImageUrl($product);
  $this->product_handler
    ->addProduct($product_id, $title, $url, $image_url, $description, $type, $variants);
}