function mailchimp_ecommerce_add_product in Mailchimp E-Commerce 7
Adds a product to Mailchimp.
Adds a product variant if a product with the given ID exists. Gracefully handles updating an existing variant.
In Mailchimp, each product requires at least one product variant. This function will create a single product variant when creating new products.
A product variant is contained within a product and can be used to represent shirt size, color, etc.
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 $description: The product description.
string $type: The product type.
string $sku: The product SKU.
string $url: The product URL.
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_add_product()
- mailchimp_ecommerce_commerce_commerce_product_insert in modules/
mailchimp_ecommerce_commerce/ mailchimp_ecommerce_commerce.module - Implements hook_commerce_product_insert().
- mailchimp_ecommerce_ubercart_node_insert in modules/
mailchimp_ecommerce_ubercart/ mailchimp_ecommerce_ubercart.module - Implements hook_node_insert().
File
- ./
mailchimp_ecommerce.module, line 961 - Mailchimp eCommerce core functionality.
Code
function mailchimp_ecommerce_add_product($product_id, $product_variant_id, $title, $description = '', $type, $sku, $url, $price, $image_url = '', $stock = 1) {
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');
$variant = [
'id' => $product_variant_id,
'title' => $title,
'sku' => $sku,
'url' => $url,
'price' => $price,
'image_url' => $image_url,
'inventory_quantity' => (int) $stock,
];
// Create Mailchimp product from product type.
$mc_ecommerce
->addProduct($store_id, $product_id, $title, $url, [
$variant,
], [
'title' => $title,
'description' => $description,
'type' => $type,
'sku' => $sku,
'url' => $url,
'price' => $price,
'image_url' => $image_url,
'inventory_quantity' => (int) $stock,
]);
} catch (Exception $e) {
// Product already exists, so try adding a variant.
if ($e
->getCode() == 400) {
try {
$mc_ecommerce
->addProductVariant($store_id, $product_id, [
'id' => $product_variant_id,
'description' => $description,
'title' => $title,
'sku' => $sku,
'price' => $price,
'url' => $url,
'image_url' => $image_url,
'inventory_quantity' => (int) $stock,
]);
} catch (Exception $e) {
// If the variant already exists, update it.
if ($e
->getCode() == 400) {
try {
$mc_ecommerce
->updateProductVariant($store_id, $product_id, $product_variant_id, [
'description' => $description,
'type' => $type,
'title' => $title,
'sku' => $sku,
'url' => $url,
'price' => $price,
'image_url' => $image_url,
]);
} catch (Exception $e) {
mailchimp_ecommerce_log_error_message('Unable to update a product variant: ' . $e
->getMessage());
mailchimp_ecommerce_show_error($e
->getMessage());
}
}
else {
mailchimp_ecommerce_log_error_message('Unable to add a product variant: ' . $e
->getMessage());
mailchimp_ecommerce_show_error($e
->getMessage());
}
}
}
if ($e
->getCode() != 404 && $e
->getCode() != 400) {
// TODO: check if product has adjustments with additional SKUs defined. Add these as variants instead of default
// No existing product; create new product with default variant.
mailchimp_ecommerce_log_error_message('Unable to add a product: ' . $e
->getMessage());
mailchimp_ecommerce_show_error($e
->getMessage());
}
}
}