Skip to content

Instantly share code, notes, and snippets.

@pmgarman
pmgarman / flashsale.php
Created March 15, 2019 16:02
Old code for WC 2.x to set a flash sale price on all products.
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Check if WP_CLI exists, and only extend it if it does
if ( defined( 'WP_CLI' ) && WP_CLI && ! class_exists( 'CP_CLI' ) ) {
/**
* Class CP_CLI
*/
@damiencarbery
damiencarbery / htaccess-root.txt
Last active December 2, 2021 10:36
Hardening and Caching WordPress - A few .htaccess and wp-config.php changes to harden and speed up your website. These are simple changes that can help protect you - https://www.damiencarbery.com/2019/03/hardening-and-caching-wordpress/
# Block WordPress xmlrpc.php requests.
<Files xmlrpc.php>
deny from all
</Files>
# Block direct access to wp-config.php.
<Files wp-config.php>
Deny from all
</Files>
<?php
include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
add_filter( 'woocommerce_admin_reports', 'my_custom_woocommerce_admin_reports', 10, 1 );
function my_custom_woocommerce_admin_reports( $reports ) {
$sales_by_country = array(
'sales_by_country' => array(
'title' => 'Sales By Country',
'description' => '',
'hide_title' => true,
'callback' => 'sales_by_country_callback',
@bekarice
bekarice / user-switching-notice-for-wc.php
Created February 12, 2019 01:40
Uses the "demo store" notice to show a switch back link for WC customer accounts with User Switching: http://cloud.skyver.ge/c50cbcd4c5e7
<?php
/**
* Plugin Name: User Switching Notice for WooCommerce
* Plugin URI: https://gist.github.com/bekarice/7785293fb60d7d5297a245b1c1271272
* Description: Adds a frontend notice to switch back to your user on WooCommerce sites with User Switching.
* Author: SkyVerge
* Author URI: http://www.skyverge.com/
* Version: 1.0.0
* Text Domain: user-switching-notice-for-woocommerce
*
@xlplugins
xlplugins / gist:ce5b3de5645d576fb264c3d1725fb745
Created December 12, 2018 06:05
WooCommerce User Countery Name Shortcode
add_shortcode( 'wc_geo_country_name', 'wcct_custom_get_user_geo_country_name' );
function wcct_custom_get_user_geo_country_name() {
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
return WC()->countries->countries[ $country ];
}
@brandomeniconi
brandomeniconi / cf-wp-cache-worker.js
Last active September 20, 2024 09:22
Basic Cloudflare Worker that allows efficient page caching for Wordpress websites
// Version 1.4
const DAY_IN_SECONDS = 60*60*24;
const BLACKLISTED_URL_PARAMS = [
'fbclid', // Google Ads click Id
'gclid', // Facebook click Id
'msclkid', // Micorosft Ads Click Id
];
addEventListener('fetch', event => {
@jessepearson
jessepearson / jp_filter_edit_shop_order_views.php
Last active December 11, 2022 07:20
WordPress will automatically create a Mine view for post types that have multiple authors. With a recent change to WooCommerce core, the customers are now the author of the orders, so Mine is showing and causing confusion. This filter will remove Mine from orders.
<?php // do not copy this line
/**
* WordPress will automatically create a Mine view for post types that have multiple authors.
* With a recent change to WooCommerce core, the customers are now the author of the orders, so Mine is
* showing and causing confusion. This filter will remove Mine from orders.
*
* @param arr $views The current views being used for orders.
* @return arr The edited views array.
*/
@komal-maru
komal-maru / wc-add-order-status-dashboard-status-widget
Last active March 9, 2023 05:05
Add a new WooCommerce order status into Dashboard status widget
<?php
//Add a WooCommerce order status (completed, refunded) into the Dashboard status widget
function woocommerce_add_order_status_dashboard_widget() {
if ( ! current_user_can( 'edit_shop_orders' ) ) {
return;
}
$refunded_count = 0;
$completed_count = 0;
foreach ( wc_get_order_types( 'order-count' ) as $type ) {
@sonipb
sonipb / ultimate-guide-to-woocommerce-checkout-fields.html
Last active April 1, 2025 22:40
woocommerce checkout page : Ultimate Guide to WooCommerce Checkout Fields
// ref. https://jeroensormani.com/ultimate-guide-to-woocommerce-checkout-fields/
//This post is meant as a one stop shop if you’d like to make any kind of customizations to your WooCommerce checkout fields. Whether this is adding additional fields, removing some unneeded ones or changing the order they’re displayed in.
This post is meant as a one stop shop if you’d like to make any kind of customizations to your WooCommerce checkout fields. Whether this is adding additional fields, removing some unneeded ones or changing the order they’re displayed in.
Additionally there will be guides on how do display fields two field side by side, updates the order totals when a field changes and how to add basic field validation.
This is a post with a lot of code snippets and likely requires changes for it to fit your exact needs. Prefer to use a plugin instead? Take a look at my Advanced Checkout Fields for WooCommerce plugin.
Good to Know
These are some good to know files, hooks and functions/methods. Some of these
@jessepearson
jessepearson / send_wc_admin_new_user_notification.php
Last active November 18, 2019 18:16
Will send the WordPress New User email to the admin if the site when new user/customer is added through WooCommerce
<?php // do not copy this line
/**
* In case you'd like to send an email to the admin when a new customer is created in WooCommerce.
* @param int $user_id New user's ID
*/
function send_wc_admin_new_user_notification( $user_id ) {
wp_send_new_user_notifications( $user_id, 'admin' );
}
add_action( 'woocommerce_new_customer', 'send_wc_admin_new_user_notification' );