Skip to content

Instantly share code, notes, and snippets.

@traviswaelbro
traviswaelbro / split_csv.py
Last active January 3, 2024 14:42
Split a CSV file into multiple CSVs of a certain number of rows.
# This is mostly copied from [this answer on Stack Overflow](https://stackoverflow.com/a/49452109/3330552)
# (Thanks [Ryan Tuck](https://github.com/ryantuck)!)
# except that I've included the dependencies and set this up to be called from the command line.
#
# Example call from bash to split a single CSV into multiple 100 line CSVs:
# python3 split_csv /path/to/my/file.csv /path/to/split_files my_split_file 100
#
# Warning: This doesn't have any validation! This will overwrite existing files if you're not careful.
import csv
@traviswaelbro
traviswaelbro / grouped_products_cost_price.php
Last active June 1, 2016 21:02
Standalone page designed to generate Excel-compatible table with all grouped products' cost and price (per components and total grouped product).
<?php
include_once "app/Mage.php";
umask(0);
Mage::app();
$layout = Mage::getSingleton('core/layout');
//load default xml layout handle and generate blocks
$layout->getUpdate()->load('default');
$layout->generateXml()->generateBlocks();
//get the loaded head and header blocks and output
$headBlock = $layout->getBlock('head');
@traviswaelbro
traviswaelbro / sale_report.py
Created May 13, 2016 19:23
Add a custom field to existing model's Group By... on the Search for Sales Analysis Report in Odoo 8
from openerp import tools
from openerp.osv import fields, osv
class SaleReport(osv.osv):
_inherit = 'sale.report'
_columns = {
'mage_store': fields.many2one('mage.store.view', 'Magento Store', readonly=True),
}
@traviswaelbro
traviswaelbro / Observer.php
Last active December 18, 2015 16:35
Simple Magento that hooks onto the Add to Cart event. Can be used to redirect somewhere, but we use to apply relational logic for our products. Example: If product has attribute "length" over 24", then add an extra packaging fee to the cart. Updated version includes more complex business logic to add packaging fee for every 8 items for specific …
<?php
// File location: /app/code/local/Mbs/Addtocart/Model/Observer.php
class Mbs_Addtocartredirect_Model_Observer {
public function addToCartRedirect($observer) {
$event = $observer->getEvent();
$product = $event->getProduct();
$productId = $product->getId();
// Add hand packing fee if product fits criteria
@traviswaelbro
traviswaelbro / change.js
Created December 18, 2015 04:07
This generic JavaScript function allows the user to easily add a function call inline, without having to do a lot of work. It can use any trigger, such as onchange or onclick, to launch a custom action, like updating its own content or the content of another element. I will update to cover more situations later, as well as creating a jQuery vers…
"use strict";
function change(idToChange, valueToChange, newValue) {
var element = document.getElementById(idToChange);
switch(valueToChange) {
case 'href':
element.href = newValue;
break;
case 'innerHTML':
@traviswaelbro
traviswaelbro / view.phtml
Created December 14, 2015 17:31
Update to Magento /catalog/category/view.phtml template that allows admin to specify a different h1 in the description to override the default "category name" as h1. In our application, this allows us to use SEO-specific phrasing on the category page, while using more user-friendly names for the navigation menu, etc.
<div class="page-title category-title">
<?php if($this->IsRssCatalogEnable() && $this->IsTopCategory()): ?>
<a href="<?php echo $this->getRssLink() ?>" class="link-rss"><?php echo $this->__('Subscribe to RSS Feed') ?></a>
<?php endif; ?>
<?php // Display normal category name as h1 unless there is an h1 set in the category description ?>
<?php if( $_description=$this->getCurrentCategory()->getDescription() ) { ?>
<?php if(strpos( $_helper->categoryAttribute($_category, $_description, 'description'), "h1" ) !== false) { ?>
<?php } else { ?>
@traviswaelbro
traviswaelbro / addtocart.php
Last active December 18, 2015 17:33
Magento file that allows adding to cart from external site's form data.
<html>
<body>
<p>Running</p>
<?php
include_once "app/Mage.php";
umask(0);
Mage::app();
Mage::getSingleton("core/session", array("name" => "frontend"));
echo "Sorry, it looks like something might have gone wrong! You might <a href='javascript:history.back()'>go back</a> and try again.<br />"; // In case a customer ends up here with an error preventing redirect