You are here

function mailchimp_ecommerce_delete_product_variant in Mailchimp E-Commerce 7

Deletes a product variant in Mailchimp.

Automatically deletes the product if the only product variant is removed.

Parameters

string $product_id: Unique ID of the product.

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

2 calls to mailchimp_ecommerce_delete_product_variant()
mailchimp_ecommerce_commerce_commerce_product_delete in modules/mailchimp_ecommerce_commerce/mailchimp_ecommerce_commerce.module
Implements hook_commerce_product_delete().
mailchimp_ecommerce_ubercart_node_delete in modules/mailchimp_ecommerce_ubercart/mailchimp_ecommerce_ubercart.module
Implements hook_node_delete().

File

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

Code

function mailchimp_ecommerce_delete_product_variant($product_id, $product_variant_id) {
  try {
    $store_id = mailchimp_ecommerce_get_store_id();
    if (empty($store_id)) {
      throw new Exception('Cannot delete a product variant without a store ID.');
    }

    /* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
    $mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
    try {
      $variants = $mc_ecommerce
        ->getProductVariants($store_id, $product_id);

      // Delete the variant if the product contains multiple variants.
      if ($variants->total_items > 1) {
        $mc_ecommerce
          ->deleteProductVariant($store_id, $product_id, $product_variant_id);
      }
      else {

        // Delete the product if the product has only one variant.
        $mc_ecommerce
          ->deleteProduct($store_id, $product_id);
      }
    } catch (Exception $e) {
      if ($e
        ->getCode() == 404) {

        // This product isn't in Mailchimp.
        return;
      }
      else {

        // An actual error occurred; pass on the exception.
        throw new Exception($e
          ->getMessage(), $e
          ->getCode(), $e);
      }
    }
  } catch (Exception $e) {
    mailchimp_ecommerce_log_error_message('Unable to delete product variant: ' . $e
      ->getMessage());
    mailchimp_ecommerce_show_error($e
      ->getMessage());
  }
}