You are here

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

@inheritdoc

Overrides ProductHandlerInterface::addProductVariant

File

src/ProductHandler.php, line 107

Class

ProductHandler
Product handler.

Namespace

Drupal\mailchimp_ecommerce

Code

public function addProductVariant($product_id, $product_variant_id, $title, $url, $image_url, $sku, $price, $stock) {
  try {
    $store_id = mailchimp_ecommerce_get_store_id();
    if (empty($store_id)) {
      throw new \Exception('Cannot add a product variant without a store ID.');
    }

    /* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
    $mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
    $mc_ecommerce
      ->addProductVariant($store_id, $product_id, [
      'id' => $product_variant_id,
      'title' => $title,
      'url' => $url,
      'image_url' => $image_url,
      'sku' => $sku,
      'price' => $price,
      'inventory_quantity' => $stock,
    ]);
  } catch (\Exception $e) {
    mailchimp_ecommerce_log_error_message('Unable to add product variant: ' . $e
      ->getMessage());
    drupal_set_message($e
      ->getMessage(), 'error');
  }
}