Skip to content

Instantly share code, notes, and snippets.

@NickGreen
NickGreen / if_sub_in_order.php
Created August 6, 2018 20:58
Do something if order has subscription product in it
<?php
$order = wc_get_order( $order_id );
$sub_in_order = false;
$items = $order->get_items();
foreach ( $items as $item ) {
@NickGreen
NickGreen / remove_basic_membership_on_purchase.php
Last active April 23, 2019 20:43
Remove basic membership whenever any other membership plan is purchased
<?php
function remove_basic_membership( $plan, $args ) {
// Check that we're purchasing anything other than the basic plan (change number to basic plan id)
if ( 123 !== $plan->id ) {
$memberships = wc_memberships_get_user_memberships( $args['user_id'] );
foreach ( $memberships as $membership ) {
// Check to see if this member has the basic plan
// then delete it if so
@NickGreen
NickGreen / disable_gifting_by_product.php
Last active November 22, 2021 02:34
WooCommerce Subscriptions Gifting - turn off for specific products or tags
<?php
add_filter( 'wcsg_is_giftable_product', 'disable_gifting_by_product', 12, 2 );
function disable_gifting_by_product( $is_sub, $product ) {
$not_giftable_products = array( 906, 964 ); // add or remove product IDs here
if ( in_array( $product->get_id(), $not_giftable_products ) ) {
return false;
} else {
return $is_sub;
}
}
@NickGreen
NickGreen / opc_disable_messages.php
Last active September 17, 2018 19:58
Disable One Page Checkout messages
<?php
add_action( 'wp_head', 'opc_disable_messages' );
function opc_disable_messages(){
remove_action( 'wcopc_product_selection_fields_before', array( 'PP_One_Page_Checkout', 'opc_messages' ));
}
<?php
add_filter( 'woocommerce_email_recipient_expired_subscription', 'wcs_change_recipient_for_expired_sub_notification', 10, 2 );
function wcs_change_recipient_for_expired_sub_notification( $recipient, $order ) {
$page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
if ( 'wc-settings' === $page ) {
return $recipient;
}
$recipient = $order->billing_email;
return $recipient;
@NickGreen
NickGreen / sort_subscriptions.php
Created October 23, 2018 00:02
Sort subscriptions on 'my subscriptions' page by ascending or descending order
<?php
add_filter('wcs_get_users_subscriptions','sort_subscriptions', 10, 2);
function sort_subscriptions( $subscriptions, $user_id ){
// choose either ksort or krsort
// in ascending order by ID
// ksort($subscriptions);
// in descending order by ID
@NickGreen
NickGreen / allow_synced_product_early_renewal.php
Created October 30, 2018 21:54
Allow early renewal for synchronized products
<?php
function allow_synced_product_early_renewal( false, $subscription ) {
return true;
}
add_filter( 'wcs_allow_synced_product_early_renewal', 'allow_synced_product_early_renewal' );
@NickGreen
NickGreen / dont-display-recurring-totals.php
Created November 9, 2018 05:04
Don't display the recurring totals in cart or checkout
<?php
/*
Plugin Name: WooCommerce Subscriptions - Remove Recurring Totals
Plugin URI:
Description: Don't display the recurring totals in cart or checkout
Version: 1.0
Author: Support
Author URI:
*/
@NickGreen
NickGreen / satt_shop_button.php
Last active January 1, 2019 21:49
Change behavior of shop 'add to cart' button for SATT products
<?php
// Changes the shop button text when a product has subscription options.
add_filter( 'wcsatt_add_to_cart_text', 'change_button_text', 12, 2 );
function change_button_text( $button_text, $product ) {
return 'add to cart';
}
// Enables shop AJAX even if product has subscription options.
add_filter( 'wcsatt_product_supports_ajax_add_to_cart', 'enable_ajax_add_to_cart', 12, 2 );
@NickGreen
NickGreen / pending-cancel.php
Created January 2, 2019 19:09
Run code when sub status changes to pending cancel
<?php
add_action('woocommerce_subscription_status_updated', 'test_func', 100, 3);
function test_func($subscription, $new_status, $old_status) {
if( 'pending-cancel' == $new_status ) {
error_log( 'In ' . __FUNCTION__ . '(), status changed to = ' . var_export( $new_status, true ) );
}
}