Skip to content

Instantly share code, notes, and snippets.

@NickGreen
NickGreen / username_email.php
Last active February 27, 2020 00:09
Create username from email, just using the part before the "@"
<?php
add_filter( 'woocommerce_new_customer_data', 'customer_username_based_on_email', 20, 1 );
function customer_username_based_on_email( $new_customer_data ){
// get the first billing name
if(isset($_POST['billing_email'])) $billing_email = $_POST['billing_email'];
if( ! empty($billing_email) && ! in_array( $_POST['billing_email'] ) ){
// Replacing 'user_login' in the user data array, before data is inserted
@NickGreen
NickGreen / add_recipient_sub_expired_email.php
Last active November 22, 2021 02:19
Send to customer and 'CC' the admin on sub expired emails
<?php
/**
* Plugin Name: Sub Expired Email Send to Customer
* Plugin URI:
* Description:
* Version: 1.0
* Author:
* Author URI:
**/
@NickGreen
NickGreen / ipn.php
Created February 15, 2020 23:01
Print all IPN data to PP standard logs
<?php
add_action( 'valid-paypal-standard-ipn-request', 'test_handle_paypal_ipn_and_record_response', - 1 );
function test_handle_paypal_ipn_and_record_response( $posted ) {
WC_Gateway_Paypal::log( 'Data collected from IPN' . wc_print_r( $posted, true ) );
}
@NickGreen
NickGreen / points-db-export.php
Created January 18, 2020 02:40
export Points and Rewards custom table to CSV
<?php
//DB details
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = 'root';
$dbName = 'local';
$dbPrefix = 'wp';
//Create connection and select DB
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
@NickGreen
NickGreen / remove_cancel_gifted_subs.php
Created January 6, 2020 21:49
Remove 'cancel' button for gifted subscriptions only.
<?php
function remove_cancel_gifted_subs( $actions, $subscription ) {
if ( WCS_Gifting::is_gifted_subscription( $subscription ) ) {
foreach ( $actions as $action_key => $action ) {
switch ( $action_key ) {
case 'cancel': // Hide "Cancel" button on subscriptions that are "active" or "on-hold"?
unset( $actions[ $action_key ] );
@NickGreen
NickGreen / prevent_item_removal.php
Last active January 9, 2020 22:28
Prevent items from being removed from subscription
<?php // don't copy this line
add_filter( 'wcs_can_items_be_removed', '__return_false' );
@NickGreen
NickGreen / edit_all_orders.php
Last active December 11, 2019 22:58
Allow all orders to be edited
<?php
add_filter( 'wc_order_is_editable', 'wc_make_all_orders_editable', 12, 2 );
function wc_make_all_orders_editable( $is_editable, $order ) {
return true;
}
@NickGreen
NickGreen / exclude_recurring_from_coupons.php
Last active December 10, 2019 23:53
Remove any product coupons from products in the cart, that have a recurring scheme applied to it from All Products for WooCommerce Subscriptions
<?php
// Set the product discount amount to zero if it has a recurring scheme
add_filter( 'woocommerce_coupon_get_discount_amount', 'zero_discount_for_excluded_products', 12, 5 );
function zero_discount_for_excluded_products( $discount, $discounting_amount, $cart_item, $single, $coupon ){
if ( false != ( $cart_item[ 'wcsatt_data' ][ 'active_subscription_scheme' ] ) )
$discount = 0;
@NickGreen
NickGreen / allow_same_product_switch.php
Last active December 28, 2019 11:01
Allow switching to identical product
<?php
/**
* Plugin Name: WooCommerce Subscriptions - Allow Same Product Switch
* Plugin URI:
* Description: Allow switching to same product for WooCommerce Subscriptions
* Version: 1.0
* Author: Nick Green
* Author URI:
**/
@NickGreen
NickGreen / max_shipping.php
Created November 18, 2019 23:56
Set a sitewide max shipping amount
<?php
/**
* Function to set a max cap on the shipping rates.
*/
function my_max_limit_shipping_rates_function( $rates, $package ) {
$methods = WC()->shipping()->get_shipping_methods();
$shipping_limit = 15; // The maximum amount would be $15
// Loop through all rates
foreach ( $rates as $rate_id => $rate ) {
// Check if the rate is higher then a certain amount