November Happy Hour will be moved to Thursday December 5th.

How to programmatically disable products? Commerce v8

Vote:
 

Hi all,

Is it possible to programmatically make products (and all child variations) unavailable?

I currently have this, but it doesn't seem to work:

SiteProductContent productContent;
if (_catalogRepository.TryGetByEntryCode(productInclusion, out productContent))
 {
    var product = productContent.CreateWritableClone();
    product.OnSale = false;

    _contentRepository.Save(product, SaveAction.Publish, AccessLevel.NoAccess);
}

If I fetch the product again immediately after, the OnSale value is null and when checking in Commerce Manager, it still shows as available...

I had thought there'd be plenty of docs on doing this kind of thing already but I've not been able to find any, so thought I'd try here.

Any help would be greatly appreciated,

Cheers,

James

#195649
Aug 02, 2018 18:23
Vote:
 

Might be wrong, haven't worked with Commerce 8 (because it's a tad old, you should upgrade if you have the green bills to do it :D), but since it seems to already be content, set the content to expired and hopefully it'll work.

#195650
Aug 02, 2018 18:37
Vote:
 

To be honest version handling in Commerce 8 was a bit sloppy (well, that might be an understatement depending on who you ask). To handle versions better you should upgrade to at least Commerce 9 - and latest version is always recommended.

There are several way to make a product unavailable:

  • Add a boolean property to the product content and use that as filter. This is of course more work for you but it is fairly version-independent.
  • Set CatalogEntryDto.CatalogEntry.IsActive to false. This means you would have to use ICatalogSystem.
  • Set CatalogEntryDto.CatalogEntry StartDate to the future, or EndDate to the past. This relates to IContent StartPublish/StopPublish, but is not 1:1 equivalent, and it can be fairly complicated.

So in short you would need to use ICatalogSystem/IsActive to control it. Note that this will make all languages "unpublished" - which I guess is what you want.

#195652
Aug 02, 2018 18:51
Vote:
 

Thanks for the advice, guys.

Unfortunately, this piece of work is to help bridge the gap between a major upgrade project, so I can't really embark on upgrading this codebase further.

I've tried a combination of things and at some point, one of them seemed to work and mark the product as unavailable in Commerce Manager, however, I'd iterated a few times before I noticed and now I'm not sure what the magic combination was that worked.

Below is what I currently have -

var endDate = DateTime.UtcNow.AddDays(-1);
var dto = CatalogContext.Current.GetCatalogEntryDto(productInclusion);
dto.CatalogEntry[0].IsActive = false;
dto.CatalogEntry[0].EndDate = endDate;
CatalogContext.Current.SaveCatalogEntry(dto);

if (_catalogRepository.TryGetByEntryCode(productInclusion, out SiteProductContent productContent))
{
	var product = productContent.CreateWritableClone<SiteProductContent>();

	//product.IsPendingPublish = true;
	product.StopPublish = endDate;
	product.AvailableDate = DateTime.UtcNow.AddYears(31);
	product.OnSale = false;

	_contentRepository.Save(product, SaveAction.Publish, AccessLevel.NoAccess);
	DataFactoryCache.RemovePage(product.ContentLink);

	result.DisableCount++;

	OnContentDisabled(product, result.DisableCount, total);
}
CatalogCache.Clear();

It's a bit of a mess but if either of you could narrow it down to the bits that should work, that'd be great. I'd hoped the top five lines alone would work but that hasn't appeared to be the case. undecided

#195682
Aug 03, 2018 15:40
Vote:
 

The block inside the if should not be needed :)

#195684
Aug 03, 2018 17:29
Vote:
 

Yep, had literally just gotten it working based off of this article - https://world.episerver.com/forum/developer-forum/Episerver-Commerce/Thread-Container/2016/5/expire-a-product/

Thanks for all your help!

#195685
Aug 03, 2018 17:34
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.