Uncaught Error: Class 'HubSpot\Client\Crm\Products\Model\PublicAssociationsForObject' not found

I get this error:

Uncaught Error: Class 'HubSpot\Client\Crm\Products\Model\PublicAssociationsForObject' not found

I copied this code from docs, but I edited some of it:

use \HubSpot\Factory;
use \HubSpot\Client\Crm\Products\ApiException;
use \HubSpot\Client\Crm\Products\Model\AssociationSpec;
use \HubSpot\Client\Crm\Products\Model\PublicAssociationsForObject;
use \HubSpot\Client\Crm\Associations\Model\PublicObjectId;
use \HubSpot\Client\Crm\Products\Model\SimplePublicObjectInputForCreate;

$hubspot = Factory::createWithAccessToken('pat-eu1-14f14ddd-8a5c-4eb3-b1f1-b13f1d36fb46');

$properties1 = [
 'description' => 'Onboarding service for data product',
 'hs_cost_of_goods_sold' => '600.00',
 'hs_recurring_billing_period' => '12',
 'hs_sku' => '191902',
 'name' => 'Implementation Service ',
 'price' => '6000.00'
];
$to1 = new PublicObjectId([
 'id' => '101'
]);
$associationSpec1 = new AssociationSpec([
 'association_category' => 'HUBSPOT_DEFINED',
 'association_type_id' => 2
]);
$publicAssociationsForObject1 = new PublicAssociationsForObject([
 'to' => $to1,
 'types' => [$associationSpec1]
]);
$simplePublicObjectInputForCreate = new SimplePublicObjectInputForCreate([
 'properties' => $properties1,
 'associations' => [$publicAssociationsForObject1],
]);
try {
 $apiResponse = $hubspot->crm()->products()->basicApi()->create($simplePublicObjectInputForCreate);
 var_dump($apiResponse);
} catch (ApiException $e) {
 echo "Exception when calling basic_api->create: ", $e->getMessage();
}

Please help

Hey, @mrabdennour :waving_hand: Thanks for reaching out! I have a few clarifying questions, and I’d like to invite some of our community members to the conversation.

Questions:

  • Can you clarify your goal, please?
  • Is it to create a Product and associate the Product to an existing Contact?
  • Or are you attempting to create a Line Item from a Product and then associate it to an exiting Object, a Contact for example?

Hey, @skimura @tominal, do you have PHP-specific tips you can share with @mrabdennour?

Thank you very much! — Jaycee

I have products in my website database, but I need to assosiate them with my contacts, how to do that?

@mrabdennour

Hi.

I think the direct cause is that ‘PublicAssociationsForObject’ is not included in the hubspot SDK.

I use ‘hubspot/api-client’ 9.4, but it not include class ‘PublicAssociationsForObject’.

→ ‘master branch’ has that class, but ‘9.4.0’ not include.

(check with ‘composer install’ and ‘direct download’)

→‘10.0.0-beta’ has that class.

git

download with ( 9.4.0 )

If you want use this class, your should use ‘10.0.0-beta’.

Or you want to use stable version ( ex, 9.4.0 ),

how about this? (Not tested. )

* I don’t know ‘products’ can associate with ‘contacts’.

$hubspot = Factory::createWithAccessToken('...');

$properties1 = [
 'description' => 'Onboarding service for data product',
 'hs_cost_of_goods_sold' => '600.00',
 'hs_recurring_billing_period' => '12',
 'hs_sku' => '191902',
 'name' => 'Implementation Service ',
 'price' => '6000.00'
];

$toObjectType = '...';
$toObjectId = '101';
$associationTypeId = 2;

$simplePublicObject = new SimplePublicObjectInput();
$simplePublicObject->setProperties($properties1);

try {
 $productCreateApiResponse = $hubspot->crm()->products()->basicApi()->create($simplePublicObject);

 $productId = $apiResponse->getId();
 $productAssocitaionCreateApiResponse = $hubspot->crm()->products()->associationsApi()->create(
 $productId,
 $toObjectType,
 $toObjectId,
 $associationTypeId,
 );

 var_dump($$productCreateApiResponse);
 var_dump($productAssocitaionCreateApiResponse);
} catch (ApiException $e) {
 echo "Exception when calling basic_api->create: ", $e->getMessage();
}

Thanks.

@Jaycee_Lewis

Thank you for mention.