Custom query string in Woocommerce external / affiliate product URL - php

I wonder if you guys can help. I do not know php very well.
I would like to add the same extra string on every external product url. The extra string would be like "?prodid=12345" so I do not want to overide the url just add to it. Would this be possible? TIA

This can be done easily with the following hooked function:
add_filter( 'woocommerce_product_add_to_cart_url', 'custom_product_add_to_cart_url', 20, 2 );
function custom_product_add_to_cart_url( $add_to_cart_url, $product ){
if( $product->is_type('external') )
$add_to_cart_url .= '?prodid=' . $product->get_id();
return $add_to_cart_url;
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
You can change in the code $product->get_id(); by a static value or any other dynamic value of your choice.

Related

Override WooCommerce global template quantity-input.php for better quantity input experience on mobile

I want WooCommerce to better handle Quantity field input when using mobile devices. Instead of showing a full keyboard for user to input quantity field, I want mobile browser to show a numeric keypad instead.
I can achieve that by modifying the quantity-input.php from
pattern="<?php echo esc_attr( $pattern ); ?>"
inputmode="<?php echo esc_attr( $inputmode ); ?>"
to
pattern="[0-9]*"
inputmode="numeric"
in
wp-content/plugins/woocommerce/templates/global/quantity-input.php
But obviously modifying the php files of WooCommerce directly is a terrible idea, so I need some guidance on the best practice of such modification.
I have been using a plugin called Code Snippet in Wordpress and it works well. How can I put by code to my site using that?
I have found a post that mentions template files overriding in WooCommerce by using filter here but I am not familiar with that as I am new to Wordpress / WooCommerce
It seems to be the way to go is to use the filterwc_get_template_part and / or woocommerce_locate_template but I don't know how to do that.
Any suggestion is welcome.
You can simply use woocommerce_quantity_input_args dedicated filter hook like:
add_filter( 'woocommerce_quantity_input_args', 'filter_quantity_input_args_callback', 10, 2 );
function filter_quantity_input_args_callback( $args, $product ) {
$args['pattern'] = '[0-9]*';
$args['inputmode'] = 'numeric';
return $args;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Or this instead two filter hooks instead (that will have the same effect):
add_filter( 'woocommerce_quantity_input_pattern', 'filter_quantity_input_pattern_callback', 10, 1 );
function filter_quantity_input_pattern_callback( $input_pattern ) {
return '[0-9]*';
}
add_filter( 'woocommerce_quantity_input_inputmode', 'filter_quantity_input_inputmode_callback', 10, 1 );
function filter_quantity_input_inputmode_callback( $inputmode ) {
return 'numeric';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Show empty subcategories in WooCommerce 3

I tried this code, it shows root categories, but subcategories without products are still hidden.
function hide_empty_categories ( $hide_empty ) {
$hide_empty = FALSE;
// You can add other logic here too
return $hide_empty;
}
add_filter( 'woocommerce_product_subcategories_hide_empty', 'hide_empty_categories', 10, 1 );
This problem can come from some other customizations you have added yourself, some wrong settings, your main theme customizations or some third party plugin.
Something else is altering your the product categories loop as your code correctly enables to show empty product subcategories in Woocommerce and it's the right hook to be used.
It can be simplified with this simple line of code too:
add_filter( 'woocommerce_product_subcategories_hide_empty', '__return_false' );
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Remove structured data from Woocommerce single product pages

I am trying to remove the structured data that Woocommerce adds to the product pages.
I did some research and found that WC_Structured_Data::generate_product_data() generates the structured data markup. It's hooked in the woocommerce_single_product_summary action hook in the woocommerce/templates/content-single-product.php template file.
I tried by adding the following code to the functions.php
remove_action( 'woocommerce_single_product_summary', 'WC_Structured_Data::generate_product_data()', 60 );
So structured data wouldn't be added by Woocommerce, but it doesn't work…
Am I doing something wrong? Is there another way to do what I am trying to achieve?
Instead, you can use dedicated filter hook 'woocommerce_structured_data_product' that is located in WC_Structured_Data for generate_product_data() method nulling the structured data output in single product pages:
add_filter( 'woocommerce_structured_data_product', 'structured_data_product_nulled', 10, 2 );
function structured_data_product_nulled( $markup, $product ){
if( is_product() ) {
$markup = '';
}
return $markup;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I suspect people want to remove the default tabs and they come here after they see the Woocommerce content-single-product.php template. In this template you see that generate_product_data() is hooked with priority 60.
After inspecting the hooks that run on woocommerce_single_product_summary.
You can easily remove the tabs with:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_output_product_data_tabs', 60 );
I think Woocommerce forgot to mention this add_action.
This is how you can remove hooks associated with instantiated object method. You have to find the variable that holds the new Object instance.
In this case the main WooCommerce object is accessible as $GLOBALS['woocommerce'] and it has public property $structured_data which holds an instance of the WC_Structured_Data object.
Hence, to remove the hook in the question you can write this code:
remove_action( 'woocommerce_before_main_content', array( $GLOBALS['woocommerce']->structured_data, 'generate_website_data' ), 30 );
Add to functions.php:
add_action('wp_loaded', function() {
remove_action('woocommerce_single_product_summary', [$GLOBALS['woocommerce']->structured_data, 'generate_product_data'], 60);
});
Unhooks WC_Structured_Data::generate_product_data(). Will not waste resources on generating product data first for no reason and then "nulling" that same generated data a moment later using a filter.

How to remove attributes from product title in woocomerce order view page? [duplicate]

Ever since we upgraded to Woocommerce version 3 our order confirmations are showing huge titles that include the variation detail. I don't like how it looks and it breaks some important functionalities in some custom-made plugins.
Reference: Order Name Showing Variations since update to WC version 3
There is a filter that can be used to disable this data displaying in the title called woocommerce_product_variation_title_include_attribute_name from what I understand. But I have no idea where to apply the filter.
Is there a quick way to apply the filter to change it back to display as it did before?
This filter should work returning a false value for $should_include_attributes first argument in woocommerce_product_variation_title_include_attributes filter hook this way:
add_filter( 'woocommerce_product_variation_title_include_attributes', 'custom_product_variation_title', 10, 2 );
function custom_product_variation_title($should_include_attributes, $product){
$should_include_attributes = false;
return $should_include_attributes;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
It should just work as you expect.
Update: The shorter way is:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
just works too.
A quick gotcha if you're using this filter to remove attributes from e-mail items. It appears that once an item has been written to an order the properties of it will not change.
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
As #freemason_17 pointed out, #LoicTheAztec's answer could potentially hide those details at other places as well so just to be sure I added a condition that would limit this to the cart page alone:
function custom_product_variation_title($should_include_attributes, $product){
if(is_cart()) {
$should_include_attributes = false;
return $should_include_attributes;
}
}
add_filter( 'woocommerce_product_variation_title_include_attributes', 'custom_product_variation_title', 10, 2 );
I use this:
/* -------------------- Remove variation names from title ------------------- */
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );

Changing "return to shop" URL for all languages with WPML plugin

In my WooCommerce Web shop I would like to change the "Return to shop" URL to a Custom URL. I tried to use the code below in the function.php file of my active theme, but it doesn't work.
On my website, I have five active languages managed by WPML commercial plugin. It also runs a script which makes sure that visitors from these countries are redirected to their own language.
/**
* Changes Return to Shop button URL on Cart page.
*
*/
function wc_empty_cart_redirect_url() {
return 'http://pacsymposium.com/';
}
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
How can I make this working to get the current language shop link?
Thanks.
Update2: In your code, you need to use:
WooCommerce wc_get_page_id() function to get the WooCommerce shop page ID.
WPML wpml_object_id filter hook to get the current language translated page ID for shop.
WooCommerce wc_get_page_permalink() that is used by the filter hook itself (see HERE)
With that material, you can get the current translated link of the shop (or any other link).
So your code is going to be:
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
function wc_empty_cart_redirect_url() {
// Getting the shop ID
$shop_id = wc_get_page_id( 'shop' );
// Getting the current language ID for the shop page
$current_lang_id = apply_filters( 'wpml_object_id', $shop_id, 'page', TRUE );
// Getting the post object for the ID
$post = get_post($current_lang_id);
// Getting the slug from this post object
$slug = $post->post_name;
// We re-use wc_get_page_permalink() function just like in this hook
$link = wc_get_page_permalink( $slug );
return $link;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Finally I tested and it works…

Categories