Skip to content

Instantly share code, notes, and snippets.

@gabros20
Created April 11, 2019 12:45
Show Gist options
  • Save gabros20/ecec988cf340ca6c754b3d682781dc25 to your computer and use it in GitHub Desktop.
Save gabros20/ecec988cf340ca6c754b3d682781dc25 to your computer and use it in GitHub Desktop.
Woo Shortcode
<?php
// ----------------------------- //
// CHILD THEME SETUP FUNCTIONS //
// ----------------------------- //
function wpbf_child_theme_setup() {
// Child Theme Textdomain
load_child_theme_textdomain( 'page-builder-framework-child', WPBF_CHILD_THEME_DIR . '/languages' );
}
add_action( 'after_setup_theme', 'wpbf_child_theme_setup' );
// Enqueue Child Theme Scripts and Styles
function wpbf_child_scripts() {
// Styles
wp_enqueue_style( 'wpbf-style-child', WPBF_CHILD_THEME_URI . '/style.css', false, WPBF_CHILD_VERSION );
// Scripts (uncomment if needed!)
// wp_enqueue_script( 'wpbf-site-child', WPBF_CHILD_THEME_URI . '/js/site-child.js', false, WPBF_CHILD_VERSION, true );
}
add_action( 'wp_enqueue_scripts', 'wpbf_child_scripts', 100 );
// --------------- //
// DEBUGING CODE //
// --------------- //
// TO DEBUG ELEMENTOR "CONTENT.PHP MISSING" ERROR DISBALE CODE BELOW, WHEN EDITING IS DONE TURN CODE BACK
/* CODE ON/OFF, delete this --> */
// --------------------------------- //
// WORDPRESS GENERAL MODIFICATIONS //
// --------------------------------- //
// HIDE WP ADMIN BAR
add_filter('show_admin_bar', '__return_false');
// DISABLE THE AUTH CHECK FOR MONITORING WHETHER THE USER IS STILL LOGGED IN //
remove_action('admin_enqueue_scripts', 'wp_auth_check_load');
// ----------------------------------------- //
// INFORMEASY CUSTOM LOGIN PAGE FUNCTIONS //
// ----------------------------------------- //
// MAIN REDIRECTION OF THE DEFAULT LOGIN PAGE //
// !! CONTENT CONTROL PLUGIN IS SET TO RESTRICT PAGE VIEW ONLY TO LOGGED IN USERS WITH ROLL: ADMIN, EDITOR, SUBSCRIBER, CUSTUMER !! //
function redirect_login_page() {
$login_page = home_url('/new-login/');
$page_viewed = basename($_SERVER['REQUEST_URI']);
if($page_viewed == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') {
wp_redirect($login_page);
exit;
}
}
add_action('init','redirect_login_page');
// REDIRECT USERS IF THEY ALREADY LOGGED IN ACCORDING TO THIER USER ROLE UPON VISITING PAGE - 6207 //
// !! THIS CAUSE ELEMENTOR "CONTENT.PHP MISSING" ERROR DISBALE CODE TO EDIT PAGE !! //
add_action( 'template_redirect', 'wpmy_redirect_logged_in_users_away_from_home' );
function wpmy_redirect_logged_in_users_away_from_home() {
if( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
switch ($role_name) {
case "administrator":
if ( is_page( [6207] ) ) {
wp_redirect('http://127.0.0.1:8080/wordpress/wp-admin');
exit;
}
break;
case "subscriber":
if ( is_page( [6207] ) ) {
wp_redirect('http://127.0.0.1:8080/wordpress/new-dashboard');
exit;
}
break;
}
}
}
// WHERE TO GO AFTER SUCCESSFUL LOGIN //
add_filter('login_redirect', 'my_login_redirect', 10, 3 );
function my_login_redirect( $url, $request, $user ) {
if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if( $user->has_cap( 'administrator') or $user->has_cap( 'author')) {
$url = admin_url();
}
else {
$url = home_url('/new-dashboard/');
}
}
return $url;
}
// WHERE TO GO IF A LOGIN FAILED //
function custom_login_failed() {
$login_page = home_url('/new-login/');
wp_redirect($login_page . '?login=failed');
exit;
}
add_action('wp_login_failed', 'custom_login_failed');
// WHERE TO GO IF ANY OF THE FIELDS WERE EMPTY //
add_filter('authenticate', 'verify_user_pass', 1, 3);
function verify_user_pass($user, $username, $password) {
$login_page = home_url('/new-login/');
if($username == "" || $password == "") {
wp_redirect($login_page . "?login=empty");
exit;
}
}
// WHAT TO DO ON LOGOUT //
function logout_redirect() {
$login_page = home_url('/new-login/');
wp_redirect($login_page . "?login=false");
exit;
}
add_action('wp_logout','logout_redirect');
// WHAT TO DO ON LOGOUT FROM DASHBOARD MENU TO DISABLE WP LOGOUT CONFIRMATION//
function logout_without_confirm($action, $result) {
if($action == "log-out" && !isset($_GET['_wpnonce'])) {
$redirect_to = isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : '';
$location = str_replace('&amp;', '&', wp_logout_url($redirect_to));
header("Location: $location");
die;
}
}
add_action('check_admin_referer', 'logout_without_confirm', 10, 2);
// ----------------------------------------- //
// WOOCOMMERCE FUNCTIONAL MODIFICATIONS //
// ----------------------------------------- //
// REDIRECT STRAIGHT TO CHECKOUT //
add_filter('add_to_cart_redirect', 'cw_redirect_add_to_cart');
function cw_redirect_add_to_cart() {
global $woocommerce;
$cw_redirect_url_checkout = $woocommerce->cart->get_checkout_url();
return $cw_redirect_url_checkout;
}
// ORDER AGAIN BUTTON ON MY ACCOUNT/YOUR ORDERS, REDIRECT STRAIGHT TO CHECKOUT //
add_filter('woocommerce_ordered_again', 'add_to_cart_checkout_redirect');
function add_to_cart_checkout_redirect() {
wp_safe_redirect( 'http://127.0.0.1:8080/wordpress/?add-to-cart=4209' );
}
add_action( 'woocommerce_add_to_cart', 'add_to_cart_checkout_redirect', 11 );
// REDIRECT STRAIGHT TO CHECKOUT IF PRODUCT ALREADY ADDED //
function wc_add_to_cart_action() {
if ( empty( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) {
return;
}
$product_id = absint( $_REQUEST['add-to-cart'] );
$adding_to_cart = wc_get_product( $product_id );
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart && $adding_to_cart->is_sold_individually() ) {
wp_safe_redirect( wc_get_checkout_url() );
exit;
}
}
add_action( 'wp_loaded', 'wc_add_to_cart_action', 19 );
// ADD REMOVE PRODUCT BUTTON ON CHECKOUT //
add_filter('woocommerce_cart_item_name', 'custom_filter_wc_cart_item_remove_link', 10, 3);
function custom_filter_wc_cart_item_remove_link($product_name, $cart_item, $cart_item_key) {
if (is_checkout()) {
$product_name .= apply_filters('woocommerce_cart_item_remove_link',
sprintf('<a href="%s" rel="nofollow" class="remove" style="float:left;">&times;</a>',
esc_url(WC_Cart::get_remove_url($cart_item_key)),
__('Remove this item', 'woocommerce'),
esc_attr($cart_item['product_id']),
esc_attr($cart_item['data']->get_sku())), $cart_item_key);
return $product_name;
}
}
// REMOVE "XX PRODUCT HAS BEEN ADDED TO YOUR CART" ON CHECKOUT PAGE //
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
// REMOVE "COMPANY NAME" AND "PHONE NUMBER" FIELDS IN BILLING & SHIPPING FORM //
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_phone']);
return $fields;
}
// REORDER EMAIL IN CHECKOUT //
add_filter( 'woocommerce_billing_fields', 'bbloomer_move_checkout_email_field', 10, 1 );
function bbloomer_move_checkout_email_field( $address_fields ) {
$address_fields['billing_email']['priority'] = 5;
return $address_fields;
}
// RREMOVE CHEKOUT NOTE //
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// CHANGES THE REDIRECT URL FOR THE "RETURN TO SHOP BUTTON" IN THE CART //
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
function wc_empty_cart_redirect_url() {
return 'http://127.0.0.1:8080/wordpress/new-landing/';
}
// CHANGES THE TEXT FOR THE "RETURN TO SHOP BUTTON" IN THE CART TO "RETURN TO HOME PAGE"//
add_filter( 'gettext', 'change_woocommerce_return_to_shop_text', 20, 3 );
function change_woocommerce_return_to_shop_text( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Return to shop' :
$translated_text = __( 'Return to Home Page', 'woocommerce' );
break;
}
return $translated_text;
}
// REDIRECT USER AFTER PAYMENT DEPENDING ON PRODUCT ID //
function misha_redirect_depending_on_product_id() {
// do nothing if we are not on the appropriate page //
if( !is_wc_endpoint_url( 'order-received' ) || empty( $_GET['key'] ) ) {
return;
}
$order_id = wc_get_order_id_by_order_key( $_GET['key'] );
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item ) {
if( $item['product_id'] == 4209 ) {
wp_redirect( 'http://127.0.0.1:8080/wordpress/onboarding' );
exit;
}
}
}
add_action( 'template_redirect', 'misha_redirect_depending_on_product_id' );
// RENAME "COUPON CODE" TO "PROMO CODE" ON CART AND CART PAGE //
add_filter( 'gettext', 'woocommerce_rename_coupon_field_on_cart', 10, 3 );
add_filter( 'gettext', 'woocommerce_rename_coupon_field_on_cart', 10, 3 );
add_filter('woocommerce_coupon_error', 'rename_coupon_label', 10, 3);
add_filter('woocommerce_coupon_message', 'rename_coupon_label', 10, 3);
add_filter('woocommerce_cart_totals_coupon_label', 'rename_coupon_label',10, 1);
add_filter( 'woocommerce_checkout_coupon_message', 'woocommerce_rename_coupon_message_on_checkout' );
function woocommerce_rename_coupon_field_on_cart( $translated_text, $text, $text_domain ) {
// bail if not modifying frontend woocommerce text
if ( is_admin() || 'woocommerce' !== $text_domain ) {
return $translated_text;
}
if ( 'Coupon:' === $text ) {
$translated_text = 'Promo Code:';
}
if ('Coupon has been removed.' === $text){
$translated_text = 'Promo Code has been removed.';
}
if ( 'Apply coupon' === $text ) {
$translated_text = 'Apply Code';
}
if ( 'Coupon code' === $text ) {
$translated_text = 'Promo Code';
}
return $translated_text;
}
// rename the "Have a Coupon?" message on the checkout page
add_filter( 'woocommerce_checkout_coupon_message', 'woocommerce_rename_coupon_message_on_checkout' );
function woocommerce_rename_coupon_message_on_checkout() {
return 'Have a Promo Code?' . ' <a href="#" class="showcoupon">' . __( 'Click here to enter your code', 'woocommerce' ) . '</a>';
}
// rename the coupon error message on the checkout page
function rename_coupon_label($err, $err_code=null, $something=null) {
$err = str_ireplace("Coupon","Promo Code ",$err);
return $err;
}
// replace the coupon field instruction string text
add_filter( 'gettext', 'woocommerce_change_coupon_field_instruction_text' );
function woocommerce_change_coupon_field_instruction_text($translated) {
$translated = str_ireplace('If you have a coupon code, please apply it below.', 'If you have a Promo Code, please enter it below.', $translated);
return $translated;
}
// ADD MY-ORDERS.PHP TO A SHORTCODE //
function shortcode_review_order() {
wc_get_template( 'myaccount/my-orders.php', array( 'checkout' => WC()->checkout() ) );
}
add_action( 'init', 'add_shortcodes' );
function add_shortcodes() {
add_shortcode( 'ie_my_order_table', 'shortcode_review_order' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment