Skip to content

Instantly share code, notes, and snippets.

@BurlesonBrad
Created April 21, 2016 03:23
Show Gist options
  • Save BurlesonBrad/e0261d3ac892c6baa2f70d6d8e14c4cc to your computer and use it in GitHub Desktop.
Save BurlesonBrad/e0261d3ac892c6baa2f70d6d8e14c4cc to your computer and use it in GitHub Desktop.
Fun for UD :-)
<?php
/*
* Plugin Name: Brad Gives to Usability Dynamics
* Plugin URI: https://bradgriffin.me
* Description: Look at your pageflow. After a user adds anything Invoice related to my cart, redirect the user to another Invoice related product. Same concept with the Property products. Almost immediately, you're total per transaction amount will go up #whoop!
* Author: Brad Griffin
* Author URI: https://bradgriffin.me
* Version: 1.0
* License: GPL 2.0
* Text Domain: woocommerce
*/
//* Add/Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'bkg_woo_add_custom_general_fields' );
function bkg_woo_add_custom_general_fields() {
global $post_id, $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_bkg_woo_product_custom_redirect_url',
'label' => __( 'Redirect on Add to Cart', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter a URL to redirect the user to after this product is added to the cart.', 'woocommerce' ) ,
'value' => get_post_meta( $post_id, '_bkg_woo_product_custom_redirect_url', true )
)
);
echo '</div>';
}
//* Save Fields
add_action( 'woocommerce_process_product_meta', 'bkg_woo_add_custom_general_fields_save' );
function bkg_woo_add_custom_general_fields_save( $post_id ){
$bkg_woo_redirect_url = $_POST['_bkg_woo_product_custom_redirect_url'];
if ( ! empty( $bkg_woo_redirect_url ) ) {
update_post_meta( $post_id, '_bkg_woo_product_custom_redirect_url', esc_url( $bkg_woo_redirect_url ) );
}
}
//* Redirect to URL
add_filter( 'woocommerce_add_to_cart_redirect', 'bkg_redirect_to_url' );
function bkg_redirect_to_url() {
global $woocommerce, $post;
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
$bkg_woo_redirect_url = get_post_meta( $product_id, '_bkg_woo_product_custom_redirect_url', true );
if ( ! empty( $bkg_woo_redirect_url ) ) {
wp_redirect( esc_url( $bkg_woo_redirect_url ) ); exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment