I am trying to display a button which will direct users to a different product but the URL of this button will be the same as the current URL but will append 2 added letters to the end of the URL. For example, I am on product URL mywebsite.com/products/super-awesome-product-abc and I want the button to link to mywebsite.com/products/super-awesome-product-abcdef. I have 100 products and I will be creating more over time. I would like a dynamic solution that will display the new URL regardless of what page/product they are on. The appended characters will always be the same. I will add that the appended "abc" and "abcdef" in my example are also the product SKU. Perhaps I can use this in the solution. I am adding these buttons to the /themes/mytheme/woocommerce-bookings/booking-form/datetime-picker.php file.
I believe this works for me. I placed the below code in my themes function.php file:
add_action( 'woocommerce_before_add_to_cart_form', 'booking_lastminute_button', 35 );
function booking_lastminute_button(){
global $product;
if( $product->get_sku() ) {
echo '<p class="lastminute-button">Click to find a better product</p>';
}
Related
I would like to hide all buttons only for Eternal/Affiliate and only on the produce page
TLDR
On my site I have a few Pricing tables where the customer can select the appropriate one and then add that item to the cart.
The issue is trying to make an empty product where you cannot purchase it but the pricing table is in the description. So I had to user Eternal/Affiliate product. Which works perfectly as the URL is the product page itself which is great for the shop.
The issue is on the product page you can see a button which says add to cart.
I would like to hide this button only for Eternal/Affiliate and only on the produce page.
You see I already have this code below but obviously I do not want to restrict it to ID numbers and variable products.
Update
so the code below works but if I put a URL link in on the product page then it does not work. Unfortunately now a little bit stumped.
What would be great is if I could remove the button and replace it with some text for example please read below.
function woocommerce_add_aff_link(){
$product = wc_get_product(get_the_ID());
if ($product->is_type('external'))
echo '<a href="' .
$product->get_product_url() .
'" class="woocommerce-LoopProductImage-link">';
}
Update +
After some digging around I found that it is actually more logical to create a new product however the solution below works perfectly:
Reference : https://www.businessbloomer.com/woocommerce-how-to-create-a-new-product-type/
To remove add to cart button on single external product pages use the following:
add_action( 'woocommerce_single_product_summary', 'remove_external_product_add_to_cart_button', 4 );
function remove_external_product_add_to_cart_button(){
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product(get_the_ID());
}
if ( $product->is_type('external') ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
Code goes in functions.php file of the active child theme (or active theme). TIt should work.
I have a link that is in the short description of a woo-commerce product. This link type is there in every product I have (different of course).
I'll need a script to automatically change the links. These are the attempts I tried so far:
WP-HideRefer
I have installed the plugin (https://wordpress.org/plugins/wp-hiderefer/), did not work. Did not change any links.
Added scripts
I used the script provided in https://anonym.to/ and put in at the end of my footer.php (before ), did not work.
Tried many other Wordpress hide referrer plugins, did not work.
Up until this point, I'm lost. I am not sure.
TL:DR
Appending text in front of every external link (link in short desc/normal desc of woocommerce product EXCEPT own website domain/link.
https://www.google.com/ is on my woocommerce product.
When user loads the product, he sees https://anonym.to/?https://www.google.com/ instead.
(https://anonym.to/? + link)
This code snippet will replace all URLs on a WooCommerce product page in both the excerpt (short description) as well as the longer description/content if you want.
It uses a regex to replace the URLs.
function so61548594_anonymize_urls($description) {
$prefix = 'https://anonym.to/?';
$description = preg_replace(
'#(<a.*?)href=["\']([^"\']+)["\'](.*?>[^>]*</a>)#i',
('$1href="' . $prefix . '$2"$3'),
$description
);
return $description;
}
add_filter('woocommerce_short_description', 'so61548594_anonymize_urls');
add_filter('the_content', function ($description) {
if (function_exists('is_product') && is_product()) {
return so61548594_anonymize_urls($description);
}
return $description;
});
Let me know if it works for you.
I want to insert / change a link on my checkout page, but am not sure how to change the code so it appears correctly. Hoping someone might be able to shed some light. I research & tried a number of things, but kept getting error pages. All I am trying to do is change the "Cart" link to be another link. But since the cart link isn't formatted like a regular link, I'm not sure what to replace to get it to show properly. I want to just replace it with a regular URL (https://sprinly.com/subscription-plans).
Thanks in advance!!
add_action( 'woocommerce_checkout_before_order_review', 'my_wc_checkout_before_order_review', 10, 0 );
function my_wc_checkout_before_order_review() {
echo '<p class="return-to-cart">If you\'d like to change your subscription, go back to Cart. </p>';
}
You need to replace the PHP that is generating the cart link (WC()->cart->get_cart_url()) with the URL, like so:
add_action( 'woocommerce_checkout_before_order_review', 'my_wc_checkout_before_order_review', 10, 0 );
function my_wc_checkout_before_order_review() {
echo '<p class="return-to-cart">If you\'d like to change your subscription, go back to Cart. </p>';
}
This question is based on an unanswered question in Wordpress Development which has not gotten a solid answer.
I have a wordpress website which lists hotels. the url for a single hotel looks like:
/hotels/the-marriot-hotel
I also have a custom taxonomy for Locations, which allows me to browse the hotels in various locations, which works fine, the urls are like:
/Locations/Liverpool
For URL's like /hotels/* I would like to use a custom template, which I have done already and works fine.
The Problem
I also want to be able to drilldown the Locations taxonomy creating a breadcrumb type URL and also use a different template for the hotel page.
For Example, if a user is browsing /Locations/Liverpool and clicks the Marriot Hotel I would like it to click through to /Locations/Liverpool/the-marriot-hotel instead of /hotels/the-marriot-hotel and also use a slightly different template, which can also load a different sidebar and recommend other hotels in the area specific to the location slug in the URL
So basically I want two routes to a single post and a different template used based on the route used.
How would I go about implementing this?
What have I tried?
I've tried adding a new page and using a rewrite rule to point to it to be the locations hotel page.
I've tried adding a slug on the end of the /Locations/{location-slug} url and reading this in the page template and loading the hotel post instead of the list it doesn't seem to be working but also feels like a terrible hack anyway
An idea that I've had is to add a rewrite to the hotels/{slug} page and using code to detect the URL used and switch templates dynamically but I'm not sure this is the best approach
I have managed to get this working using the second method mentioned above (adding a rewrite to the locations landing page and checking for a query_var).
I will post the code below that I used but although this works and seems to be working very well, It does not feel like the best way of doing it. If someone know of a better way of doing this please post the answer.
I used this online post for reference.
Note: The listing page shows the list of hotels in the taxonomy in a side column down the side and shows the currently selected or a random one in the main content area. Which will explain how I am using the loop below.
function prefix_locations_rewrite_rule() {
add_rewrite_rule( 'Locations/([^/]+)/([^/]+)', 'index.php?locations=$matches[1]&hotel=$matches[2]', 'top' );
}
function prefix_register_query_var( $vars ) {
$vars[] = 'hotel';
return $vars;
}
function prefix_url_rewrite_templates() {
if ( get_query_var( 'hotel' ) && is_singular( 'hotel' ) ) {
add_filter( 'template_include', function() {
return get_template_directory() . '/taxonomy-locations.php';
});
}
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
add_filter( 'query_vars', 'prefix_register_query_var' );
add_action( 'init', 'prefix_locations_rewrite_rule' );
In my template file for the hotels landing page:
$hotelSlug = get_query_var( 'hotel', false);
if ( have_posts() ) {
while (have_posts()) : the_post();
if ($post->post_name == $hotelSlug) {
break;
}
endwhile;
}
This bit of code will iterate over the posts and if the hotel slug matches the query var it will break there so that the current post is the one we wanted.
We could just use a query here but as I already have a list of posts within the taxonomy I thought I'd just iterate over it. Below this I check to see if a specific hotel has been selected otherwise I show a random one from the list.
I am still to add additional logic and error handling to this code, I hope it helps someone with a similar issue
I've seen many tutorials dealing on how to customize woocommerce's checkout page by adding or removing fields.
But what I want is to place a link or button on the Woocommerce Checkout page saying "Return to Cart" (obviously linking to the cart page) but I want it placed just after the "Your Order" section, (the section where you review your order). I want it there because I want it along with a text saying something like "If you want to change your order return to Cart".
If I edit the actual checkout page and add the link there, it shows all the way to the bottom so maybe I have to add code to the theme's functions file? Any guidance will be greatly appreciated.
Thank you.
EDIT:
Ok, I've found a very crappy way of doing it.
I just added this line to the review-order.php file located in woocommerce/templates/checkout/ , right after the shop_table class:
<?php echo "<strong>If you'd like to change your order, go back to <a href='http://www.mysite.com/cart/'>My Cart</a></strong><br />"; ?>
This does the trick, but everytime I update woocommerce I will have to added it again.
Any suggestion of a more practical and intelligent way of doing it?
Create a child theme.
Put this in the child theme's functions.php
/**
* Add link back to cart after order review on Checkout
*/
add_action( 'woocommerce_review_order_before_payment', 'my_back_to_cart_link' );
function my_back_to_cart_link(){
//get the cart link
global $woocommerce;
$cartUrl = $woocommerce->cart->get_cart_url();
//the HTML markup to add
$backToCartLink="<p class='backtocart'><a class='button alt' href='".$cartUrl."'>".__('Edit Cart','wooint')."</a></p>";
echo $backToCartLink;
}
Well, if you created a child theme you could have put that line in your child's functions.php and then the only way an update would affect it is if they changed the coding.