Skip to content

Instantly share code, notes, and snippets.

@bernattorras
bernattorras / custom_add_product_to_cart.php
Created July 5, 2018 17:14
Function to add a product to clear the cart and add a specific product when a certain page is loaded
<?php
/**
* Function to add a product to clear the cart and add a specific product when a certain page is loaded
**/
add_action( 'template_redirect', 'custom_add_product_to_cart' );
function custom_add_product_to_cart() {
if(is_page(124)){
$product_id = 1098;
WC()->cart->empty_cart();
@bernattorras
bernattorras / woocommerce_subscription_pre_update_status.php
Created July 5, 2018 17:12
Function to run some code when (before) the status of a subscription changes
<?php
/**
* Function to run some code when (before) the status of a subscription changes
**/
add_action('woocommerce_subscription_pre_update_status', 'order_satus_changed', 100, 3);
function order_satus_changed($from, $to, $order ){
// Run any code here
}
@bernattorras
bernattorras / save_trial_end_meta.php
Created July 5, 2018 17:11
Function to save the end of the trial date (on a custom meta field) when changing a subsription status to 'cancel', 'pending cancelation', 'switched' or 'expired'
<?php
/**
* Function to save the end of the trial date (on a custom meta field) when changing a subsription status to 'cancel', 'pending cancelation', 'switched' or 'expired'
**/
add_action('woocommerce_subscription_pre_update_status', 'save_trial_end_meta', 10, 3);
function save_trial_end_meta($old_status, $new_status, $sub){
if(in_array($new_status, array('cancelled', 'pending-cancel', 'switched', 'expired'))){
$sub_id = $sub->get_id();
$trial_end_date = $sub->get_date('trial_end');
@bernattorras
bernattorras / disable_switch_notice.php
Created July 5, 2018 17:09
Function to disable the "Choose a new subscription" message when switching a subscription item
<?php
/**
* Function to disable the "Choose a new subscription" message when switching a subscription item
**/
add_filter('woocommerce_add_notice', 'disable_switch_notice', 10, 1);
function disable_switch_notice( $message){
if($message=="Choose a new subscription."){
$message = '';
}
@bernattorras
bernattorras / redirect_to_checkout.php
Created July 5, 2018 17:08
Function to redirect the customer to a the checkout page (or a custom page) after adding a product to the cart
<?php
/**
* Function to redirect the customer to a the checkout page (or a custom page) after adding a product to the cart
**/
add_filter ('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
return wc_get_checkout_url();
}
@bernattorras
bernattorras / woo_custom_redirect_after_purchase.php
Created July 5, 2018 17:07
Function to redirect the customer to a custom 'thank you' page after placing an order
<?php
/**
* Function to redirect the customer to a custom 'thank you' page after placing an order
**/
add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
function woo_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( ' http://localhost:8888/subscriptions/custom-thank-you-page/' );
@bernattorras
bernattorras / woocommerce_subscription_period_interval_strings.php
Created July 5, 2018 17:06
Function to add extra month intervals to the subscription editor page
<?php
/**
* Function to add extra month intervals to the subscription editor page
**/
add_filter('woocommerce_subscription_period_interval_strings', 'add_extra_month_intervals', 10, 1);
function add_extra_month_intervals($intervals){
foreach ( range( 7, 12 ) as $i ) {
$intervals[$i] = "every ".$i."th";
}
@bernattorras
bernattorras / woocommerce_add_to_cart_redirect.php
Created July 5, 2018 17:05
Function to preserve custom URL parameters on OPC pages after adding a product to the cart
<?php
/**
* Function to preserve custom URL parameters on OPC pages after adding a product to the cart
**/
add_filter( 'woocommerce_add_to_cart_redirect', 'opc_keep_affiliate_arg', 11, 1 );
function opc_keep_affiliate_arg( $url ) {
if ( ! is_ajax() && is_wcopc_checkout() ) {
$schema = is_ssl() ? 'https://' : 'http://';
$url = explode('?', $schema . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] );
@bernattorras
bernattorras / woocommerce_scheduled_subscription_payment.php
Created July 5, 2018 17:03
Function to execute some code when a subscription is paid
<?php
/**
* Function to execute some code when a subscription is paid
**/
add_action('woocommerce_scheduled_subscription_payment', 'renewal_notifier', 11, 1);
function renewal_notifier($sub_id){
// Your custom functionality
}
?>
@bernattorras
bernattorras / woocommerce_scheduled_subscription_payment_stripe.php
Created July 5, 2018 17:02
Function to execute some code when a subscription is paid using Stripe gateway
<?php
/**
* Function to execute some code when a subscription is paid using Stripe gateway
**/
add_action('woocommerce_scheduled_subscription_payment_stripe', 'renewal_notifier_stripe', 11, 2);
function renewal_notifier_stripe($total, $renewal_order){
// Your custom functionality
}
?>