Skip to content

Instantly share code, notes, and snippets.

View jrick1229's full-sized avatar
☠️

Joey Ricketts jrick1229

☠️
  • VA
  • 00:11 (UTC -04:00)
View GitHub Profile
@jrick1229
jrick1229 / custom_a2c.php
Last active September 5, 2020 16:51
Custom add to cart redirect
<?php
function custom_a2c( $url ) {
$url = get_permalink( 121 ); // URL to redirect to (121 is the page ID here)
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'custom_a2c' );
<?php
add_filter('woocommerce_subscription_period_interval_strings', 'add_extra_period_intervals', 10, 1);
function add_extra_period_intervals($intevals){
$max_intervals = 20; // The maximum number of intervals you want to display
$intervals = array( 1 => _x( 'every', 'period interval (eg "$10 _every_ 2 weeks")', 'woocommerce-subscriptions' ) );
foreach ( range( 2, $max_intervals ) as $i ) {
$intervals[ $i ] = sprintf( _x( 'every %s', 'period interval with ordinal number (e.g. "every 2nd"', 'woocommerce-subscriptions' ), WC_Subscriptions::append_numeral_suffix( $i ) );
}
@jrick1229
jrick1229 / changemenuitem.php
Created December 11, 2018 21:21
Change the 'Subscriptions' text in the My Account area -- Copied from: https://gist.github.com/jorgeatorres/f2577267a28041da15f4dd2584f91aea
<?php
add_filter( 'woocommerce_account_menu_items', function( $items ) {
if ( isset( $items['subscriptions'] ) ) {
$items['subscriptions'] = 'My new menu item label';
}
return $items;
}, 11 );
@jrick1229
jrick1229 / needs_approval_status.php
Last active October 20, 2020 08:11
Create "Needs Approval" status and apply to orders that contain specified product ID
<?php
/*
*
* Register "Needs Approval" status
*
*/
add_filter( 'woocommerce_register_shop_order_post_statuses', 'register_needs_approval_status' );
@jrick1229
jrick1229 / new_role_based_on_product_IDs.php
Last active February 15, 2019 15:58
Assign a new specified role based on the products that are purchased in the order.
<?php
function adding_new_role_for_product_purchase() {
//add the test-1 customer role
add_role(
'test-1',
"TEST 1 ROLE",
array(
'read' => true,
@jrick1229
jrick1229 / check_num_of_subscriptions.php
Created February 19, 2019 17:51
Don't allow subscription products to be added to the cart if user already has an active subscription.
<?php
add_filter( 'woocommerce_add_to_cart_validation', 'check_num_of_subscriptions', 10, 2 );
function check_num_of_subscriptions( $valid, $product_id ) {
$product_to_add = wc_get_product( $product_id );
if ( $product_to_add instanceof WC_Product_Subscription || $product_to_add instanceof WC_Product_Variable_Subscription ) {
// alternative use: $has_sub = wcs_user_has_subscription( '', '', 'active' );
@jrick1229
jrick1229 / notice__before_payment_methods.php
Last active September 15, 2019 21:53
Create a notice on the Payment Methods section of the My Account page. Works best for instructing customers that changing the 'Default method' will NOT change any active subscriptions without also checking the checkbox.
<?php
function notice__before_payment_methods( $has_methods ) {
wc_print_notice( __( 'In order to effectively change the payment method on ALL of your subscriptions, you must check the checkbox on the next page after clicking \'Add payment method\'.<br><br>You can also change the payment method on ONE subscription by visiting the individual subscription in the \'Subscriptions\' tab. More info can be found <a href="https://docs.woocommerce.com/document/subscriptions/customers-view/#section-11">here</a>.', 'woocommerce' ), 'notice' );
};
add_action( 'woocommerce_before_account_payment_methods', 'notice__before_payment_methods', 10, 1 );
@jrick1229
jrick1229 / wcsdp_get_available_payment_gateways.php
Last active February 21, 2019 21:12
Hide PayPal as an option during checkout for all products, but allow pre-existing subscriptions that use PayPal to continue to renew properly. Modified from: https://github.com/Prospress/woocommerce-subscriptions-disable-paypal/blob/master/woocommerce-disable-paypal-for-subscriptions.php
<?php
function wcsdp_get_available_payment_gateways( $available_gateways ) {
global $wp; // required for - if ( class_exists( 'WC_Subscriptions_Cart' ) ) {
global $woocommerce; // required for - if ( $woocommerce->cart->cart_contents_count != 0 ) {
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();
if ( is_checkout_pay_page() ) {
$order_contains_renewal = function_exists( 'wcs_order_contains_subscription' ) ? wcs_order_contains_subscription( $wp->query_vars['order-pay'] ) : WC_Subscriptions_Order::order_contains_subscription( $wp->query_vars['order-pay'] );
@jrick1229
jrick1229 / notice__before_after_subscriptions_list.php
Created February 27, 2019 18:11
Add a notice before or after the list of subscriptions in the My Account 'Subscriptions' tab.
<?php
/*
*
* Before subscriptions list in the My Account 'Subscriptions' tab.
*
*/
function notice__before_subscriptions_list( $has_methods ) {
wc_print_notice( __( 'These are your current subscriptions...', 'woocommerce' ), 'notice' );
};
@jrick1229
jrick1229 / satt_replace_none_with_price.php
Created March 1, 2019 18:17
Replace the 'None' option text in Subscribe All The Things with the product's price string.
<?php
add_filter('wcsatt_single_product_one_time_option_description', 'satt_replace_none_with_price', 5, 2);
function satt_replace_none_with_price ($none_string, $product) {
$none_string = _x( '' . $product->get_price_html() . '', 'product subscription selection - negative response', 'woocommerce-subscribe-all-the-things' );
return $none_string;