Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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 / 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 / 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 / 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 / deactivate_paypal_unless_subscription.php
Last active May 15, 2019 00:14
Deactivate PayPal standard for non-subscription purchases
<?php
function deactivate_paypal_unless_subscription( $available_gateways ) {
global $wp;
if ( class_exists( 'WC_Subscriptions_Cart' ) ) {
// version 1.5 and 2.0 compatibility
$cart_contains_renewal = function_exists( 'wcs_cart_contains_renewal' ) ? wcs_cart_contains_renewal() : WC_Subscriptions_Cart::cart_contains_subscription_renewal();
@NickGreen
NickGreen / pre-select.php
Last active November 22, 2021 02:25
Pre-select drop-down item by value
<?php
function preselect_option() {
// URL should be in the format https://xxxxxxx/?option=VALUE_OF_OPTION
$option = htmlspecialchars($_GET["option"]);
echo '<script>';
echo 'function setSelectedIndex(s, valsearch) {';
echo ' // Loop through all the items in drop down list';
echo ' for (i = 0; i< s.options.length; i++) { ';