WP woocommerce change "Proceed to checkout" buttons URL - wordpress

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
}

Related

Woocommerce hook: How to add content inside the feature products on home page?

I'm trying to add the custom message inside the woocommerce website "Featured Product" section.
Like content goes inside the red box:
I've found the hook like below but it does not have any effect:
add_action('storefront_homepage_after_featured_products_title','homepage',15);
function homepage() {
echo "test";
}
I also try below code but that only display message under catalogue page.
add_action( 'woocommerce_after_shop_loop_item', 'catalogue_message', 15);
function catalogue_message() {
global $product;
$item_cost = $product->get_price();
if($item_cost != '') {
echo "TEST";
}
}
So not sure what's the name of woocommerce hook that can add the custom message on home page - Featured Product section.
Can anyone please let me know the name of the hook.
Thanks:)
Try this code to add html after price in featured product.
add_filter( 'woocommerce_get_price_html', 'theme_add_description_after_or_after_price', 2, 10 );
function theme_add_description_after_or_after_price($price, $product){
$description = ($product->is_featured()) ? 'Your HTML or text' : '';
return $price . $description ;
}

Error with: WooCommerce Avoid add to cart for non logged user

I wondered if anyone can help me?
I am using a Wordpress site with Woocommerce plugin.
I am using a piece of code to avoid adding to cart for non logged in customers which I found on this site, it works great apart from one issue. It doesn't work on the product page. When you click the add to cart button, it doesn't redirect to the custom login page like it does if you press the button on the category view page. Instead the page just refreshes.
I put the code in to the functions.php file. I've then tried putting it into a few other places but that hasn't worked. Could anyone help me with this and let me know if there is another location I should be putting the code in? Thanks in advance, I'd really appreciate the help!
Here's the link to the question and the code is below: WooCommerce Avoid add to cart for non logged user
// Replacing add-to-cart button in shop pages and archives pages (forn non logged in users)
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( ! is_user_logged_in() ) {
$link = get_permalink($product_id);
$button_text = __( "View product", "woocommerce" );
$html = ''.$button_text.'';
}
return $html;
}
// Avoid add to cart for non logged user (or not registered)
add_filter( 'woocommerce_add_to_cart_validation', 'logged_in_customers_validation', 10, 3 );
function logged_in_customers_validation( $passed, $product_id, $quantity) {
if( ! is_user_logged_in() ) {
$passed = false;
// Displaying a custom message
$message = __("You need to be logged in to be able adding to cart…", "woocommerce");
$button_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
$button_text = __("Login or register", "woocommerce");
$message .= ' '.$button_text.'';
wc_add_notice( $message, 'error' );
}
return $passed;
}
Firstly, your function hook for woocommerce_loop_add_to_cart_link is incorrect. You are using conditionally_change_loop_add_to_cart_link rather than quantity_inputs_for_woocommerce_loop_add_to_cart_link.
Secondly, your URL for the link is using the current product page ID, which is going to point you at the current product page URL and not another page.
Other than that, you had it mostly correct with woocommerce_add_to_cart_validation.
EDIT:
For product single pages, if you look at content-single-product.php in Woocommerce, the action woocommerce_template_single_add_to_cart seems to handle what the "add to cart" form looks like. If you'd like to not show the add to cart form, you'll want to first remove the action from the woocommerce_single_product_summary hook.
if(!is_user_logged_in()) {
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);
}
Then add your own action in at that priority to put in your message:
add_action('woocommerce_single_product_summary', function() {
global $product_id;
if(!is_user_logged_in()) {
$message = __("You need to be logged in to be able adding to cart…", "woocommerce");
$button_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
$button_text = __("Login or register", "woocommerce");
$message .= ' '.$button_text.'';
echo $message;
}
});
yes, you can do it by just adding following code into your active theme function.php file.
add_filter('woocommerce_get_price_html','login_before_addtocart');
function login_before_addtocart($price){
if(is_user_logged_in() ){
return $price;
}
else {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
$response .= $price;
$response .= '<br> Login to add product into cart';
return $response;
}
}

Add itemReviewed props into woocommerce product loop

On my woocommerce theme I get some errors on Google structured data such as:
I've found code into mytheme/woocommerce/loop/ratings.php
In this file there are only this method:
$product->get_average_rating();
The question is: there's a hook or action that implement this function?
I need to implement "ItemReviewed" props.
This is down to your product schema, option one is to remove the error but wont push the reviews is
/**
* Remove the generated product schema markup from Product Category and Shop pages.
*/
function wc_remove_product_schema() {
remove_action( 'woocommerce_shop_loop', array( WC()->structured_data, 'generate_product_data' ), 10, 0 );
}
add_action( 'woocommerce_init', 'wc_remove_product_schema' );
if you want to aggregate your reviews you will need to update your schema if you are using yoast and woocommerce this can be achieved by looking into the specific yoast woocoommerce plugin and the plugin docs to add the correct schema
https://developer.yoast.com/schema-documentation/woocommerce-seo/
I had the same problem - reviews were not validating and throwing an error
Managed to get it fixed by editing /wp-content/themes/YOURTHEME/woocommerce/single-product/review.php
I added the below code:
<p>Item Reviewed: <span itemprop="itemReviewed"><?php echo get_the_title(); ?></span></p>
That generated the itemReviewed markup in Structured Data Tool and my reviews validated.
Might want to place it in the child theme just in case :)
define the woocommerce_structured_data_review callback
function filter_woocommerce_structured_data_review( $markup, $comment ) {
global $product;
$markup['itemReviewed']['sku'] = $product->get_sku();
$markup['itemReviewed']['brand'] = $product->get_attribute( 'brand' ) ?? null;
$markup['itemReviewed']['description'] = wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) );
$markup['itemReviewed']['image'] = wp_get_attachment_url( $product->get_image_id() );
$markup['itemReviewed']['isbn'] = $product->get_attribute( 'isbn' ) ?? null;
$markup['itemReviewed']['AggregateRating'] = $product->get_average_rating();
return $markup;
};
Woocommerce Reviews filter
add_filter( 'woocommerce_structured_data_review',
'filter_woocommerce_structured_data_review', 10, 2 );

Override WooCommerce Shop Site Breadcrumb with Yoast SEO Breadcrumb

I tried to remove the WooCommerce Breadcrumbs with this code:
add_action( 'init', 'remove_wc_breadcrumbs' );
function remove_wc_breadcrumbs() {
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
}
...which works.
After that I activated the Yoast Seo Plugin Breadcrumbs an placed this code in my theme header.php
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
...which also works.
The problem appears when I navigate to my "shop-site" which I defined in the WooCommerce settings. The full breadcrumb looks like this
Home > Product
Note: The Page name in WordPress for the "shop-site" is "Test" (not "Product").
I want to override the "Product" in something different. I tried to override it by defining a custom breadcrumb title in the advanced Yoast settings on the wordpress "shop-site" page but it didn't seems to work. Overriding works if the page isn't defined as the "shop-site" in the WooCommerce settings. So I guess some function overwrites the Yoast Breadcrumb override but I can't figure out which function does this.
The same problem appears when I navigate to a single product. Breadcrumb looks like this
Home > Product > Product Name
Again I want to rename the "Product" (only the second crumb here) into something different.
Can someone solve this mysticism?
I fixed it with this code:
add_filter( 'wpseo_breadcrumb_output', 'custom_wpseo_breadcrumb_output' );
function custom_wpseo_breadcrumb_output( $output ){
if( is_product() ){
$from = 'rel="v:url" property="v:title">Product</a>';
$to = 'rel="v:url" property="v:title">New Title</a>';
$output = str_replace( $from, $to, $output );
}
elseif ( is_shop() ) {
$from = '<span class="breadcrumb_last">Products</span>';
$to = '<span class="breadcrumb_last">New Title</span>';
$output = str_replace( $from, $to, $output );
}
return $output;
}
I wonder if there wouldn't be an easier way to change the first "product" into sth like "product category"...
Have you tried in wp-admin:
Go to "Yoast"
Then "SEO settings"
Then "Breadcrumbs" and adapt taxonomies (eg: product > category).

Open WooCommerce External Products in New Tab

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.

Resources