You are here

public function ProductHandler::addProduct in Mailchimp E-Commerce 8

@inheritdoc

Overrides ProductHandlerInterface::addProduct

File

src/ProductHandler.php, line 17

Class

ProductHandler
Product handler.

Namespace

Drupal\mailchimp_ecommerce

Code

public function addProduct($product_id, $title, $url, $image_url, $description, $type, $variants) {
  try {
    $store_id = mailchimp_ecommerce_get_store_id();
    if (empty($store_id)) {
      throw new \Exception('Cannot add a product without a store ID.');
    }

    /* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
    $mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
    $mc_ecommerce
      ->addProduct($store_id, (string) $product_id, $title, $url, $variants, [
      'description' => $description,
      'type' => $type,
      'url' => $url,
      'image_url' => $image_url,
    ]);
  } catch (\Exception $e) {

    //TODO: If add fails with product exists error code, run an update here.
    mailchimp_ecommerce_log_error_message('Unable to add product: ' . $e
      ->getMessage());
    drupal_set_message($e
      ->getMessage(), 'error');
  }
}