Freegifts by Secomapp
  • About Free Gifts by Secomapp
  • Adding liquid code to your theme
  • Before you get started
    • Introduction
    • Important terms
  • User Guide
  • How to create Offer on Cart
    • How to reward customer a gift if he Buy 2 items of the same product? (Buy 2X get Y offer)
    • What is the "Use gift(s) in condition of other gift on Cart offer" feature?
  • How to create Offer on Catalog
    • Disable multiple gift feature for Catalog offer
    • Gift item should be the same product customer should buy
    • The "Bundle products" feature
  • Exporting report for your Promotion campaign
  • Advance features
    • Features in Setting page
    • Populating Promotion Codes via a URL
    • Narrow by Customers' orders history
    • How to let customer input property fields before adding gift to cart.
  • FAQs
    • How To Show Promotion info slider?
    • Why I cannot select a product as gift?
    • How to create Goal Offer?
    • How to create Buy X get Y Offer?
  • Manual coding instruction
    • How to Add Liquid Code?
    • Hide Gifts Variants For Themes Which Use Shopify's Ajax Api Functions
Powered by GitBook
On this page

Was this helpful?

  1. Manual coding instruction

Hide Gifts Variants For Themes Which Use Shopify's Ajax Api Functions

Note: If your theme using Shopify's ajax api functions for showing select variants box, you will found codes look like below:

new Shopify.OptionSelectors('product-select', { product: {{ product | json }}, onVariantSelected: selectCallback, enableHistoryState: true });

We need to edit the product's data information first before giv shopify's api showing it by those code below:

window.freegifts_product_json = function(product){ if(product){ if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype.toString.call(arg) === '[object Array]'; }; } if(!String.prototype.endsWith) { Object.defineProperty(String.prototype, 'endsWith', { value: function(searchString, position) { var subjectString = this.toString(); if (position === undefined || position > subjectString.length) { position = subjectString.length; } position -= searchString.length; var lastIndex = subjectString.indexOf(searchString, position); return lastIndex !== -1 && lastIndex === position; } }); }

    if(product.options && Array.isArray(product.options)) {
        for (var i = 0; i < product.options.length; i++) {
            if (typeof product.options[i] !== 'undefined' && typeof product.options[i].values !== 'undefined') {
                var option = product.options[i];
                if(option.values && Array.isArray(option.values))
                    for (var r = 0; r < option.values.length; r++) (option.values[r].endsWith("(Freegifts)") || option.values[r].endsWith("% off)")) && (option.values.splice(r, 1), r--)
            }
        }
    }

    var sca_price = product.price;
    var sca_price_min = product.price_max;
    var sca_price_max = product.price_min;
    var sca_compare_at_price = product.compare_at_price;
    var sca_compare_at_price_min = product.compare_at_price_max;
    var sca_compare_at_price_max = product.compare_at_price_min;

    if(product.variants && Array.isArray(product.variants)) {
        for (var id = 0; id < product.variants.length; id++) {
            var variant = product.variants[id];
            var option_name = variant.option3 ? variant.option3 : variant.option2 ? variant.option2 : variant.option1;
            if (("undefined" != typeof SECOMAPP && "undefined" != typeof SECOMAPP.gifts_list_avai && "undefined" != typeof SECOMAPP.gifts_list_avai[variant.id])
                || option_name.endsWith("(Freegifts)")
                || option_name.endsWith("% off)")) {
                product.variants.splice(id, 1);
                id = id - 1;
            } else {
                if (sca_price_min >= variant.price) {
                    sca_price_min = variant.price;
                    sca_price = variant.price;
                }
                if (sca_price_max <= variant.price) {
                    sca_price_max = variant.price;
                }
                if (variant.compare_at_price) {
                    if (sca_compare_at_price_min >= variant.compare_at_price) {
                        sca_compare_at_price_min = variant.compare_at_price;
                        sca_compare_at_price = variant.compare_at_price;
                    }
                    if (sca_compare_at_price_max <= variant.compare_at_price) {
                        sca_compare_at_price_max = variant.compare_at_price;
                    }
                }
                if (variant.available == true) {
                    product.available = true;
                }
            }
        }
    }
    product.price = sca_price;
    product.price_max = sca_price_max;
    product.price_min = sca_price_min;
    product.compare_at_price = sca_compare_at_price;
    product.compare_at_price_max = sca_compare_at_price_max;
    product.compare_at_price_min = sca_compare_at_price_min;
    if(sca_price_min < sca_price_max){
        product.price_varies = true;
    }else{
        product.price_varies = false;
    }
    if(sca_compare_at_price_min < sca_compare_at_price_max){
        product.compare_at_price_varies = true;
    }else{
        product.compare_at_price_varies = false;
    }
}
return product;
}

var sca_product_info = freegifts_product_json ( {{product | json }} ); new Shopify.OptionSelectors('product-select', { product: sca_product_info, onVariantSelected: selectCallback, enableHistoryState: true });

if(sca_product_info.variants.length===1 && sca_product_info.variants[0].title.indexOf('Default')!==-1 ){ jQuery('.single-option-selector:eq(0)').css('display','none'); }

PreviousHow to Add Liquid Code?

Last updated 3 years ago

Was this helpful?