Guest User

Untitled

a guest
Nov 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.94 KB | None | 0 0
  1. /*
  2. Some of these override earlier varien/product.js methods, therefore
  3. varien/product.js must have been included prior to this file.
  4. */
  5.  
  6. Product.Config.prototype.getMatchingSimpleProduct = function(){
  7. var inScopeProductIds = this.getInScopeProductIds();
  8. if ((typeof inScopeProductIds != 'undefined') && (inScopeProductIds.length == 1)) {
  9. return inScopeProductIds[0];
  10. }
  11. return false;
  12. };
  13.  
  14. /*
  15. Find products which are within consideration based on user's selection of
  16. config options so far
  17. Returns a normal array containing product ids
  18. allowedProducts is a normal numeric array containing product ids.
  19. childProducts is a hash keyed on product id
  20. optionalAllowedProducts lets you pass a set of products to restrict by,
  21. in addition to just using the ones already selected by the user
  22. */
  23. Product.Config.prototype.getInScopeProductIds = function(optionalAllowedProducts) {
  24.  
  25. var childProducts = this.config.childProducts;
  26. var allowedProducts = [];
  27.  
  28. if ((typeof optionalAllowedProducts != 'undefined') && (optionalAllowedProducts.length > 0)) {
  29. // alert("starting with: " + optionalAllowedProducts.inspect());
  30. allowedProducts = optionalAllowedProducts;
  31. }
  32.  
  33. for(var s=0, len=this.settings.length-1; s<=len; s++) {
  34. if (this.settings[s].selectedIndex <= 0){
  35. break;
  36. }
  37. var selected = this.settings[s].options[this.settings[s].selectedIndex];
  38. if (s==0 && allowedProducts.length == 0){
  39. allowedProducts = selected.config.allowedProducts;
  40. } else {
  41. // alert("merging: " + allowedProducts.inspect() + " with: " + selected.config.allowedProducts.inspect());
  42. allowedProducts = allowedProducts.intersect(selected.config.allowedProducts).uniq();
  43. // alert("to give: " + allowedProducts.inspect());
  44. }
  45. }
  46.  
  47. //If we can't find any products (because nothing's been selected most likely)
  48. //then just use all product ids.
  49. if ((typeof allowedProducts == 'undefined') || (allowedProducts.length == 0)) {
  50. productIds = Object.keys(childProducts);
  51. } else {
  52. productIds = allowedProducts;
  53. }
  54. return productIds;
  55. };
  56.  
  57.  
  58. Product.Config.prototype.getProductIdOfCheapestProductInScope = function(priceType, optionalAllowedProducts) {
  59.  
  60. var childProducts = this.config.childProducts;
  61. var productIds = this.getInScopeProductIds(optionalAllowedProducts);
  62.  
  63. var minPrice = Infinity;
  64. var lowestPricedProdId = false;
  65.  
  66. //Get lowest price from product ids.
  67. for (var x=0, len=productIds.length; x<len; ++x) {
  68. var thisPrice = Number(childProducts[productIds[x]][priceType]);
  69. if (thisPrice < minPrice) {
  70. minPrice = thisPrice;
  71. lowestPricedProdId = productIds[x];
  72. }
  73. }
  74. return lowestPricedProdId;
  75. };
  76.  
  77.  
  78. Product.Config.prototype.getProductIdOfMostExpensiveProductInScope = function(priceType, optionalAllowedProducts) {
  79.  
  80. var childProducts = this.config.childProducts;
  81. var productIds = this.getInScopeProductIds(optionalAllowedProducts);
  82.  
  83. var maxPrice = 0;
  84. var highestPricedProdId = false;
  85.  
  86. //Get highest price from product ids.
  87. for (var x=0, len=productIds.length; x<len; ++x) {
  88. var thisPrice = Number(childProducts[productIds[x]][priceType]);
  89. if (thisPrice > maxPrice) {
  90. maxPrice = thisPrice;
  91. highestPricedProdId = productIds[x];
  92. }
  93. }
  94. return highestPricedProdId;
  95. };
  96.  
  97.  
  98.  
  99. Product.Config.prototype.updateFormProductId = function(productId){
  100. if (!productId) {
  101. return false;
  102. }
  103. var currentAction = $('product_addtocart_form').action;
  104. newcurrentAction = currentAction.sub(/product\/\d+\//, 'product/' + productId + '/');
  105. $('product_addtocart_form').action = newcurrentAction;
  106. $('product_addtocart_form').product.value = productId;
  107. };
  108.  
  109.  
  110. Product.Config.prototype.addParentProductIdToCartForm = function(parentProductId) {
  111. if (typeof $('product_addtocart_form').cpid != 'undefined') {
  112. return; //don't create it if we have one..
  113. }
  114. var el = document.createElement("input");
  115. el.type = "hidden";
  116. el.name = "cpid";
  117. el.value = parentProductId.toString();
  118. $('product_addtocart_form').appendChild(el);
  119. };
  120.  
  121.  
  122.  
  123. Product.OptionsPrice.prototype.updateSpecialPriceDisplay = function(price, finalPrice) {
  124.  
  125. var prodForm = $('product_addtocart_form');
  126.  
  127. var specialPriceBox = prodForm.select('p.special-price');
  128. var oldPricePriceBox = prodForm.select('p.old-price, p.was-old-price');
  129. var magentopriceLabel = prodForm.select('span.price-label');
  130.  
  131. if (price == finalPrice) {
  132. specialPriceBox.each(function(x) {x.hide();});
  133. magentopriceLabel.each(function(x) {x.hide();});
  134. oldPricePriceBox.each(function(x) {
  135. x.removeClassName('old-price');
  136. x.addClassName('was-old-price');
  137. });
  138. }else{
  139. specialPriceBox.each(function(x) {x.show();});
  140. magentopriceLabel.each(function(x) {x.show();});
  141. oldPricePriceBox.each(function(x) {
  142. x.removeClassName('was-old-price');
  143. x.addClassName('old-price');
  144. });
  145. }
  146. };
  147.  
  148. //This triggers reload of price and other elements that can change
  149. //once all options are selected
  150. Product.Config.prototype.reloadPrice = function() {
  151. var childProductId = this.getMatchingSimpleProduct();
  152. var childProducts = this.config.childProducts;
  153. var usingZoomer = false;
  154. if(this.config.imageZoomer){
  155. usingZoomer = true;
  156. }
  157.  
  158. if (childProductId){
  159. var price = childProducts[childProductId]["price"];
  160. var finalPrice = childProducts[childProductId]["finalPrice"];
  161. optionsPrice.productPrice = finalPrice;
  162. optionsPrice.productOldPrice = price;
  163. optionsPrice.reload();
  164. optionsPrice.reloadPriceLabels(true);
  165. optionsPrice.updateSpecialPriceDisplay(price, finalPrice);
  166. this.updateProductShortDescription(childProductId);
  167. this.updateProductDescription(childProductId);
  168. this.updateProductName(childProductId);
  169. this.updateProductAttributes(childProductId);
  170. this.updateFormProductId(childProductId);
  171. this.addParentProductIdToCartForm(this.config.productId);
  172. this.showCustomOptionsBlock(childProductId, this.config.productId);
  173. this.showFullImageDiv(childProductId, this.config.productId);
  174. // if (usingZoomer) {
  175. // this.showFullImageDiv(childProductId, this.config.productId);
  176. // }else{
  177. // this.updateProductImage(childProductId);
  178. // }
  179.  
  180. } else {
  181. var cheapestPid = this.getProductIdOfCheapestProductInScope("finalPrice");
  182. //var mostExpensivePid = this.getProductIdOfMostExpensiveProductInScope("finalPrice");
  183. var price = childProducts[cheapestPid]["price"];
  184. var finalPrice = childProducts[cheapestPid]["finalPrice"];
  185. optionsPrice.productPrice = finalPrice;
  186. optionsPrice.productOldPrice = price;
  187. optionsPrice.reload();
  188. optionsPrice.reloadPriceLabels(false);
  189. optionsPrice.updateSpecialPriceDisplay(price, finalPrice);
  190. this.updateProductShortDescription(false);
  191. this.updateProductDescription(false);
  192. this.updateProductName(false);
  193. this.updateProductAttributes(false);
  194. this.showCustomOptionsBlock(false, false);
  195. this.showFullImageDiv(false, false);
  196. // if (usingZoomer) {
  197. // this.showFullImageDiv(false, false);
  198. // }else{
  199. // this.updateProductImage(false);
  200. // }
  201. }
  202. };
  203.  
  204.  
  205.  
  206. Product.Config.prototype.updateProductImage = function(productId) {
  207. var imageUrl = this.config.imageUrl;
  208. if(productId && this.config.childProducts[productId].imageUrl) {
  209. imageUrl = this.config.childProducts[productId].imageUrl;
  210. }
  211.  
  212. if (!imageUrl) {
  213. return;
  214. }
  215.  
  216. if($('image')) {
  217. $('image').src = imageUrl;
  218. } else {
  219. $$('#product-image img').each(function(el) {
  220. var dims = el.getDimensions();
  221. el.src = imageUrl;
  222. el.width = dims.width;
  223. el.height = dims.height;
  224. });
  225. }
  226. };
  227.  
  228. Product.Config.prototype.updateProductName = function(productId) {
  229. var productName = this.config.productName;
  230. if (productId && this.config.childProducts[productId].productName) {
  231. productName = this.config.childProducts[productId].productName;
  232. }
  233. $$('#product-info h1').each(function(el) {
  234. el.innerHTML = productName;
  235. });
  236. };
  237.  
  238. Product.Config.prototype.updateProductShortDescription = function(productId) {
  239. var shortDescription = this.config.shortDescription;
  240. if (productId && this.config.childProducts[productId].shortDescription) {
  241. shortDescription = this.config.childProducts[productId].shortDescription;
  242. }
  243. $$('#product-info #product-description').each(function(el) {
  244. el.innerHTML = shortDescription;
  245. });
  246. };
  247.  
  248. Product.Config.prototype.updateProductDescription = function(productId) {
  249. var description = this.config.description;
  250. if (productId && this.config.childProducts[productId].description) {
  251. description = this.config.childProducts[productId].description;
  252. }
  253. console.log(description);
  254. $$('#product-overview .white-box-inner').each(function(el) {
  255. el.innerHTML = description;
  256. });
  257. };
  258.  
  259. Product.Config.prototype.updateProductAttributes = function(productId) {
  260. var productAttributes = this.config.productAttributes;
  261. if (productId && this.config.childProducts[productId].productAttributes) {
  262. productAttributes = this.config.childProducts[productId].productAttributes;
  263. }
  264. //If config product doesn't already have an additional information section,
  265. //it won't be shown for associated product either. It's too hard to work out
  266. //where to place it given that different themes use very different html here
  267. $$('#extended-features').each(function(el) {
  268. el.innerHTML = jQuery(productAttributes).html();
  269. decorateTable('product-attribute-specs-table');
  270. });
  271. };
  272.  
  273. Product.Config.prototype.showCustomOptionsBlock = function(productId, parentId) {
  274. var coUrl = this.config.ajaxBaseUrl + "co/?id=" + productId + '&pid=' + parentId;
  275. var prodForm = $('product_addtocart_form');
  276.  
  277. if ($('SCPcustomOptionsDiv')==null) {
  278. return;
  279. }
  280.  
  281. Effect.Fade('SCPcustomOptionsDiv', { duration: 0.5, from: 1, to: 0.5 });
  282. if(productId) {
  283. //Uncomment the line below if you want an ajax loader to appear while any custom
  284. //options are being loaded.
  285. //$$('span.scp-please-wait').each(function(el) {el.show()});
  286.  
  287. //prodForm.getElements().each(function(el) {el.disable()});
  288. new Ajax.Updater('SCPcustomOptionsDiv', coUrl, {
  289. method: 'get',
  290. evalScripts: true,
  291. onComplete: function() {
  292. $$('span.scp-please-wait').each(function(el) {el.hide()});
  293. Effect.Fade('SCPcustomOptionsDiv', { duration: 0.5, from: 0.5, to: 1 });
  294. //prodForm.getElements().each(function(el) {el.enable()});
  295. }
  296. });
  297. } else {
  298. $('SCPcustomOptionsDiv').innerHTML = '';
  299. window.opConfig = new Product.Options([]);
  300. }
  301. };
  302.  
  303.  
  304. Product.Config.prototype.showFullImageDiv = function(productId, parentId) {
  305. var imgUrl = this.config.ajaxBaseUrl + "image/?id=" + productId + '&pid=' + parentId;
  306. var prodForm = $('product_addtocart_form');
  307. var destElement = false;
  308. var defaultZoomer = this.config.imageZoomer;
  309.  
  310. prodForm.select('#product-image').each(function(el) {
  311. destElement = el;
  312. });
  313.  
  314. if(productId) {
  315. jQuery('body').append('<div id="ajax-load"></div>');
  316. jQuery('#ajax-load').center();
  317.  
  318. new Ajax.Updater(destElement, imgUrl, {
  319. method: 'get',
  320. evalScripts: false,
  321. onComplete: function(data) {
  322. jQuery('#ajax-load').remove();
  323. jQuery('#product-image-browser').html(jQuery(data.responseText).html());
  324. jQuery('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
  325. // //Product.Zoom needs the *image* (not just the html source from the ajax)
  326. // //to have loaded before it works, hence image object and onload handler
  327. // if ($('image')){
  328. // var imgObj = new Image();
  329. // imgObj.src = $('image').src;
  330. // imgObj.onload = function() {product_zoom = new Product.Zoom('image', 'track', 'handle', 'zoom_in', 'zoom_out', 'track_hint'); };
  331. // } else {
  332. // destElement.innerHTML = defaultZoomer;
  333. // product_zoom = new Product.Zoom('image', 'track', 'handle', 'zoom_in', 'zoom_out', 'track_hint')
  334. // }
  335. }
  336. });
  337. } else {
  338. jQuery('#product-image-browser').html(defaultZoomer);
  339. jQuery('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
  340. // product_zoom = new Product.Zoom('product-image', 'track', 'handle', 'zoom_in', 'zoom_out', 'track_hint');
  341. }
  342. };
  343.  
  344.  
  345.  
  346. Product.OptionsPrice.prototype.reloadPriceLabels = function(productPriceIsKnown) {
  347. var priceFromLabel = '';
  348. var prodForm = $('product_addtocart_form');
  349.  
  350. if (!productPriceIsKnown && typeof spConfig != "undefined") {
  351. priceFromLabel = spConfig.config.priceFromLabel;
  352. }
  353.  
  354. var priceSpanId = 'configurable-price-from-' + this.productId;
  355. var duplicatePriceSpanId = priceSpanId + this.duplicateIdSuffix;
  356.  
  357. if($(priceSpanId) && $(priceSpanId).select('span.configurable-price-from-label'))
  358. $(priceSpanId).select('span.configurable-price-from-label').each(function(label) {
  359. label.innerHTML = priceFromLabel;
  360. });
  361.  
  362. if ($(duplicatePriceSpanId) && $(duplicatePriceSpanId).select('span.configurable-price-from-label')) {
  363. $(duplicatePriceSpanId).select('span.configurable-price-from-label').each(function(label) {
  364. label.innerHTML = priceFromLabel;
  365. });
  366. }
  367. };
  368.  
  369.  
  370.  
  371. //SCP: Forces the 'next' element to have it's optionLabels reloaded too
  372. Product.Config.prototype.configureElement = function(element) {
  373. this.reloadOptionLabels(element);
  374. if(element.value){
  375. this.state[element.config.id] = element.value;
  376. if(element.nextSetting){
  377. element.nextSetting.disabled = false;
  378. this.fillSelect(element.nextSetting);
  379. this.reloadOptionLabels(element.nextSetting);
  380. this.resetChildren(element.nextSetting);
  381. }
  382. }
  383. else {
  384. this.resetChildren(element);
  385. }
  386. this.reloadPrice();
  387. };
  388.  
  389.  
  390. //SCP: Changed logic to use absolute price ranges rather than price differentials
  391. Product.Config.prototype.reloadOptionLabels = function(element){
  392. var selectedPrice;
  393. var childProducts = this.config.childProducts;
  394.  
  395. //Don't update elements that have a selected option
  396. if(element.options[element.selectedIndex].config){
  397. return;
  398. }
  399.  
  400. for(var i=0;i<element.options.length;i++){
  401. if(element.options[i].config){
  402. var cheapestPid = this.getProductIdOfCheapestProductInScope("finalPrice", element.options[i].config.allowedProducts);
  403. var mostExpensivePid = this.getProductIdOfMostExpensiveProductInScope("finalPrice", element.options[i].config.allowedProducts);
  404. var cheapestFinalPrice = childProducts[cheapestPid]["finalPrice"];
  405. var mostExpensiveFinalPrice = childProducts[mostExpensivePid]["finalPrice"];
  406. element.options[i].text = this.getOptionLabel(element.options[i].config, cheapestFinalPrice, mostExpensiveFinalPrice);
  407. }
  408. }
  409. };
  410.  
  411. //SCP: Changed label formatting to show absolute price ranges rather than price differentials
  412. Product.Config.prototype.getOptionLabel = function(option, lowPrice, highPrice){
  413.  
  414. var str = option.label;
  415.  
  416. if (!this.config.showPriceRangesInOptions) {
  417. return str;
  418. }
  419.  
  420. var to = ' ' + this.config.rangeToLabel + ' ';
  421. var separator = ': ';
  422.  
  423. lowPrices = this.getTaxPrices(lowPrice);
  424. highPrices = this.getTaxPrices(highPrice);
  425.  
  426. if(lowPrice && highPrice){
  427. if (lowPrice != highPrice) {
  428. if (this.taxConfig.showBothPrices) {
  429. str+= separator + this.formatPrice(lowPrices[2], false) + ' (' + this.formatPrice(lowPrices[1], false) + ' ' + this.taxConfig.inclTaxTitle + ')';
  430. str+= to + this.formatPrice(highPrices[2], false) + ' (' + this.formatPrice(highPrices[1], false) + ' ' + this.taxConfig.inclTaxTitle + ')';
  431. } else {
  432. str+= separator + this.formatPrice(lowPrices[0], false);
  433. str+= to + this.formatPrice(highPrices[0], false);
  434. }
  435. } else {
  436. if (this.taxConfig.showBothPrices) {
  437. str+= separator + this.formatPrice(lowPrices[2], false) + ' (' + this.formatPrice(lowPrices[1], false) + ' ' + this.taxConfig.inclTaxTitle + ')';
  438. } else {
  439. str+= separator + this.formatPrice(lowPrices[0], false);
  440. }
  441. }
  442. }
  443. return str;
  444. };
  445.  
  446.  
  447. //SCP: Refactored price calculations into separate function
  448. Product.Config.prototype.getTaxPrices = function(price) {
  449. var price = parseFloat(price);
  450.  
  451. if (this.taxConfig.includeTax) {
  452. var tax = price / (100 + this.taxConfig.defaultTax) * this.taxConfig.defaultTax;
  453. var excl = price - tax;
  454. var incl = excl*(1+(this.taxConfig.currentTax/100));
  455. } else {
  456. var tax = price * (this.taxConfig.currentTax / 100);
  457. var excl = price;
  458. var incl = excl + tax;
  459. }
  460.  
  461. if (this.taxConfig.showIncludeTax || this.taxConfig.showBothPrices) {
  462. price = incl;
  463. } else {
  464. price = excl;
  465. }
  466.  
  467. return [price, incl, excl];
  468. };
  469.  
  470.  
  471. //SCP: Forces price labels to be updated on load
  472. //so that first select shows ranges from the start
  473. document.observe("dom:loaded", function() {
  474. //Really only needs to be the first element that has configureElement set on it,
  475. //rather than all.
  476. $('product_addtocart_form').getElements().each(function(el) {
  477. if(el.type == 'select-one') {
  478. if(el.options && (el.options.length > 1)) {
  479. el.options[0].selected = true;
  480. spConfig.reloadOptionLabels(el);
  481. }
  482. }
  483. });
  484. });
Add Comment
Please, Sign In to add comment