Skip to content

Instantly share code, notes, and snippets.

View ChrisFlannagan's full-sized avatar

Chris Flannagan ChrisFlannagan

View GitHub Profile
<?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(){
<?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 );
<?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;
}
<?php
namespace Project\WooCommerce\Gateways;
class Estimates extends \WC_Payment_Gateway {
const ID = 'estimates-gateway';
const WC_STATUS = 'wc-estimate';
public function __construct() {
<?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 October 9, 2022 12:12
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
<?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[] = [
<?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!
<?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
<?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 October 13, 2017 16:52
Calculate distance between to coordinates of latitude and longitude using the WP REST API and return posts ordered by distance from user's coordinates
<?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";