@creedon
WELL it worked for a very short time, and then suddenly for seemingly no reason none of the Add to Cart buttons were links anymore. I'm sorry to rely on you once again, but I'm at my wit's end trying to figure out what's causing the sudden problem 🙃
To the best of my knowledge, I didn't make any meaningful changes to the script between when it worked and when it didn't but I've attached my current code at the bottom in case you can see something I cannot. I've also verified that the sku codes didn't change for the products, so that isn't the issue.
The notable changes that happened in that time are:
Descriptions were added to each product to display on the product pages. Seems unlikely to cause a problem, but it was done by a colleague working on the website at the same time as I was editing the Products Page Header Code Injection and CSS so maybe there was some sort of interference?
Similarly to this thread as well, I removed the Quantity drop-down selection by tagging each product "hidequantity" and adding these lines to the Custom CSS:
//Remove Quantity Dropdown//
article .tag-hidequantity .product-quantity-input{
display: none;
}
Reverting these changes did not fix the issue, so I am at a loss. The only other thing is that I initially mistakenly added the above CSS code to the Products Page Header Code Injection. It's worth noting that I first noticed the issue after removing those lines and placing them in the proper place in the Custom CSS, but I find it hard to imagine that they could cause problems even after they're gone.
Any advice would be greatly appreciated! As mentioned, here is the code being injected into the Products Page Header:
<script>
$( ( ) => {
// replace add to cart button on product item with button that goes to a url
const skuUrlMappings = {
/*
skus can be copied from a product item
if you need more than one mapping then copy and repeat the line below
for each mapping, remove the "// " at the beginning of the line and enter the appropriate data
the url can be any valid url in the context of Squarespace. It can be
a full one like "https://www.squarespace.com/" or a partial url like
"/contact"
*/
// '[enter sku here]' : '[enter url here]',
// last or only item doesn't get a comma at the end
'SQ5417563' : 'https://www.bccannabisstores.com/products/cbd-thc-biomed?_pos=4&_sid=a6188a708&_ss=r', //THC CBD Indica
'SQ8161064' : 'https://www.bccannabisstores.com/products/thc-hybrid-thc-biomed?_pos=2&_sid=a6188a708&_ss=r', //THC Hybrid
'SQ2402320' : 'https://www.bccannabisstores.com/products/thc-indica-thc-biomed?_pos=1&_sid=a6188a708&_ss=r', //THC Indica
'SQ6764880' : 'https://www.bccannabisstores.com/products/thc-sativa-lr-thc-biomed?_pos=3&_sid=a6188a708&_ss=r', //THC Sativa
'SQ9487063' : 'https://www.bccannabisstores.com/products/thc-sativa-lr?_pos=6&_sid=a6188a708&_ss=r', //Preroll Sativa
// 'SQ7454924' : '', //Preroll Indica //NEED LINK
// 'SQ4683342' : '', //Preroll Hybrid //NEED LINK
// 'SQ9602290' : '', //Preroll CBD Indica //NEED LINK
'SQ0334801' : 'https://www.bccannabisstores.com/products/thc-kiss-cannabis-beverage-shot?_pos=5&_sid=a6188a708&_ss=r', //THC Kiss
'SQ1331800' : 'https://www.bccannabisstores.com/search?type=article%2Cpage%2Cproduct&q=THC*+BioMed*' //THC Kiss Gummies //NEED LINK
}
defaultUrl = 'https://www.bccannabisstores.com/search?type=article%2Cpage%2Cproduct&q=THC*+BioMed*';
// do not change anything below, there be the borg here
if ( ! $( '.ProductItem' ).length ) return;
const skus = $.map ( Static.SQUARESPACE_CONTEXT.product.variants,
function ( v ) {
return ( v.sku );
} );
/*
if our skus don't contain a mapped sku then bail, in other words does not
replace add to cart button
*/
if ( ! skus.some ( r => Object.keys ( skuUrlMappings ).indexOf ( r ) >= 0 )
) return;
let $addToCartButton = $( '.sqs-add-to-cart-button' );
let $addToCartButtonClone = $addToCartButton.clone ( )
.click ( function ( ) {
let sku = skus [ 0 ]; // we assume one variant for product item
// we go looking for more than one
let $productVariants = $( '.product-variants' );
if ( $productVariants.length ) {
let selectedVariant = $( '.product-variants' )
.attr ( 'data-selected-variant' );
if ( selectedVariant === undefined ) return;
selectedVariant = JSON.parse ( selectedVariant );
sku = selectedVariant.sku;
}
const url = sku in skuUrlMappings ? skuUrlMappings [ sku ] : defaultUrl;
location = url;
} );
$addToCartButton.replaceWith ( $addToCartButtonClone );
} );
</script>