You are here

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

@inheritdoc

Overrides ProductHandlerInterface::updateProduct

File

src/ProductHandler.php, line 45

Class

ProductHandler
Product handler.

Namespace

Drupal\mailchimp_ecommerce

Code

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

    // Ubercart doesn't have product objects. So, just pass the ID.
    if (!is_string($product)) {
      $product_id = $product
        ->get('product_id')->value;
    }
    else {
      $product_id = $product;
    }

    /* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
    $mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');

    // Update the base product with no variant.
    $mc_ecommerce
      ->updateProduct($store_id, $product_id, $variants, [
      'title' => $title,
      'description' => $description,
      'type' => $type,
      'url' => $url,
      'image_url' => $image_url,
    ]);
  } catch (\Exception $e) {
    if ($e
      ->getCode() == 404) {
      drupal_set_message('This product doesn\'t exist in Mailchimp. Please sync all your products.');
    }
    else {

      // An actual error occurred; pass on the exception.
      mailchimp_ecommerce_log_error_message('Unable to update product: ' . $e
        ->getMessage());
      drupal_set_message($e
        ->getMessage(), 'error');
    }
  }
}