Skip to content

Instantly share code, notes, and snippets.

@AndresInSpace
AndresInSpace / jquery.waituntilexists.js
Created December 18, 2016 01:06 — forked from PizzaBrandon/jquery.waituntilexists.js
Updated waitUntilExists plugin
;(function ($, window) {
var intervals = {};
var removeListener = function(selector) {
if (intervals[selector]) {
window.clearInterval(intervals[selector]);
intervals[selector] = null;
}
@AndresInSpace
AndresInSpace / fixGetCrossSellProductCollection
Last active September 26, 2017 14:09
Fix getCrossSellProductCollection() to stop returning ENTIRE product catalog collection in object data..
<?php $plcsCol = Mage::getModel('catalog/product_link')->getCollection()->addFieldToFilter('link_type_id',Mage_Catalog_Model_Product_Link::LINK_TYPE_CROSSSELL)->addFieldToFilter('product_id',$_product->getId())->getData();//get crosssell product IDs attached to parent product ?>
<?php $csIDs = array_column($plcsCol,'linked_product_id');//map cs IDs to array, use array to filter collection by IDs ?>
<?php $_crossSellProducts = $_product->getCrossSellProductCollection()->addAttributeToFilter('entity_id', array('in'=>$csIDs))->addAttributeToSelect('*'); //ensures our crosssell collection is limited to ONLY attached crossells of parent.. calling getCrossSellProductCollection without filtering seems to return the ENTIRE product catalog collection..
//replace addAttributeToSelect('*') with whatever fields you need from product flat catalog..
@AndresInSpace
AndresInSpace / magento-1.x-cheatsheet
Last active May 29, 2018 16:12
Magento 1.x Cheatsheet
//Get Layout Handles Used
var_dump(Mage::app()->getLayout()->getUpdate()->getHandles());
//Get loaded layout blocks
var_dump(array_keys(Mage::app()->getLayout()->getAllBlocks()));
//Add to Global Messages
Mage::getSingleton('core/session')->addSuccess('Success Message');
Mage::getSingleton('core/session')->addError('Error Message');
Mage::getSingleton('core/session')->addWarning('Warning Message');
@AndresInSpace
AndresInSpace / Varien.php
Created August 20, 2018 17:41
FIX Magento 1.9x Initial Session Creation (CORE BUG)
FILE PATH: app/code/core/Mage/Core/Model/Session/Abstract/Varien.php
// session cookie params
$cookieParams = array(
'lifetime' => $cookie->getLifetime(),
'path' => $cookie->getPath(),
- 'domain' => $cookie->getConfigDomain(),
+ 'domain' => $cookie->getDomain(),
'secure' => $cookie->isSecure(),
'httponly' => $cookie->getHttponly()
);
default
catalog_category_default (* also used in directory.xml)
catalog_category_layered (* also used in directory.xml)
catalog_product_compare_index
catalog_product_gallery
catalog_product_send
catalog_product_view (* also used in customeralert.xml, tag.xml)
catalog_seo_searchterm_popular
catalog_seo_sitemap_category
catalog_seo_sitemap_product
@AndresInSpace
AndresInSpace / MagentoFastCategoryProductCollection
Last active October 4, 2018 18:51
Get Magento Product Collection filtered by category and ordered by product position from that category without loading category model and subsequent overhead
<?php
public function getProductCollection(){
if($_category = Mage::registry('current_category')){
$_categoryId = $_category->getId();
$_productCollection = Mage::getResourceModel('catalog/product_collection');
$_productCollection->joinTable(array('cp'=>'catalog/category_product'), 'product_id=entity_id', array('*') ,'cp.category_id='.$_categoryId, 'inner'); //join the table we need, filtered on Category ID
$_productCollection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) //attributes needed from products using default config, or select your own
->addAttributeToFilter('status', 1) //ensure its enabled
@AndresInSpace
AndresInSpace / qty-input-counter
Last active February 15, 2019 15:53
Qty Input Addition/Subtraction for IE10+ (w/ JQuery for delegation.. feel free to change to vanilla JS) https://codepen.io/AndresInSpace/pen/xMyeVO
var editQty = (function(){
function editValue(e) {
var input = e.target.nextElementSibling || e.target.previousElementSibling;
input.value = counter(e.target.classList.contains('plus'),input.value);
}
function counter(add,x) {
return add ? ++x : (x > 1 ? --x : x);
}
@AndresInSpace
AndresInSpace / magento.bestsellers.catalog.category.collection.php
Created April 3, 2019 14:33
Magento 1 Bestsellers from Order Data (Not aggregated data)
/* Best Sellers Category */
$today = time();
$from = date('Y-m-d', strtotime("-12 months",$today));//pull best sellers from 12 months prior order data
$to = date("Y-m-d", $today);
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$orderTableAliasName = $readConnection->quoteIdentifier('order');
@AndresInSpace
AndresInSpace / gist:3a7c4ca83abd043ec68b07cdbca0af75
Last active April 27, 2019 02:36
Magento 1 Add Product To Cart w Custom Options [CartController or Observer Methods]
$custom_option1 = array('label'=>'Custom1','value'=>$value1);
$custom_option2 = array('label'=>'Custom2','value'=>$value2);
$product->addCustomOption('additional_options', serialize(array($custom_option1,$custom_option2)));
$product->setHasOptions(true);
$cart->addProduct($product, $params);
$cart->save();
//USE 'additional_options' if you need the CustomOption to show up on checkout/cart, as $item->getCustomOptions() only looks at 'additional_options' and 'option_ids'
$item->getOptionByCode('option_ids');
@AndresInSpace
AndresInSpace / jquery-bs-prototype-fix
Last active April 30, 2019 13:30
Fix Prototype JS Library conflicts with Bootstrap JS (with BS dependent on jQuery)
//ADD THIS TO END OF YOUR JQUERY FILE OR RUN IT AFTER JQUERY IS LOADED
// Prototype.js loads -> then jquery.js loads -> then run this
//alternatively, upgrade and load jquery 3 first then prototype.js and you shouldn't need this fix.
if(Prototype.BrowserFeatures.ElementExtensions){var disablePrototypeJS=function(o,e){var t=function(e){e.target[o]=void 0,setTimeout(function(){delete e.target[o]},0)};e.each(function(e){jQuery(window).on(o+".bs."+e,t)})},pluginsToDisable=["collapse","dropdown","modal","tooltip","popover"];disablePrototypeJS("show",pluginsToDisable),disablePrototypeJS("hide",pluginsToDisable)}