Skip to content

Instantly share code, notes, and snippets.

View alexphelps's full-sized avatar
🤔

Alex Phelps alexphelps

🤔
View GitHub Profile
@alexphelps
alexphelps / override-ups-shipping-weight.php
Last active May 27, 2016 01:52
WooCommerce UPS Shipping Default Product Weight
add_filter( 'woocommerce_shipping_init', 'set_default_product_weight', 1 );
function set_default_product_weight( ) {
if ( ! is_admin() ) {
foreach ( WC()->cart->get_cart() as $item_id => $values ) {
$_product = $values['data'];
if ( empty( $_product->weight ) ) {
$_product->weight = 1.00;
}
}
}
@alexphelps
alexphelps / register-tax.php
Last active June 7, 2016 14:33
Register Taxonomy
function register_teams_taxonomy() {
$labels = array(
'name' => __( 'Teams', 'taxonomy general name' ),
'singular_name' => __( 'Team', 'taxonomy singular name' ),
'search_items' => __( 'Search Teams' ),
'all_items' => __( 'All Teams' ),
'parent_item' => __( 'Parent Team' ),
'parent_item_colon' => __( 'Parent Team:' ),
'edit_item' => __( 'Edit Team' ),
'update_item' => __( 'Update Team' ),
@alexphelps
alexphelps / wc-products-filter-custom-tax.php
Last active March 17, 2024 11:55
WooCommerce Products Filter Custom Taxonomy
/**
* Filter team products from main shop only on frontend on shop, category, and tag archives
*/
function exclude_team_products( $query ) {
$archive_query = $query->is_post_type_archive('product') && $query->is_main_query();
$cat_tag_query = $query->is_tax( array('product_cat', 'product_tag') ) && $query->is_main_query();
if ( !is_admin() && $archive_query || !is_admin() && $cat_tag_query ) {
$taxquery = array(
array(
'taxonomy' => 'product_team',
@alexphelps
alexphelps / nginx-uwsgi-python3
Created February 14, 2017 23:01 — forked from simoncoulton/nginx-uwsgi-python3
Setting up Nginx, uWSGI & Python3
======================================
Setting up Nginx, uWSGI and Python3
======================================
First off, I'm traditionally a PHP developer, but am looking at moving across to Python. I really struggled to find decent documentation on how to get a server up and running for deploying Python web applications from the point of view of someone coming from PHP. The main problems I came across with documentation were:
1) Only showed you how to run the server for a single web application.
2) Only showed you how to configure the app, not the server it was running on.
My preferred workflow for development is by setting up a new VM in VMware Fusion and then forwarding through all requests to that VM via /etc/hosts. This might not be the optimal way to get things up and running, but it works for me.
@alexphelps
alexphelps / gist:ac3db1c533285f98be0bc111a7f1b5ed
Last active May 26, 2017 15:51
Django Email with Premailer
from premailer import Premailer
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.dispatch import receiver, Signal
from django.shortcuts import get_object_or_404
from django.template import loader
from django.template.loader import get_template
from django.contrib.auth.models import User
@alexphelps
alexphelps / example_table.py
Created June 12, 2017 19:10
Example Table Json
def get(self, request, company_id, **kwargs):
company_id = self.kwargs['company_id']
company = get_object_or_404(Company, pk=company_id)
company_timezone = timezone.activate(company.timezone)
range_from = timezone.now() + timedelta(days=-30)
if 'from' in request.GET:
range_from = datetime.strptime(
request.GET['from'],
'%m-%d-%Y'
)
@alexphelps
alexphelps / premailer_django.py
Last active June 13, 2017 19:05
Premailer Example
import json
from premailer import Premailer
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.dispatch import receiver, Signal
from django.template import loader
@receiver(welcome_notification)
@alexphelps
alexphelps / accounts_table.py
Created June 23, 2017 17:26
Accounts Table Example
accounts = Account.objects.annotate(client_count=Count('client'), users_count=Count('users'))
account_data = []
for account in accounts:
account_data.append(
{
'id': account.id,
'name': account.name,
'clients': account.client_count,
'users': account.users_count
}
@alexphelps
alexphelps / ecs-compose.yml
Last active September 1, 2017 13:12
ECS Compose
---
version: '2'
services:
nginx:
image: "#####.dkr.ecr.us-east-1.amazonaws.com/nginx:latest"
cpu_shares: 50
mem_limit: 104857600
logging:
driver: awslogs
options:
@alexphelps
alexphelps / event_save.py
Last active September 8, 2017 18:27
Event save
class ModelDiffMixin(object):
"""
Ref: https://stackoverflow.com/questions/1355150/django-when-saving-how-can-you-check-if-a-field-has-changed
"""
def __init__(self, *args, **kwargs):
super(ModelDiffMixin, self).__init__(*args, **kwargs)
self.__initial = self._dict
@property
def _dict(self):