Skip to content

Instantly share code, notes, and snippets.

View pelmered's full-sized avatar
🎯
Focusing

Peter Elmered pelmered

🎯
Focusing
View GitHub Profile
@pelmered
pelmered / purchasable-by-category.php
Last active December 22, 2017 18:11
Make WooCommerce products purchasable by category
add_action('wp', 'prefix_theme_setup', 99);
function prefix_theme_setup()
{
add_filter( 'woocommerce_is_purchasable', 'prefix_is_purchasable', 20, 2);
}
function prefix_is_purchasable($purchasable, $product)
@pelmered
pelmered / hide-when-free-avaialble.php
Last active September 4, 2017 14:52
Hide other shipping methods except local delivery when “Free Shipping” is available
/**
* Hide shipping rates except local delivery when free shipping is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
$local_pickup = array();
@pelmered
pelmered / gist:9593f6ca25a84a7ce962
Last active April 2, 2017 21:44
Remove and reset all products and categories in magento
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE `catalog_product_bundle_option`;
TRUNCATE TABLE `catalog_product_bundle_option_value`;
TRUNCATE TABLE `catalog_product_bundle_selection`;
TRUNCATE TABLE `catalog_product_entity_datetime`;
TRUNCATE TABLE `catalog_product_entity_decimal`;
TRUNCATE TABLE `catalog_product_entity_gallery`;
@pelmered
pelmered / wp-config.php
Created February 27, 2017 20:56
Fix issues with symlinked wordpress
<?php
/**
* Fixes issues with symlinked wordpress
*/
if ( defined('ABSPATH') ) {
$fixed_abspath = str_replace( 'wordpress', 'public', ABSPATH );
define('WP_CONTENT_DIR', $fixed_abspath . 'wp-content');
define('WP_PLUGIN_DIR', $fixed_abspath . 'wp-content/plugins');
}
@pelmered
pelmered / letter-post-list.php
Last active February 22, 2017 19:24
Letter post list
<?php
$posts = get_posts([
'post_status' => 'publish',
'posts_per_page' => -1,
'post_type' => 'post'
]);
$post_index = array_fill_keys( array_merge( [ '#' ], range( 'a', 'z' ), ['å', 'ä', 'ö', ] ), [] );
foreach( $posts as $post ) {
$first_letter = strtolower( substr( $post->post_title, 0, 1 ) );
@pelmered
pelmered / taxonomies.php
Last active January 4, 2016 16:49
Create taxonomy with rewrite with Post types creator https://github.com/pelmered/post-types-creator
<?php
if( class_exists( 'PE_Post_Type_Creator' ) )
{
$ptc = new PE_Post_Type_Creator();
$text_domain = 'text-domain';
$ptc->set_taxonomies(array(
'min-taxonomi' => array(
'singular_label' => _x('Min taxonomi', 'Post type plural', $text_domain),
@pelmered
pelmered / functions.php
Last active September 6, 2015 12:23
Add info link based on product category [WooCommerce]
<?php
add_action( 'woocommerce_single_product_summary', 'my_add_read_more_about_category', 25);
function my_add_read_more_about_category()
{
global $product;
$cat_post_map = array(
// category id => array(<page id to link to>, <category plural label (optional)>)
@pelmered
pelmered / wp-db-backup.sh
Last active August 29, 2015 14:26
WP-CLI snippets and scripts
# Only database backup
declare -a SITES=('site1' 'site2' 'site3' 'site4');
SITES_PATH_BASE=/var/www/
SITES_PATH_SUFIX=/repo/public
SITES_BACKUP_FOLDER=backups
CURRENT_DATE=`date +%Y-%m-%d`;
//
// Improved and much cleaner version of https://gist.github.com/andyl/6360906
//
//
// Bootstrap Mid-Small - col-ms-* - the missing grid set for Bootstrap3.
//
// This is a hack to fill the gap between 480 and 760 pixels - a missing range
// in the bootstrap responsive grid structure. Use these classes to style pages
// on cellphones when they transition from portrait to landscape.
//
@pelmered
pelmered / plugin.php
Created June 12, 2015 15:25
Allow template to override plugin templates
function include_template( $template, $args = array() )
{
//Allow plugins to manipulate the tempalte data before sending it to the template
$data = apply_filters('prefix_template_data', $args, $template);
//Look in theme folder first, then plugin folder
if(locate_template($template) === '')
{
include PREFIX_PLUGIN_PATH . 'templates/' . $template;