Navigating to PDP page from My account

There is an issue that occurred when Normally passing the url to model to navigate to the PDP page from the searcvh section.

While using the normal url, we are getting an issue that the url will redirect to the home page qnd the navigation to the homepage is not happening

In order to resolve this issue, I have encoded the URL before setting to the model

  _.extend(ItemsSearcherItemView.prototype, {
                    getContext: _.wrap(ItemsSearcherItemView.prototype.getContext, function (fn) {
                        try {
                            var originalRet = fn.apply(this, _.toArray(arguments).slice(1));
                            var option = this.options;
                            var matrixparent = option.childItem;
                            var urlOptionset = [];
                            if (this.model && matrixparent) {
                                //Code to get the url as per the options of the child item
                                var optionSet = this.model.get('options').models;
                                var newUrl = "";
                                var itemOption = _.reject(optionSet, function (item) {
                                    return (!item.get('values').length >= 1);
                                });
                                for (var i = 0; i < itemOption.length; i++) {
                                    urlOptionset.push(itemOption[i].get('itemOptionId'));
                                    var optionRequired = itemOption[i].get('itemOptionId');
                                    var selectedOptionValue = matrixparent[optionRequired];
                                    var itemOptionValue = itemOption[i].get('values');
                                    itemOptionValue = _.reject(itemOptionValue, function (item) {
                                        return item.label != selectedOptionValue;
                                    });
                                    var optionUrl = itemOptionValue[0].url.split('?')[1];
                                    newUrl += "%26" + optionUrl; // Append options for Url 
                                }
                                //end of code to get the url as per the options of the child item
                                //Code to get the image of the child item
                                var thumbnail = this.model.get('_thumbnail');
                                var urlSubstring;
                                var urlComponent;
                                var newImgUrl = null;
                                var itemImages_detail = this.model.get('itemimages_detail') ? this.model.get('itemimages_detail') : null;
                                urlOptionset.forEach(function (element) {
                                    urlComponent = urlComponent ? urlComponent + matrixparent[element] : matrixparent[element];
                                    var subitemImage = itemImages_detail[matrixparent[element]];
                                    if (!!subitemImage) {
                                        if (!!subitemImage.urls) {
                                            if(!!subitemImage.urls[1]){
                                            newImgUrl = subitemImage.urls[1].url;
                                            }else{
                                                newImgUrl = subitemImage.urls[0].url;
                                            }
                                        }else{
                                        urlOptionset.forEach(function (child) {
                                            urlSubstring = subitemImage[matrixparent[child]];
                                            if (!!urlSubstring) {
                                                if (!!urlSubstring.urls) {
                                                    if(!!urlSubstring.urls[1]){
                                                    newImgUrl = urlSubstring.urls[1].url;
                                                    }else{
                                                        newImgUrl = urlSubstring.urls[0].url;
                                                    }
                                                }
                                            }
                                        })
                                       }
                                    }
                                });
                                if (newImgUrl === null) {
                                    function cleanString(str) {
                                        return str.replace(/[^ws]/gi, '').replace(/s+/g, '');
                                    }
                                    function navigateToObject(obj, component) {
                                        component = cleanString(component); // Clean the component string
                                        for (var key in obj) {
                                            var cleanedKey = cleanString(key); // Clean the key string
                                            if (cleanedKey.includes(component)) {
                                                return obj[key];
                                            } else if (typeof obj[key] === 'object') {
                                                var result = navigateToObject(obj[key], component);
                                                if (result) {
                                                    return result;
                                                }
                                            }
                                        }
                                        return null;
                                    }
                                    var resultObject = navigateToObject(itemImages_detail, urlComponent);
                                    newImgUrl = resultObject.url;
                                }
                                if(!!newImgUrl){
                                    thumbnail.url = newImgUrl;
                                    this.model.set('_thumbnail', thumbnail);
                                }
                                //Code to get the image of the child item
                                var itemName = matrixparent.custitemmatrixchildwebdisplayname ? matrixparent.custitemmatrixchildwebdisplayname : matrixparent.itemid;
                                this.model.set('storedisplayname2', itemName);
                                this.model.set('_name', matrixparent.custitemmatrixchildwebdisplayname);
                                newUrl = newUrl.replace(/=/g, '%3D');
                                var completeUrl = this.model.get('urlcomponent')+"%3Fquantity%3D1"+newUrl;
                                this.model.set('_url', completeUrl);
                                return {
                                    model: this.model,
                                    thumbnail: this.options.areResults ? thumbnail : {},
                                    currentQuery: _(this.options.query).escape(),
                                    isItemSelected: !!this.model,
                                    hasResults: this.options.areResults,
                                    isAjaxDone: this.options.isAjaxDone
                                };
                            } else {
                                return originalRet;
                            }
                        } catch (e) {
                            console.log('err@ItemsSearcherItemView', e);
                            return originalRet;
                        }
                    })
                });

Leave a comment

Your email address will not be published. Required fields are marked *