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

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!!

Related

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

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.

Add custom javascript to Woocommerce edit product page

how can I reference a custom js-file only to the Woocommerce edit / add product page?
Thanks in advance.
Ciao
This worked for me (to be added in themes's functions.php file):
//wholesale-prices
function add_admin_scripts( $hook ) {
global $post;
if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
if ( 'product' === $post->post_type ) {
wp_enqueue_script( 'myscript', get_stylesheet_directory_uri().'/js/wholesale-prices.js' );
}
}
}
add_action( 'admin_enqueue_scripts', 'add_admin_scripts', 10, 1 );

How can i access woocommerce order variables inside shortcode?

I am adding a shortcode from wordpress to woocommerce email template.
do_shortcode('[sample_test name="additional_gift_msg"]);
Then i am using this to display the value in email. I am able to display value.
function th_email_shortcode_handler( $atts ) {
if ( ! empty( $atts['name'] ) ) {
$field = $atts['name'];
echo 'Found me';
}
}
add_shortcode('sample_test','th_email_shortcode_handler');
But i need $order or $order_id inside this handler function to take some value from post meta. How can i use those variables? The shortcode handler function is in functions.php
Also i tried the following but still $order_id is empty.
do_shortcode('[sample_test name="additional_gift_msg" order_id=' . $order->get_id() . ']');
Below code will do the trick.
function th_email_shortcode_handler( $atts ) {
if ( ! empty( $atts['name'] ) ) {
$field = $atts['name'];
echo 'Found me';
}
global $wp;
$order_id = absint( $wp->query_vars['order_id'] );
if ( empty($order_id) || $order_id == 0 )
return; // Exit;
return $order_id;
}
add_shortcode('sample_test','th_email_shortcode_handler');

Add woocommerce product type to body class array

I am trying to add Woocommerce product types to the Wordpress body tag class array thats called in header.php with
body_class();
I have the following function in functions.php but it is not adding the class. If I remove the conditional and just have
$classes[] = 'simple-product';
Then the class is added. I assume this is to do with an issue getting global values. I am calling in $woocommerce, $post and $product globals as I am not sure which I actually need.
//Add Woocommerce body classes
add_filter('body_class','ttm_woocommerce_body_classes');
function ttm_woocommerce_body_classes($classes){
global $woocommerce, $post, $product;
if ( $product->product_type == 'simple' ) $classes[] = 'simple-product';
return $classes;
}
Thanks
Have you tried var_dump($product) to see what (if anything) exists in that object?
According to the codex, you might have to populate it yourself using $post->ID, like so:
//Add Woocommerce body classes
add_filter('body_class','ttm_woocommerce_body_classes');
function ttm_woocommerce_body_classes($classes){
global $post;
$product = get_product( $post->ID );
if ( $product->product_type == 'simple' ) $classes[] = 'simple-product';
return $classes;
}
In woocommerce page add product_type name in your website body tag. First you need to get this page is a product page.
Using with get_product() you got your page is working with woocommerce functionality.
This is a code to add all product_type name added in body tag.
Add this code into your activate theme folder : functions.php
add_filter('body_class','obw_woocommerce_body_classes');
function obw_woocommerce_body_classes( $classes ) {
global $woocommerce, $post, $product;
$product = get_product( $post->ID );
$product_type = $product->product_type;
if ( $product->product_type == 'external' ) $classes[] = 'external-product';
if ( $product->product_type == 'grouped' ) $classes[] = 'grouped-product';
if ( $product->product_type == 'simple' ) $classes[] = 'simple-product';
if ( $product->product_type == 'variable' ) $classes[] = 'variable-product';
return $classes;
}

wordpress show only media user has uploaded in wp_editor

I'm creating a wordpress site where the registered user has the ability to create his own post via wp_editor() on the frontend, but just one post.
Now I want to restrict the user to be able to only see his uploaded media. I use the following script in the functions.php, which works in the backend. So if a user goes to the media section in the backend he will only see his uploaded media.
But if the user goes to "insert media" pop-up on the frontend wp_editor he can still see the uploaded media from all the users.
function restricted_media_view( $wp_query ) {
if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false
|| strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
if ( !current_user_can( 'level_5' ) ) {
global $current_user;
$wp_query->set( 'author', $current_user->id );
}
}
}
add_filter('parse_query', 'restricted_media_view' );
Do you have any idea hot to solve this annoyance? Thank you!
You might try this plugin: http://wordpress.org/extend/plugins/view-own-posts-media-only/
Alternatively try this:
add_action('pre_get_posts','ml_restrict_media_library');
function ml_restrict_media_library( $wp_query_obj ) {
global $current_user, $pagenow;
if( !is_a( $current_user, 'WP_User') )
return;
if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
return;
if( !current_user_can('manage_media_library') )
$wp_query_obj->set('author', $current_user->ID );
return;
}
Source: http://wpsnipp.com/index.php/functions-php/restricting-users-to-view-only-media-library-items-they-upload/#comment-810649773
alternatively since WordPress 3.7
add_filter( 'ajax_query_attachments_args', "user_restrict_media_library" );
function user_restrict_media_library( $query ) {
global $current_user;
$query['author'] = $current_user->ID ;
return $query;
}
I use API/Filter Reference/ajax query attachments args for WP 4.3.1 and works
add_filter( 'ajax_query_attachments_args', 'show_current_user_attachments', 10, 1 );
function show_current_user_attachments( $query = array() ) {
$user_id = get_current_user_id();
if( $user_id ) {
$query['author'] = $user_id;
}
return $query;
}
just add on functions.php
or check this link WP Codex

Resources