SHARE
TWEET

Untitled

a guest Nov 19th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. global class XC_SpotlightProductsController {
  2.     @RemoteAction
  3.     global static Map<String, Object> getSpotlightProducts(ccrz.cc_RemoteActionContext ctx) {
  4.         ccrz.cc_RemoteActionResult res = ccrz.cc_CallContext.init(ctx);
  5.         String storefront = ctx.storefront;
  6.         Map<String, Object> success = new Map<String, Object>();
  7.         try{
  8.  
  9.             List<ccrz__E_FeaturedProduct__c> spotlightProducts = [SELECT XC_Template__c,ccrz__Product__c, ccrz__Product__r.XC_Root_Style__c, ccrz__Sequence__c,Id
  10.             FROM ccrz__E_FeaturedProduct__c where ccrz__Type__c = 'Spotlight' and ccrz__Enabled__c= true and ccrz__Storefront__c =:ctx.storefront
  11.             order by ccrz__Sequence__c];
  12.  
  13.  
  14.             Set<String> productIds = new Set<String>();
  15.  
  16.             for(ccrz__E_FeaturedProduct__c spotlight : spotlightProducts){
  17.                 productIds.add(spotlight.ccrz__Product__c);
  18.             }
  19.  
  20.             Map<Id, ccrz__E_Product__c> products = new Map<Id, ccrz__E_Product__c>([SELECT Id, XC_Root_Style__c, XC_ColorSwatchURI__c, XC_Color__c, XC_ColorName__c, XC_ColorFamily__c FROM ccrz__E_Product__c
  21.             where Id in :productIds]);
  22.  
  23.             Map<String, ProductData> productToProductData = new Map<String, ProductData>();
  24.             List<ccrz__E_ProductMedia__c> productMedias = [select id, ccrz__MediaType__c, ccrz__URI__c, ccrz__Product__c from ccrz__E_ProductMedia__c
  25.             where ccrz__Product__c in:products.keySet() and ccrz__MediaType__c in( 'Product Image', 'Alternate Image')
  26.             order by ccrz__MediaType__c asc];
  27.  
  28.             for(ccrz__E_FeaturedProduct__c spotlightProduct : spotlightProducts){
  29.                 for(ccrz__E_ProductMedia__c media : productMedias) {
  30.                     if (spotlightProduct.ccrz__Product__c == media.ccrz__Product__c) {
  31.                         ProductData productData = productToProductData.get(spotlightProduct.ccrz__Product__c);
  32.                         if (productData == null) {
  33.                             productData = new ProductData();
  34.                         }
  35.                         if (spotlightProduct.XC_Template__c == 'Footwear') {
  36.                             if (media.ccrz__MediaType__c == 'Product Image') {
  37.                                 productData = checkImages(productData, media.ccrz__URI__c, true);
  38.                             } else if (media.ccrz__MediaType__c == 'Alternate Image') {
  39.                                 productData = checkAlternates(productData, media.ccrz__URI__c);
  40.                             }
  41.                         } else if (spotlightProduct.XC_Template__c == 'Other') {
  42.                             if (media.ccrz__MediaType__c == 'Product Image') {
  43.                                 productData = checkImages(productData, media.ccrz__URI__c, false);
  44.                             }
  45.                         }
  46.                         productToProductData.put(spotlightProduct.ccrz__Product__c, productData);
  47.                     }
  48.                 }
  49.             }
  50.  
  51.  
  52.  
  53.  
  54.             success.put('success', true);
  55.  
  56.  
  57.             success.put('products', products);
  58.  
  59.  
  60.             success.put('productMedias', productMedias);
  61.  
  62.             success.put('spotlightProducts', spotlightProducts);
  63.  
  64.             success.put('productToProductData', productToProductData);
  65.  
  66.             system.debug(JSON.serialize(products));
  67.         }catch(Exception e){
  68.             success.put('success', false);
  69.             success.put('error ', e.getMessage() + ':  ' + e.getStackTraceString());
  70.  
  71.             system.debug(success);
  72.         }
  73.  
  74.         return success;
  75.     }
  76.     public static ProductData checkImages(ProductData productData, String value, Boolean isFootwear){
  77.         if(String.isNotBlank(productData.productImage)){
  78.             if(isFootwear){
  79.                 productData = checkAlternates(productData, value);
  80.             }
  81.         }
  82.         else{
  83.             productData.productImage = value;
  84.         }
  85.  
  86.         return productData;
  87.     }
  88.     public static ProductData checkAlternates(ProductData productData, String value){
  89.         if(String.isNotBlank(productData.alternateImage1)){
  90.             productData.alternateImage2 = value;
  91.         }
  92.         else{
  93.             productData.alternateImage1 = value;
  94.         }
  95.         return productData;
  96.     }
  97.     public class ProductData{
  98.         public String productImage{get;set;}
  99.         public String alternateImage1{get;set;}
  100.         public String alternateImage2{get;set;}
  101.     }
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top