Skip to content

Instantly share code, notes, and snippets.

View ihorduchenko's full-sized avatar
💭
Working

Ihor Duchenko ihorduchenko

💭
Working
View GitHub Profile
@ihorduchenko
ihorduchenko / shopify-currency-switcher-no-apps.liquid
Last active July 14, 2020 16:48
Shopify built in currency switcher
{%- form 'localization', class: 'selectors-form' -%}
<select id="currency-list">
{% for currency in form.available_currencies %}
<option{% if currency.iso_code == form.current_currency.iso_code %} selected="true"{% endif %}
data-name="{{ currency.name }}" value="{{ currency.iso_code }}">
{%- if currency.symbol -%}{{ currency.symbol }}{%- endif -%} {{ currency.iso_code }}
</option>
{%- endfor -%}
</select>
<input id="currentCurrencyInp" type="hidden" name="currency_code" value="{{ form.current_currency.iso_code }}">
@ihorduchenko
ihorduchenko / rtwp_filter_wordcount_acf.php
Last active October 22, 2021 22:08
Hooking into WP Reading time plugin - Advanced custom fields (ACF) + Flexible content (rtwp_filter_wordcount filter)
function up_the_count( $count ) {
global $post;
$id = $post->ID;
$post_type = get_post_type($id);
switch ($post_type) {
case 'post':
if ( have_rows( 'content', $id ) ):
while ( have_rows( 'content', $id ) ) : the_row();
if ( get_row_layout() == 'paragraph_section' ):
$count += count( preg_split( '/\s+/', get_sub_field( 'paragraph' ) ) );
@ihorduchenko
ihorduchenko / geolocation.js
Created July 14, 2020 21:16
Get country code using geolocation service
$.getJSON(
'https://geolocation-db.com/json/0f761a30-fe14-11e9-b59f-e53803842572',
function(data) {
console.log(data);
if ( data.country_code === "US" || data.country_name === "United States" ) {
console.log('Is USA');
}
}
);
@ihorduchenko
ihorduchenko / swiper.js
Last active December 11, 2023 17:54
Get instance of Swiper from DOM
// This example shows us how to update broken swiper after Fancybox carousel closing
if (typeof fancyboxInit !== undefined) {
// Main approach here is the next row
$.fancybox.defaults.backFocus = false;
let showCaseSlider = $('.showCaseSlider .swiper-container');
$('[data-fancybox]').fancybox({
afterClose: function() {
if (showCaseSlider) {
let showCaseSliderInstance = showCaseSlider[0].swiper;
showCaseSliderInstance.update();
@ihorduchenko
ihorduchenko / defer-scripts-dependent.js
Created February 17, 2021 11:14
Execute scripts, dependent on jQuery, when jQuery load
function loadAllScripts() {
// Put all your scripts here
};
const loadScriptsInterval = setInterval(function() {
if (window.jQuery && window.Swiper) {
// Here we clear interval and fire scripts execution
clearInterval(loadScriptsInterval);
loadAllScripts();
}
@ihorduchenko
ihorduchenko / critical-css.html
Created April 10, 2021 17:11
Critical CSS for deferred styles while page is loading
<style id="critical-css">
/* Your styles here */
</style>
<script>
const criticalStyleTag = document.getElementById('critical-css');
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function(){
criticalStyleTag.parentNode.removeChild(criticalStyleTag);
}, 2000);
});
@ihorduchenko
ihorduchenko / defer-jquery.js
Created May 12, 2021 08:08
Execute scripts after jQuery loaded
(function($) {
$(document).ready(function() {
// Do stuff now
})
})(window.jQuery);
@ihorduchenko
ihorduchenko / payment-icons.liquid
Created May 18, 2021 07:40
Insert available payment methods icons
{%- unless shop.enabled_payment_types == empty -%}
<div class="footer-group">
<p class="footer__title">
Payment methods
</p>
<ul class="inline-list payment-icons">
{%- for type in shop.enabled_payment_types -%}
<li class="icon--payment">
{{ type | payment_type_svg_tag }}
</li>
@ihorduchenko
ihorduchenko / IntersectionObserver.js
Created July 27, 2021 21:52
Using IntersectionObserver to detect whether element is in viewport or not
const target = document.querySelector('.stickyATCtrigger');
function handleIntersection(entries) {
entries.map((entry) => {
if (entry.isIntersecting) {
// element is in viewport
$('body').removeClass('sticky-addtocart-mob');
} else {
// element is out of viewport
$('body').addClass('sticky-addtocart-mob');
}
@ihorduchenko
ihorduchenko / mutation-observer.js
Created August 26, 2021 12:41
Use mutation observer to detect dynamic element creation
var target = document.querySelector('#bc-sf-filter-products');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
// Do some stuff
productsGrid();
}
});
});
var config = { attributes: true, childList: true, characterData: true }