Skip to content

Instantly share code, notes, and snippets.

@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++) { ';
@NickGreen
NickGreen / auto_apply_coupon.php
Last active April 25, 2019 17:50
Auto-apply coupon to cart at least $25
<?php
// apply free shipping coupon automatically when the cart total is >=$25
add_action('woocommerce_before_cart_table', 'freeship_when_total_at_least_25');
function freeship_when_total_at_least_25() {
global $woocommerce;
$cart_total = WC()->cart->get_cart_contents_total();
if( $cart_total >= 25 ) {
$coupon_code = 'freeshipping'; // this requires the coupon of this name to already exist
if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
@NickGreen
NickGreen / subscriptions_activated_for_order_test.php
Last active November 22, 2021 02:26
subscriptions_activated_for_order early renewal test
<?php
add_action( 'subscriptions_activated_for_order', 'test_function' );
function test_function( $order_id ) {
$subscription = wcs_get_subscriptions_for_order( $order_id, array( 'order_type' => array( 'any' ) ));
error_log( 'In ' . __FUNCTION__ . '(), $subscription = ' . var_export( $subscription, true ) );
}