I have a multi-currency wordpress website (using WPML plugin), I need a way to make a link to a specific product in a specified currency. I found this "hook" which can be used for that purpose: wcml_client_currency. As part of that, I added this code to my child functions.php file:
add_action( 'wcml_client_currency', 'currency' );
function currency( $current_currency ) {
$currency = isset( $_GET['currency'] ) ? esc_attr( $_GET['currency'] ) : $current_currency;
return strtoupper( $currency );
}
This works partially. I can use the URL mysite.com/?currency=EUR and arrive to the page with the currency selected to Euro. However, this code disables the currency selector widget which I have on my site. So landing on this page now does not change the currency anymore when you use the normal currency selector.
It seems to be a problem with two contradicting URL informations. i.e. with the above code, I add this "?currency=EUR" but the currency selector automatically adds this "wcmlc=GBP" to the URL. When both appear in the URL, the latter does not work.
When I use the "?currency=EUR" URL link and then browse to another page on my site, the currency stays the one I selected and the string disappears from the URL, now the normal currency switcher can be used.
Is there any code fix I can do to the above code which will display the currency requested but removes the string from the URL? or some other workaround to this problem?
I actually figured out the answer myself. In case anyone else is interested, I will post the solution:
instead of using "currency=EUR" in the URL, use "wcmlc=EUR in the URL, this will change the actual currency selector and will not overrule it. With that everything works.
The code I used:
add_action( 'wcml_client_currency', 'wcmlc' );
function wcmlc( $current_currency ) {
$wcmlc = isset( $_GET['wcmlc'] ) ? esc_attr( $_GET['wcmlc'] ) : $current_currency;
return strtoupper( $wcmlc );
}
Related
I'm figuring out a filter but can't be figured :)
Just want to change PrimaryImageOfPage value with an og:image value in CollectionPage for Taxonomies.
It may be that I have to use woocommerce_structured_data_product to filter here and look for the image $markup['image'] = $image;
I have created some filters, but it isn't woocommerce_structured_data_product I guess.
One of them was like that but it... nah.
add_filter( 'woocommerce_structured_data_product', function( $markup ) {
$markup['image'] = ('rank_math/opengraph/{$network}/image');
return $markup;
});
What am I doing wrong? I have been doing this whole day -_-
If You inspect the code of the class WC_Structured_Data, You would see that woocommerce_structured_data_product filter is executed inside function generate_product_data
WC Complete Source of the class WC_Structured_Data : https://github.com/woocommerce/woocommerce/blob/b88b868ab8919b7c854173098b7d6d4ab227f9ee/includes/class-wc-structured-data.php
$this->set_data( apply_filters( 'woocommerce_structured_data_product', $markup, $product ) );
And generate_product_data function is executed on action woocommerce_single_product_summary
add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ), 60 );
So, your code would work on product single page only and not archive/list pages, such as the taxonomy page.
The feature you want to achieve is not feasible out of the box in WC, You will need to customize yourself.
Logically, structured data on a single product page is feasible due to the fact there is only one product and product information is available, on the other hand, product list has more than one product (you might need to consider like you want to show details of first product) so it is ambiguous and not feasible out of the box for WC.
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 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>';
}
I designed a WooCommerce template for a customer. Now he wants to remove prices and visitors only add what they want to the list. So there will be no payment method. s:
- I can't change plugin or database. All data must remain how they are.
- I need to remove prices from all over the site. where ever they are. Cart, wishlist, single page & etc.
- I need to change payment method to something like submit list.
- at the end after submit list page there must be a contact info page.
please help.
You can hook into wc_price and remove the prices there
function my_filter_wc_price( $price ){
return '';
}
add_filter( 'wc_price', 'my_filter_wc_price' );
Then you can hook into parse_query and redirect the checkout page to the contact page.
function my_parse_query(){
if( is_page( wc_get_page_id( 'checkout' ) ) ){
wp_redirect( '/contact' );
exit;
}
}
add_action( 'parse_query', 'my_parse_query' );
That will stop people from being able purchasing things. Just hiding the price with CSS will not. They will still be able to craft URL's to add to cart, and find the cart / checkout pages.
since I found no other way I decided to do it width CSS and translation.
I removed all prices by display:none;
and I translated all the strings which are about shop and price.
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