Skip to content

Instantly share code, notes, and snippets.

Chris Flannagan ChrisFlannagan

Block or report user

Report or block ChrisFlannagan

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Login.php
<?php
class Login {
public function hook(){
add_filter( 'determine_current_user', [ $this, 'login_via_token' ], 20 );
add_action( 'rest_api_init', [ $this, 'register_routes' ], 10 );
}
public function register_routes(){
View swp-rest-post-type-query.php
<?php
add_filter( 'rest_product_query', function( $query_args, $request ) {
if ( isset( $_GET['search'] ) && ! empty( $_GET['search'] ) && class_exists( '\SWP_Query' ) ) {
$query_args['orderby'] = 'post__in';
}
return $query_args;
}, 99, 2 );
View swp-pre-get-posts.php
<?php
add_action( 'pre_get_posts', function( $query ) {
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
return
}
if ( is_admin() || ! isset( $query->query['post_type'] ) || $query->query['post_type'] !== 'product' ) {
return;
}
View estimates.php
<?php
namespace Project\WooCommerce\Gateways;
class Estimates extends \WC_Payment_Gateway {
const ID = 'estimates-gateway';
const WC_STATUS = 'wc-estimate';
public function __construct() {
View add-full-size-gifs.php
<?php
/**
* If inserting a gif always place the full size image
*
* @param string $html
* @param int $send_id
* @param array $attachment
*
* @return string
*/
@ChrisFlannagan
ChrisFlannagan / remove-hidden-products-query.php
Last active Mar 15, 2018
Woo doesn’t filter out products with hidden catalog visibility outside of main shop loop. This is important to remember when using the REST API for front end display or general search results. This code snippet will keep them from showing outside of the admin. Source: whoischris.com
View remove-hidden-products-query.php
<?php
/**
* https://whoischris.com/remove-catalog-visibility-hidden-products-in-woocommerce-from-search-wp-rest-api-results/
*/
add_action( 'pre_get_posts', 'hidden_search_query_fix' );
public function hidden_search_query_fix( $query = false ) {
if ( ! is_admin() && isset( $query->query['post_type'] ) && $query->query['post_type'] === 'product' ) {
$tax_query = $query->get( 'tax_query' );
$tax_query[] = [
View temp-disable-woocommerce-local.php
<?php
unload_textdomain( 'woocommerce' );
WC()->countries->states = null;
WC()->countries->load_country_states();
$formatted_address = WC()->countries->get_formatted_address( $address );
WC()->load_plugin_textdomain();
WC()->countries->load_country_states();
//$formatted_address was now in English!
View cod-add-fee.php
<?php
add_action( 'woocommerce_checkout_create_order', function( $order, $data ) {
// For my example I only want to do this on payments through cash on delivery gateway
// This is not a requirement though, you can do it for any reason you want.
if ( $order->get_payment_method() !== 'cod' ) {
return;
}
// I get my fee from the COD gateway settings field we added
View cod-fields.php
<?php
add_filter( 'woocommerce_settings_api_form_fields_cod', function( $form_fields ) {
$form_fields[ 'custom_fee' ] = [
'title' => __( 'Fee in $', 'textdomain' ),
'type' => 'number',
'description' => __( 'Add a fee to orders using Cash on Delivery gateway.', 'textdomain' ),
'default' => 0,
'min' => 0,
'step' => .01,
@ChrisFlannagan
ChrisFlannagan / geodistance.php
Created Oct 13, 2017
Calculate distance between to coordinates of latitude and longitude using the WP REST API and return posts ordered by distance from user's coordinates
View geodistance.php
<?php
/**
* Heavily borrowed from: http://xplus3.net/2010/08/08/filtering-on-a-non-standard-database-field-with-wordpress/
**/
class CoordinatesTable extends DB {
protected $db_option = "coordinates_db";
You can’t perform that action at this time.