Woocommerce - How To Link to Product Variation? - wordpress

I am trying to find a way to have a link to a specific product variation.
I thought I had it solved with this plugin...
https://wordpress.org/plugins/woocommerce-direct-variation-link/
However the I have 2 word product attribute names and can't seem to get it to work? I put _, -, & and + between the 2 words but not go.
Here is the doc...In my case I would have "color style" instead of just "color"
mysite.com/product/happy-ninja/?color=blue
mysite.com/product/happy-ninja/?color=blue&size=small (additional variations should be separated by '&')
mysite.com/product/happy-ninja/?color=blue+green (where the variation value is "Blue Green" with the space replaced by a '+')

Further to the last poster, I couldn't get his code to work properly, but I did discover that WooCommerce generates these for the Cart most of the time:
Visit a variation product yourself
Add a variation to the Cart
Open your cart and hover over the item --> the link in your status bar (or right click > Copy Link Address) is it!
It should have a suffix like this:
http://website.com/product/product-name/?attribute_colours=Grey
Note that colours is lowercase because it's the slug while Grey is the actual name for the Variation.
For spaces in your Variation name, use a + instead of the space.
For selecting multiple variation attributes, I believe you can just start with & and spit the ?attribute_[slug]=Variation again.

I think many will search for a solution to link specific variations by a url parameter so here is a longer answer for that topic.
As many others mentioned here, you can access any variation with the attribute parameter. The way you choose an variation on the product details page is an select menu. The name attribute of that select is the parameter name and the options value attribute is the parameter value. Here is an example:
If you want to link the highlighted variation your link will be like https://example.com/some_product/?attribute_pa_size=small
Of course you can add multiple parameters like https://example.com/some_product/?attribute_pa_size=small&attribute_pa_color=red
The thing is, using dev tools to dig out these informations is not a very convenient solution. Down below is a code snippet you can add to your themes functions.php (or inside a simple one-file-plugin) that shows a variation-permalink in the wordpress admin of the specific product variation tab/accordeon.
add_action( 'woocommerce_product_after_variable_attributes', 'gdy_add_wc_varition_permalink', 100, 3 );
function gdy_add_wc_varition_permalink( $loop, $variation_data, $variation ) {
echo '' . __( 'Permalink for this variation' ) . '';
}

I have tried so many solutions and plugins myself, but i found a way to easily do it without a plugin. I'm not sure if this worked back in oktober 2015, but it does work now.
I have explained it in this youtube video https://www.youtube.com/watch?v=Y_hMI4bXN6A
What you want to do is add something like '/?attribute_color=red' behind your product.
website.com/shop/product1/?attribute_color=red
website.com/shop/product1/?attribute_length=10m
However what i found out by trial and error is what matters is the type of attribute you use.
You can add a attribute in 2 ways, a pre-made one (text/select) or a custom attribute. Linking directly with this method only works if you create a custom product attribute.
For it to work you have to pay attention if you used capitol letters or not. For the name of the custom product attribute it wont matter if you use capitols in the backend (as long as you dont use theme in the url). For the values however you can only use normal letters and no capitols, both in the backend as in the url itself.
Example
Name: Color
Values: red | Black, green
website.com/shop/product1/?attribute_color=red > will work
website.com/shop/product1/?attribute_Color=red > will not work
website.com/shop/product1/?attribute_color=Red > will not work
website.com/shop/product1/?attribute_color=Black > will work
website.com/shop/product1/?attribute_color=black > will not work
I have explained it in this youtube video https://www.youtube.com/watch?v=Y_hMI4bXN6A

You can just:
$variation_product_url = $variation_product->get_permalink();
WooCommerce handles the rest; adding the correct parameters to the url.

I am using a plugin Variation Swatches for WooCommerce so I am not sure if this is core wordpress / woocommerce functionality or not.
What I did was open one of my products and in ""Product data" went to "Linked Products" tab and linked one of my variations to the product. Updated and went to the product page. After clicking on the linked product I got url that looks like this:
example.eg/product/product-name-slug/?attribute_pa_kids-size=27&attribute_pa_color=navy
following ?attribute_pa_ is my attribute name slug (kids-size) and following = is slug for the single attribute slug (27, 28, blue, red, navy ...).
As you can see it works for multiple attributes in url.
If you don't know your slugs, just link your variation product to another and copy the link.

You can create direct links to variants in any case, meaning both with variants using custom attributes and variants using predefined attributes.
There is just a slight difference in the URL parameter you need to append to the URL.
Examples:
Custom attribute variant URL:
domain.com/shop/product-x/?attribute_color=blue
Predefined attribute variant URL:
domain.com/shop/product-x/?attribute_pa_color=blue
The only difference is "pa_".
Note: The question is quite old, but I got here and didn't find a full answer, so figure I'd add it for others to benefit from.

I have not tested this, but try replacing your space in your variation name to %20 which is the url-encoded version of the space character.

Another easy way to get the links without taking time to add then copy from cart is to export the product information in XML format then searching for your product in XML file and copying the link needed from there.
You can get the XML format from wordpress admin->tools->export->variables.
Open the file in sublime text 3 program and search by title or post ID.. the URL for variant will be easily found. just found it faster than adding to cart.

Simply look at your HTML code of single product page, find your product variable select combo ID and add at he end of URL: ?attribute_YOUR_ID=YOUR_VALUE
You can use combinations if you have multiple variable options, separate them with &
example:
website.com/shop/product1/?attribute_id_of_variable_combo=value_of_combo

Little JS snippet to automatically update the WooCommerce single product page URL with the needed parameters for the active variations' selection.
Each time the user changes the variation selection, the URL is updated with the new parameters, so on hard refresh the selected variation is displayed.
Also, fast way to get the direct URL to the selected variation.
Paste it into your browser console and run it. Or use it in your theme/plugins.
const els = document.querySelectorAll('.variations select')
// add el.name and el.value as query parameters to the URL
function updateURL(el) {
const url = new URL(window.location.href)
url.searchParams.set(el.name, el.value)
window.history.pushState({}, '', url.href)
}
// update the URL when the user changes the select
els.forEach(el => el.addEventListener('change', () => updateURL(el)))
Paste the below into a new entry of your browser's bookmarks bar and click on it on the given WC single product page:
javascript:(function()%7B%2F**%0A%20*%20Little%20JS%20snippet%20to%20automatically%20update%20the%20WooCommerce%20single%20product%20page%20URL%20with%20the%20needed%20parameters%20for%20the%20active%20variations'%20selection.%0A%20*%20%0A%20*%20Each%20time%20the%20user%20changes%20the%20variation%20selection%2C%20the%20URL%20is%20updated%20with%20the%20new%20parameters%2C%20so%20on%20hard%20refresh%20the%20selected%20variation%20is%20displayed.%0A%20*%20%0A%20*%20Also%2C%20fast%20way%20to%20get%20the%20direct%20URL%20to%20the%20selected%20variation.%0A%20*%20%0A%20*%20Paste%20it%20into%20your%20browser%20console%20and%20run%20it.%20Or%20use%20it%20in%20your%20theme%2Fplugins.%0A%20*%2F%0A%0Aconst%20els%20%3D%20document.querySelectorAll('.variations%20select')%0A%0A%2F%2F%20add%20el.name%20and%20el.value%20as%20query%20parameters%20to%20the%20URL%0Afunction%20updateURL(el)%20%7B%0A%20%20%20%20const%20url%20%3D%20new%20URL(window.location.href)%0A%20%20%20%20url.searchParams.set(el.name%2C%20el.value)%0A%20%20%20%20window.history.pushState(%7B%7D%2C%20''%2C%20url.href)%0A%7D%0A%0A%2F%2F%20update%20the%20URL%20when%20the%20user%20changes%20the%20select%0Aels.forEach(el%20%3D%3E%20el.addEventListener('change'%2C%20()%20%3D%3E%20updateURL(el)))%7D)()%3B
https://gist.github.com/devidw/2f674f273fb556cb48c82dc1b70aff25

Related

woocommerce display tag description on product page

I looked around for a while today for a way to display a tag description on the product page. But could not find a close answer to my issue of displaying a tag description.
I know if you have more than one tag on a page and it pulls in all tag descriptions would not be good for SEO or page look but I believe we are only pulling in one tag that contains a description, the rest are generic tags without a description.
Where should I be looking at how this can be done? I'm sure this is a common woocommerce mod and I'm not using the correct woocommerce terminology. Here is the info currently being displayed on our product page.
----current----
Tag - Rose Quartz
----desired-----
Tag - Rose Quartz
This beautiful pink stone is well-known for the loving powers that it
gives off. This is the stone of love, but more crystal experts see it
as self-love, self-esteem, and acceptance of one’s true self. Rose
quartz also helps to ward off any resentment that the wearer has in
their lives that have built up over time. This stone brings strength to
the heart.
So I figured out how this can be done and would like to share my findings. You will need to first have a way to add snippets to your page. I used a plugin called XYZ PHP Code, this plugin allows me to create PHP snippets to be used in elementor page builder.
I created a new snippet in XYZ named "tagdesc" and used the following code to display tag descriptions.
<?php
$posttags = get_the_terms( get_the_ID(), 'product_tag' );
if ($posttags) {
foreach($posttags as $product_tag) {
echo $product_tag->name;
echo $product_tag->description;
}
}
?>
Save and use your snippet in my case it was called [xyz-ips snippet="tagdesc"].
This will display all the tag names and descriptions of that product. In my case I removed (echo $product_tag->name;) as I did not need to show the tag name. This works in our case as only one tag has a description, I feel a better solution would be pulling out a description from a single product attribute using a similar method but have not found a solution that works.

add custom field template to theme admin page

in WP admin how to add the custom field template plugin to a theme page?
it automatically shows in posts and pages but i want this in the theme page. the theme am using is from iwak "creations" portfolio page.
what files do i need to modify to add this?
It's very hard to say what you need to modify without being able to look at the code. Being a premium theme, we can't just download it and take a look.
Having said that, the theme may use the WordPress custom post type functionality. Search the code for a call to the register_post_type function. If it's used, you may be in luck. Either
add 'custom-fields' to the supports argument in that call, or
call add_post_type_support after the post type is registered. The $post_type parameter will be the first value passed to the register_post_type function, and the $supports parameter will be 'custom-fields'.
Daniel, are you using this Custom Post Type Plugin - http://wordpress.org/extend/plugins/custom-field-template/screenshots/? I've used it before, and the guy who created it is Japanese, so his personal page isn't very useful as far as support for english goes.
I had some trouble with this at first. But what I think you're trying to do is add the custom fields to your new pages you've created, correct?
It's not very straightforward, but once it works it's pretty awesome.
Basically, when you set up the plugin you create these different "Custom fields," right? Well part of that should look like this:
[Custom Field Name]
type = textarea
rows = 4
cols = 40
tinyMCE = true
htmlEditor = true
Ok, so once you've created those "Custom fields" keep note of the part in brackets. You'll add this to your template pages:
<?php getCustomField('Custom Field Name'); ?>
Now when you enter the info in the pages or posts, the content should appear as you've entered it.

Wordpress, is there a hook for insert/edit link in post page?

I am trying to add http:// in the post link dialog box if there is not already added. I tried with filter
add_filter('pre_link_url', 'add_http_link_url');
It didn't work. Does anybody know how to do that ?
Doesn't Wordpress automatically add the "http://" there by default?
Maybe this plugin would help?
wordpress.org/extend/plugins/auto-hyperlink-urls/
EDIT
found this on http://betterwp.net/wordpress-tips/make-links-clickable/
a function named make_clickable() that can be found in wp-includes/formatting.php.
make_clickable() filters the comment_text hook with this:
add_filter( 'comment_text', 'make_clickable', 9 );
Since it is that simple, let’s try adding the same filter to our post contents and see if it works
add_filter( 'the_content', 'make_clickable', 12 );
The priority of 12 as used above simply tells WordPress to make links clickable for post contents after shortcodes are parsed (which is at priority 11). If you don’t like such behaviour, just change 12 to any number you want. You should take a look at wp-includes/default-filters.php to choose an appropriate priority for make_clickable().
Being that awesome, however, make_clickable() has a limitation, which you can clearly see in this clickable link: http://codex.wordpress.org/Function_Reference/make_clic ... _clickable.
See the full stop punctuation mark also included in the link, thus making it broken? To avoid such behaviour you must always have one space plus another character after a plain link, or in other words, never put a plain link like that at the end of a paragraph. In case you must, just make the link clickable the normal way .
Of course if you don’t like your visitors to be able to post links that way you can easily remove the filter using:
remove_filter('comment_text', 'make_clickable', 9);
hope this helps, sorry i had to remove the first link as i can only post 2 links till i get my rep up :)

Wordpress - How can I create my own template outside of the expected hierarchy of templates and feed a query to it?

BACKGROUND
I have used WordPress custom post types to create a newsletter section for my website. Newsletters consist of Articles (dm_article), which are grouped by the Issues taxonomy (dm_issues).
1) I have created an index of all of my newsletter Articles. I am using a template called taxonomy-dm_issues.php to loop within a selected Issue and display each Article's title, excerpt and a link to the full content, which is managed by single-dm_article.php. This is working great.
2) I would also like to create second taxonomy-based template for Issues. This is going to act as a print-friendly option: "print entire newsletter". I would like the template to loop through and display each Article title, excerpt, and long description. Some of the look and feel will also be different.
For #2, let's assume I've created a template called print-dm_issues.php. It currently looks identical to taxonomy-dm_issues.php, except it has the additional long description data for each Article and contains some different styling.
I want to setup this "print friendly" option without making the WordPress admin have to jump through any hoops when Issues and Articles are created. I also do not want to have to create a new template each time a new Issue is created.
CORE QUESTION:
What I am asking for may boil down to this: How can I create my own WordPress template outside of the expected hierarchy of templates and feed a query to it? Do note I am using the "month and name" common permalink structure, so I'll have to muck with my htaccess.
ALTERNATIVES:
1) My fallback is to have taxonomy-dm_issues.php contain the information for both variations and use style to handle the different view states. I know how to do this. But, I'd rather not do this for sake of load times.
2) Using Ajax to fetch all of the Article long descriptions (the_content()) with a single click is an option, but I don't know how.
3) ???
With or without clean URLs, you can pass variables based on your taxonomies through the links query string if you want to only return a single taxonomy term and style the page differently depending on the term.
$taxonomyTerm = $_GET['dm_issues'];
$args=array(
'post_type' => 'dm_article',
'dm_issues' => $taxonomyTerm,
'post_status' => 'publish',
);
There is reference to this int he Wordpress 'query_posts' documentation by passing variable into your query parameters: http://codex.wordpress.org/Function_Reference/query_posts#Example_4
For instance in the link below, the title is generated based on the sting in the URL.
http://lph.biz/areas-we-serve/service-region/?region=Conestoga
You can set up a parameter that will return a default value if the page is reached without the variable being defined. See below:
if (empty($taxonomyTerm)) {
$taxonomyTerm = 'Default Value';
}
You can create a separate page template. Define the template name at the top of your PHP document:
<?php
/*
Template Name: Printed Page Template
*/
Place your custom query, including all of the content that you need output in this page template... In your WP admin, create a new blank page and assign your new 'Printed Page Template' template to this page. Save it and view the page.

How do you remove the default title and body fields in a CCK generated Drupal content-type?

When you create a new content type in Drupal using the Content Creation Kit, you automatically get Title and Body fields in the generated form. Is there a way to remove them?
If you're not a developer (or you want to shortcut the development process), another possible solution is to utilize the auto_nodetitle module. Auto nodetitle will let you create rules for generating the title of the node. These can be programmatic rules, tokens that are replaced, or simply static text. Worth a look if nothing else.
To remove the body edit the type, expand "Submission form settings" and put in blank for body field label. For title you can rename it to another text field. If you really have no need for any text fields you can create a custom module, say called foo, and create function foo_form_alter() which replaces $form['title'] with a #value when $form['type']['#value'] is your node type.
No need to install anything:
when editing the content type, press "Edit"
(on the menu of Edit | Manage fields | Display fields )
click on the Submission form settings
on the Body field label:
Leave it blank, it would remove the Body field.
If you're not a developer (or you want
to shortcut the development process),
another possible solution is to
utilize the auto_nodetitle module.
Auto nodetitle will let you create
rules for generating the title of the
node. These can be programmatic rules,
tokens that are replaced, or simply
static text. Worth a look if nothing
else.
And to add on to William OConnor's solution...
The module is poorly documented unfortunately. It's really only effective if you use PHP with it in my opinion. Check off the "Evaluate PHP in Pattern" and type into the "Pattern for the title" field something like:
<?php echo $node->field_staff_email[0]['email']; ?>
or:
<?php echo $node->field_staff_name[0]['value'] . '-' . gmdate('YmdHis'); ?>
...where I had a field with an internal name of "field_staff_email" and was using the CCK Email module -- thus the 'email' type was used. Or, I had a field with an internal name of "field_staff_name" and was just an ordinary text field -- thus the 'value' type was used. The gmdate() call on the end is to ensure uniqueness because you may have two or more staff members named the same thing.
The way I discovered all this was by first experimenting with:
<?php print_r($node); ?>
...which of course gave crazy results, but at least I was able to parse the output and figure out how to use the $node object properly here.
Just note if you use either of these PHP routines, then you end up with the Content list in Drupal Admin showing entries exactly as you coded the PHP. This is why I didn't just use gmdate() alone because then it might be hard to find my record for editing.
Note also you might be able to use Base-36 conversion on gmdate() in order to reduce the size of the output because gmdate('YmdHis') is fairly long.
The initial answers are all good. Just as another idea for the title part... how about creating a custom template file for the cck node type. You would copy node.tpl.php to node-TYPE.tpl.php, and then edit the new file and remove where the title is rendered. (Dont forget to clear your cache).
Doing it this way means that every node still has a title, so for content management you aren't left with blank titles or anything like that.
HTH!

Resources