You are here

function mailchimp_ecommerce_update_product in Mailchimp E-Commerce 7

Updates an existing product in Mailchimp.

Mailchimp only allows for product variants to be updated. The parent product cannot be changed once created. This function will update the variant associated with the given product ID and SKU.

Parameters

string $product_id: Unique ID of the product.

string $product_variant_id: ID of the product variant. May be identical to $product_id for single products.

string $title: The product title.

string $sku: The product SKU.

float $price: The product price.

string $image_url: A URL to a representative product Image.

int $stock: If enabled, the current active inventory.

2 calls to mailchimp_ecommerce_update_product()
mailchimp_ecommerce_commerce_commerce_product_update in modules/mailchimp_ecommerce_commerce/mailchimp_ecommerce_commerce.module
Implements hook_commerce_product_update().
mailchimp_ecommerce_ubercart_node_update in modules/mailchimp_ecommerce_ubercart/mailchimp_ecommerce_ubercart.module
Implements hook_node_update().

File

./mailchimp_ecommerce.module, line 1066
Mailchimp eCommerce core functionality.

Code

function mailchimp_ecommerce_update_product($product_id, $product_variant_id, $title, $description = '', $sku, $url, $price, $image_url = '', $stock = 1) {
  try {
    $store_id = mailchimp_ecommerce_get_store_id();
    if (empty($store_id)) {
      throw new Exception('Cannot update a product without a store ID.');
    }

    /* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
    $mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
    $variant = [
      'id' => $product_variant_id,
      'title' => $title,
      'sku' => $sku,
      'url' => $url,
      'price' => $price,
      'image_url' => $image_url,
      'inventory_quantity' => (int) $stock,
    ];

    // This code only runs if the 404 isn't returned on get product.
    $mc_ecommerce
      ->updateProduct($store_id, $product_id, [
      $variant,
    ], [
      'title' => $title,
      'description' => $description,
      'sku' => $sku,
      'url' => $url,
      'price' => $price,
      'image_url' => $image_url,
      'inventory_quantity' => (int) $stock,
    ]);
  } catch (Exception $e) {
    mailchimp_ecommerce_log_error_message('Unable to add a product: ' . $e
      ->getMessage());
    mailchimp_ecommerce_show_error($e
      ->getMessage());
  }
}