Skip to content

Instantly share code, notes, and snippets.

@drewgillson
drewgillson / sell-through-report.sql
Created August 22, 2012 05:45
Magento ERP sell-through report
SELECT brand, name, style, color, size, season, initial_qty, current_qty, sell_through, price, avg_price, cost, avg_cost FROM (
SELECT
initial_qty.sm_product_id, brand, name, style, color, size, season, initial_qty.qty AS initial_qty, current_qty.qty AS current_qty, ROUND(IF(initial_qty.qty < current_qty.qty, 0, current_qty.qty / initial_qty.qty), 2) AS sell_through, ROUND(price,2) AS price, ROUND(cost,2) AS cost,
(SELECT ROUND(AVG(price),2) FROM sales_flat_order_item WHERE price != 0 AND sku = initial_qty.sku) AS avg_price,
(SELECT ROUND(AVG((pop_price_ht * (IF(pop_discount > 0, pop_discount, 100) / 100))),2) FROM purchase_order_product WHERE pop_product_id = initial_qty.sm_product_id) AS avg_cost
FROM (
SELECT a.sm_product_id, b.sku, b.manufacturer_value AS brand, b.name, b.vendor_product_id AS style, b.image_label AS color, b.choose_size_value AS size, b.season, SUM(IF(sm_source_stock = 1, -sm_qty, sm_qty)) AS qty, price, cost
FROM stock_movement AS a
INNER JOIN catalog_product_flat_1 AS b O
@drewgillson
drewgillson / Data.php
Created June 28, 2012 05:09
Dg_Pricerulesextended Data.php
class Dg_Pricerulesextended_Helper_Data extends Mage_Core_Helper_Abstract {
private function generatePromoCode($length = null) {
$rndId = crypt(uniqid(rand(),1));
$rndId = strip_tags(stripslashes($rndId));
$rndId = str_replace(array(".", "$"),"",$rndId);
$rndId = strrev(str_replace("/","",$rndId));
if (!is_null($rndId)){
return strtoupper(substr($rndId, 0, $length));
}
@drewgillson
drewgillson / GenerateController.php
Created June 28, 2012 05:07
Dg_Pricerulesextended GenerateController.php
class Dg_Pricerulesextended_GenerateController extends Mage_Core_Controller_Front_Action
{
public function promocodeAction() {
$helper = Mage::helper('pricerulesextended');
$params = $this->getRequest()->getParams();
$email = $params['email'];
$email = explode(',',$email);
$email = $email[0];
@drewgillson
drewgillson / observer.php
Created June 28, 2012 05:06
Dg_Pricerulesextended Observer.php
class Dg_Pricerulesextended_Model_Observer {
public function newsletterSubscriberSave(Varien_Event_Observer $observer) {
$helper = Mage::helper('pricerulesextended');
$subscriber = $observer->getEvent()->getSubscriber();
$email = $subscriber->getEmail();
$promo_value = Mage::getStoreConfig('pricerulesextended/promocode/dollarvalue');
$promo_min = Mage::getStoreConfig('pricerulesextended/promocode/minpurchase');
@drewgillson
drewgillson / config.xml
Created June 28, 2012 05:01
Dg_Pricerulesextended config.xml
<?xml version="1.0"?>
<config>
<modules>
<Dg_Pricerulesextended>
<version>0.1</version><platform>ce</platform>
</Dg_Pricerulesextended>
</modules>
<global>
<models>
@drewgillson
drewgillson / redirectReferer.php
Created June 14, 2012 17:46
Notes on Mage_Core_Controller_Varien_Action::_redirectReferer()
<input type="hidden" name="uenc" value="<?= Mage::helper('core')->urlEncode(Mage::app()->getStore()->getBaseUrl() . 'your_page_url_key') ?>"/>
@drewgillson
drewgillson / Grid.php
Created June 10, 2012 17:47
Do not copy and paste this code - it won't work!
<?php
class Zyn_Common_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {
protected function _prepareColumns() {
$this->addColumn('zyn_featured',
array(
'header'=> 'Show on Homepage',
'width' => '50px',
'index' => 'zyn_featured',
'type' => 'options',
@drewgillson
drewgillson / Grid.php
Created June 10, 2012 17:40
Example - add custom columns to the Magento administration Catalog > Product grid
<?php
class Zyn_Common_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {
protected function _prepareColumns() {
$this->addColumn('zyn_featured',
array(
'header'=> 'Show on Homepage',
'width' => '50px',
'index' => 'zyn_featured',
@drewgillson
drewgillson / CustomerWishlist.sql
Created June 2, 2012 13:52
Magento customer wishlist report
SELECT b.email, c.value AS name, a.updated_at, d.added_at, d.product_id, e.name, SUM(g.qty_ordered) AS purchased
FROM `wishlist` AS a
INNER JOIN customer_entity AS b ON a.customer_id = b.entity_id
INNER JOIN customer_entity_varchar AS c ON a.customer_id = c.entity_id AND c.attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'firstname' AND entity_type_id = b.entity_type_id)
INNER JOIN wishlist_item AS d ON a.wishlist_id = d.wishlist_id
INNER JOIN catalog_product_flat_1 AS e ON d.product_id = e.entity_id
LEFT JOIN sales_flat_order AS f ON f.customer_email = b.email
LEFT JOIN sales_flat_order_item AS g ON (f.entity_id = g.order_id AND g.sku LIKE CONCAT(e.sku,'%') AND g.product_type = 'simple')
GROUP BY b.email, c.value, a.updated_at, d.added_at, d.product_id, e.name
@drewgillson
drewgillson / sproc_exportObjectToCSV.sql
Created March 3, 2012 04:02
An MSSQL stored procedure to export a table or view to CSV, with column headers, using BCP
/****** Object: StoredProcedure [dbo].[sproc_exportObjectToCSV] Script Date: 03/02/2012 21:00:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
GO