I'm trying to customize WooCommerce external product links to open in new tabs...
This is my try:
added a filter to the WordPress theme functions.php file as the following:
add_filter( 'woocommerce_product_add_to_cart_url', 'woocommerce_externalProducts_openInNewTab' );
function woocommerce_externalProducts_openInNewTab($product_url) {
global $product;
if ( $product->is_type('external') ) {
$product_url = $product->get_product_url() . '"target="_blank""';
}
return $product_url;
}
What did I missed?
what you're currently doing is wrong... get_product_url is named as what it do. It will give you the url... not the html anchor that has the url, but just the url.. so you're just adding some text to the url.. that's what you are doing...
One solution is given by #Ash Patel. You can change the markup by using templates... just navigate to your plugin folder and look for this file.. woocommerce\templates\single-product\add-to-cart\external.php. You can find instructions inside it.
Now, sometimes, we don't like editing templates... especially if it's just minor edits like this...
Below code will do it the way you want it... just paste this code in your theme's functions.php.
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
add_action( 'woocommerce_external_add_to_cart', 'rei_external_add_to_cart', 30 );
function rei_external_add_to_cart(){
global $product;
if ( ! $product->add_to_cart_url() ) {
return;
}
$product_url = $product->add_to_cart_url();
$button_text = $product->single_add_to_cart_text();
do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<p class="cart">
<?php echo esc_html( $button_text ); ?>
</p>
<?php do_action( 'woocommerce_after_add_to_cart_button' );
}
Here is how you add target="_blank" to the links on archive pages:
function ns_open_in_new_tab($args, $product)
{
if( $product->is_type('external') ) {
// Inject target="_blank" into the attributes array
$args['attributes']['target'] = '_blank';
}
return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'ns_open_in_new_tab', 10, 2 );
Replace ns_ part with your own namespace abbreviation.
Remove above funtion from function.php:
Use plugin files from Template folder by Template Overwrite method and then
follow below path:
woocommerce\templates\single-product\add-to-cart\external.php
open external.php where there is a tag, apply target="_blank".
it will work.
Related
I include woocommerce/template in my theme with this name : 'woocommerce' and also use add_theme_support() in my functions.php to introduce it .
but when user add a product to cart and wants to see the cart , user redirect to index.php
what should I do to fix this ?
#Ali Please share your code so I can have better idea.
OR
Add Theme Support Code to functions.php file
function mytheme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'mytheme_add_woocommerce_support' );
Created folder "woocommerce" into your theme folder.
Copy all files from "wp-content/plugins/woocommerce/templates/" to this folder.
Add below code to functions.php file.
function my_custom_add_to_cart_redirect( $url ) {
$url = get_permalink( get_option( 'woocommerce_checkout_page_id' ) );
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );
Try this.
finally I fix it. it was so stupid.
I just forgot to add page.php in my theme
<?php get_header(); if (have_posts()) {while (have_posts({the_content();}}get_footer();
because cart shortcut need to display in a page .
I use WooCommerce shortcode to show some products on the front page.
Like this [products limit="3" category="my-category" ids="86, 71, 54"].
The front page is a regular WordPress static page. The problem is that it doesn't show product short descriptions. If I use the code below but for is_front_page(), it shows short description of a regular WordPress post (not of the listed products).
function custom_short_description() {
if ( is_product_category() ) {
echo '<div class="custom-short-description">' . get_the_excerpt() . '</div>';
} }
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_short_description', 45 );
Adding to the function
global $post;
$product = get_product($loop->post);
and using
$product->post->post_excerpt;
didn't help.
Any ideas how to show product short descriptions?
===================
Update
===================
If you create custom loops, you might want to create variables at the beginning of the loop and then use them:
$product = wc_get_product( $loop->post->ID );
$product_short_description = $product->get_short_description();
$product_url = $product->add_to_cart_url();
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_short_description', 45 );
function custom_short_description() {
if (is_front_page()) {
global $product;
echo '<div class="custom-short-description">' . $product->get_short_description() . '</div>';
}
}
This should get you the outcome you're looking for.
Tried and tested WordPress 5.1.
I want to change to URL of the "Proceed to checkout" button. I wanna do this to to check if there's a need for my product, so my customers shouldn't be able to really buy it. Therefore I want to direct them not to the checkout but to a customized page of mine.
Thanks a lot.
Josh
It should be available at below path
wp-content/plugins/woocommerce/templates/cart/proceed-to-checkout-button.php
the best way is the woocommerce hooks
Try to use this code (put it in your functions.php theme file).
add_filter( 'woocommerce_get_checkout_url', 'my_change_checkout_url', 30 );
function my_change_checkout_url( $url ) {
$url = "your checkout url ";
return $url;
}
You can customize it by adding some conditions to change the checkout url
for example:
add_filter( 'woocommerce_get_checkout_url', 'my_change_checkout_url', 30 );
function my_change_checkout_url( $url ) {
$allowed_countries = array('FR');
$customer_country = WC()->customer->get_default_country();
if( !in_array( $customer_country , $allowed_countries ) ) {
$url = wc_get_page_permalink( 'other-checkout' );
}
return $url;
}
Change Proceed To Checkout Text In WooCommerce
* Change Proceed To Checkout Text in WooCommerce
* Place this in your Functions.php file
This is the new method, for version 2.3.8 of WooCommerce and forward.
<?php
function woocommerce_button_proceed_to_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
?>
<?php _e( 'Check On Out', 'woocommerce' ); ?>
<?php
}
I am working on a project in woocommerce.
Can anyone help me to add a custom text field next to "add to cart"?
I tried the following but it is not working.
add_action( 'woocommerce_after_single_product_summary', 'add_custom_field', 0 );
function add_custom_field() {
global $post;
echo "<div class='prod-code'>Product Code: ";
$text= get_field('txt-field', $post);
echo "</div>";
return true;
}
Perhaps try using $post->ID like this:
add_action( 'woocommerce_after_single_product_summary', 'add_custom_field', 0 );
function add_custom_field() {
global $post;
echo "<div class='prod-code'>Product Code: ";
$text= get_field('txt-field', $post->ID);
echo "</div>";
return true;
}
put your html code inside content-product.php (wp-content/themes/YOUR THEME/woocommerce/content-product.php)
content-product.php contains code of list page in wordpress. if you want to edit product page than edit content-single-product.php.
You are using Woocommerce there are two case :
You are reading post ID
You want to display a SKU
You can implement it via single function :
add_action( 'woocommerce_single_product_summary', 'display_productCode', 5 );
function display_productCode(){
global $product;
echo 'SKU: ' . $product->get_sku();
echo 'Product ID:'.$product->get_id();
}
I am creating a plugin that needs a virtual page in order to dynamically create a "Review Order" page (Orders are a custom post type I created). I was able to create a virtual page using the following code:
// Set up the rewrite rules for the order page
add_action( 'init', 'action_init_redirect' );
function action_init_redirect() {
add_rewrite_rule( 'orders/?', 'index.php?orders=new', 'top' );
}
add_filter( 'query_vars', 'filter_query_vars' );
function filter_query_vars( $query_vars ) {
$query_vars[] = 'orders';
return $query_vars;
}
add_action( 'parse_request', 'action_parse_request');
function action_parse_request( &$wp ) {
if ( array_key_exists( 'orders', $wp->query_vars ) ) {
//Beginning of page code
echo "hello";
exit;
}
}
The problem is that this creates a page with a blank template, that is, the above code creates a blank page with the text hello. I would like for the virtual page to be within the theme of the site and displayed like a regular page within the WordPress framework. How to accomplish this?
A solution is to add a template page inside parse_request:
function action_parse_request( $wp ) {
if ( array_key_exists( 'virtual', $wp->query_vars ) ) {
get_header(); ?>
<div id="primary">
<div id="content" role="main">
Hello, world!
</div>
</div>
<?php get_footer();
exit;
}
}
try using the following:
define('WP_USE_THEMES', true);
require('/ABSOLUTE_PATH_HERE/wp-blog-header.php');
require('/ABSOLUTE_PATH_HERE/wp-load.php');
you will need to add this to the top of your page.