Skip to content

Instantly share code, notes, and snippets.

@NickGreen
NickGreen / subscription_updated_log.php
Created May 16, 2019 22:37
Add a new entry in the "wcs-subscription-payment" log (in WooCommerce>Status>Logs page) when any order related to a subscription is paid.
<?php
function aer_wc_subscription_updated( $subscription ) {
$logger = wc_get_logger();
$logger->add( 'wcs-subscription-payment', "New payment for subscription #", $subscription->get_id() );
}
add_action('woocommerce_subscription_payment_complete', 'aer_wc_subscription_updated', 10, 1);
@NickGreen
NickGreen / cancel-all-subs.php
Last active May 17, 2019 15:37
Cancel all of your subs
<?php
/**
* Plugin Name: Cancel all subscriptions.
* Description: Cancel all subscriptions.
* Version: 1.0
*/
// Prevent direct access to this file
if ( ! defined( 'ABSPATH' ) ) {
die( "You can't do anything by accessing this file directly." );
}
@NickGreen
NickGreen / change_strings.php
Created August 2, 2019 17:04
Change options strings for subscription products (works for All Products sub options)
<?php
function change_text( $subscription_string, $subscription_details ) {
if ($subscription_string == '<span class="subscription-details">every 2 weeks</span>') {
$subscription_string = '<span class="subscription-details">Fortnightly</span>';
}
if ($subscription_string == '<span class="subscription-details">every month</span>') {
$subscription_string = '<span class="subscription-details">Monthly</span>';
}
return $subscription_string;
@NickGreen
NickGreen / dont_autocomplete.php
Last active August 8, 2019 21:53
don't autocomplete any orders
<?php
add_filter( 'woocommerce_payment_complete_order_status', 'my_function', 100, 3 );
function my_function( $new_order_status, $order_id, $order = null ) {
if ( $new_order_status == 'completed' ) {
$new_order_status = 'processing';
}
@NickGreen
NickGreen / filter_cart_weight.php
Created August 21, 2019 21:51
filter cart weight
<?php
add_filter('woocommerce_cart_contents_weight', 'round_cart_contents_weight', 100, 1);
function round_cart_contents_weight($weight) {
return 100;
// return ceil($weight);
}
@NickGreen
NickGreen / turn_off_sub_order_emails.php
Last active November 13, 2019 22:28
Don't send customer the default order emails if purchasing a subscription product.
<?php
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'wcs_disable_customer_order_email_if_sub', 10, 2 );
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'wcs_disable_customer_order_email_if_sub', 10, 2 );
// If order contains subscription we change the email recipient to ""
function wcs_disable_customer_order_email_if_sub( $recipient, $order ) {
$page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
if ( 'wc-settings' === $page ) {
return $recipient;
@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
@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 / 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 / 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;
}