Easy Digital Downloads redirected to a specific page when they visit the checkout page is empty - wordpress

I am using Easy Digital Downloads to sell some digital products, I want if the checkout page is empty, when they try to visit the empty checkout page they will be redirected to a specific page. is it possible?
if possible how can I do this?

I know that this post is old but I found a snippet that actually works, maybe someone can be useful.
function edd_empty_cart_redirect() {
$cart = function_exists( 'edd_get_cart_contents' ) ? edd_get_cart_contents() : false;
$redirect = site_url( 'shop' ); // could be the URL to your shop
if ( function_exists( 'edd_is_checkout' ) && edd_is_checkout() && ! $cart ) {
wp_redirect( $redirect, 301 );
exit;
}
}
add_action( 'template_redirect', 'edd_empty_cart_redirect' );

WooCommerce already redirect checkout to cart page if there are not products in cart, but you can change that and redirect to your custom page:
function wc_custom_template_redirect(){
global $wp_query, $wp;
// When on the checkout with an empty cart, redirect to cart page.
if ( is_page( wc_get_page_id( 'checkout' ) ) && wc_get_page_id( 'checkout' ) !== wc_get_page_id( 'cart' ) && WC()->cart->is_empty() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_redirect_empty_cart', true ) ) {
$page_id = 62;
wp_redirect( get_the_permalink( $page_id ) );
exit;
}
}
add_action( 'template_redirect', 'wc_custom_template_redirect', 0 );
Make sure the $page_id reflects ID of the page you want to redirect to.

Related

External product feature image to external link in new tab -wordpress woocommerce

I have a woocommerce store.
Its an affiliate store.
I want that when clicked on the featured image on the shop page it goes to an external website
I have added below code to my function.php its working fine just need to know what code and where is to be added to open it in a new tab
**<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_action( 'template_redirect', 'redirect_external_products' );
function redirect_external_products() {
global $post;
if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
wp_redirect( $product->get_product_url() );
exit;
}
}**
There is no way to redirect in a new tab, I will suggest adding "target='_blank'" to the tag where you display the featured image with probably the same condition as in the function above. The hacky way maybe
add_action( 'template_redirect', 'redirect_external_products' );
function redirect_external_products() {
global $post;
if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
echo "<script> window.open(" . $product->get_product_url() . ", '_blank') </script>";
}
}
But I strongly suggest you not to go this way!!

Redirect to URL if the user isnt the author of the post

I am looking for a way to redirect when viewing any publication, except the author of the publication.
There are two roles in the "author" and "custom_role" site. This last role is allowed to see all.
the role of author can only see his own, the rest redirects.
I've tried for a while, in this last code I'm working but it does not work and I do not know why
Thanks very much!
add_action( 'pre_get_posts', function() {
if( is_author() )
{
if( ! is_user_logged_in() )
{
wp_redirect( 'https://aaa.com/custom' );
exit;
}
$author = get_queried_object();
if( $author->ID != get_current_user_id() )
{
wp_redirect( get_author_posts_url(
get_current_user_id() ) );
exit;
}
}
} );
First of all, is_author() is used to check if current page is author archive page. Please check following example. This may not be the exact anster but it may help. In the single post, post author and current user ID is compared. If they are not same then it is redirected to home page. If those IDs are same then, current user is also the post author, so current user will be allowed to view page.
add_action( 'get_header', 'wpso_author_redirection' );
function wpso_author_redirection() {
if ( is_singular() ) {
$current_post_details = get_post( get_the_ID(), ARRAY_A );
$user_id = get_current_user_id();
if ( $user_id !== absint( $current_post_details['post_author'] ) ) {
wp_redirect( home_url() );
}
}
}

Redirect to home if the cart is empty in Woocommerce

I am looking for and can not find anywhere as I can redirect the empty woocommerce cart to the homepage. I only find redirects that go to the store.
This is what I find, but I do not need to redirect to the home:
add_action("template_redirect", 'redirection_function');
function redirection_function(){
global $woocommerce;
if( is_cart() && WC()->cart->cart_contents_count == 0){
wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
}
}
Context link: If cart is empty, the cart page will redirect to shop page in WooCommerce?
To redirect to home if cart is empty you will use a similar code:
add_action( 'template_redirect', 'empty_cart_redirection' );
function empty_cart_redirection(){
if( WC()->cart->is_empty() && is_cart() ){
wp_safe_redirect( esc_url( home_url( '/' ) ) );
exit;
}
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
But if cart is emptied through ajax, this code will enable a redirection to home until the page will be reloaded.
For those finding this via search (like me) the code that Loic provided works with one small change if you're wanting the empty cart to redirect back to the homepage:
add_action( 'template_redirect', 'empty_cart_redirection' );
function empty_cart_redirection(){
if( WC()->cart->is_empty() && ( ! is_front_page() || is_cart() ) ){
wp_safe_redirect( esc_url( home_url( '/' ) ) );
exit;
}
}

Redirected non-admin users. Now after deleting the code non-admins still can't login

I have used the following code to redirect non-admin users to the home page. Now after deleting the code, the effect is irreversible. non-admins still redirecting after login.
add_action( 'init', 'restrict_wp_dashboard_init' );
function restrict_wp_dashboard_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
!( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_redirect( home_url() );
exit;
}
}
Help !!!

I need to create a few customer specific products in woocommerce which should only be shown to specific users

I am developing a ecommerce store using WordPress and WooCommerce.
I have a products.
I will only allow a specific customer to purchase the product.
So I would like to only show this product if the specific customer is logged in.
Thanks
This is useful to restrict user,
// Woocommerce - Redirect unauthorised users from accessing a specified product category when clicked or visited via direct url
function woocommerce_hide_non_registered() {
if( ( is_product_category('specials') ) && ! ( current_user_can( 'customer' ) || current_user_can( 'administrator' ) ) ) {
wp_redirect( site_url( '/' ) );
exit();
}
}
add_action( 'template_redirect','woocommerce_hide_non_registered' );
// End - Woocommerce - redirect unauthorised users from accessing a specified product category
// Woocommerce - Removes category link from woocommerce product category widgets so they are not seen
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if a product category and on the shop page
if ( in_array( 'product_cat', $taxonomies ) && ! ( current_user_can( 'customer' ) || current_user_can( 'administrator' ) ) && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'specials' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
// End - Woocommerce - Removes category link from woocommerce product category widgets so they are not seen
// Woocommerce - Remove products from being displayed that belong to a category user is not authorised to visit. Products seem to still be accessible via direct url unfortunately.
add_action( 'pre_get_posts', 'custom_pre_get_posts' );
function custom_pre_get_posts( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! ( current_user_can( 'customer' ) || current_user_can( 'administrator' ) ) && is_shop() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'specials'), // Don't display products in the private-clients category on the shop page
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
// End - Woocommerce - Remove products from being displayed that belong to a category user is not authorised to visit. Products seem to still be accessible via direct url unfortunately.
This will do the trick for you.
add_action( 'pre_get_posts', 'dm_restrict_user_to_show_own_posts_only' );
function dm_restrict_user_to_show_own_posts_only( $dm_wp_query_obj )
{
// Front end, do nothing
if( !is_admin() )
return;
global $current_user, $pagenow;
wp_get_current_user();
// http://php.net/manual/en/function.is-a.php
if( !is_a( $current_user, 'WP_User') )
return;
// Not the correct screen, bail out
if( 'edit.php' != $pagenow )
return;
// Not the correct post type, bail out
if( 'product' != $dm_wp_query_obj->query['post_type'] )
return;
// If the user is not administrator, filter the post listing
if( !current_user_can( 'delete_plugins' ) )
$dm_wp_query_obj->set('author', $current_user->ID );
}

Resources