Usable woocommerce template for custom taxonomy - php

I created a custom taxonomy book-author for my Woocommerce store, and now I'm trying to conjoint an archive template for it to display frontend like normal Woo archive. Yesterday I found this code, which helped me bring the taxonomy out of the 404 error when clicked, but it returned a shop page with No product notice (though I'd clicked on one of the existed taxonomies).
The point is, book-author taxonomy is a mother of small tags, or "authors", so I need to fix this tag or find a way to make it universal to the mother book-author and all its kids/authors.
add_filter('template_include', 'team_set_template');
function team_set_template( $template ){
if(is_tax('book-author')) :
$taxonomy = 'book-author';
$term = get_query_var($taxonomy);
$prod_term = get_terms($taxonomy, 'slug='.$term.'');
$term_slug = $prod_term[0]->slug;
$t_id = $prod_term[0]->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$term_meta['bookauthor_access_pin'];
wc_get_template( 'archive-product.php' );
else :
wc_get_template( 'archive-product.php' );
endif;
}
I've tried copying archive-product.php, renaming it taxonomy-book-author.php and putting it in my child theme folder. This seems to be a more better approach, but there was no result - still 404.
The reason why book-author is a tag, not a category because there is no hierarchy for an author. And I know there's plugin for this (Toolset), but they upgraded there free version to paid ones so I'm trying to find a more manual and permanent way.
Thank you in advance, guys.

There is many errors just when reviewing and trying your code…
Note: A filter hooked function needs always to return something.
In your code:
- get_query_var($taxonomy) will return always a term "slug"
- $term_meta['bookauthor_access_pin']; is not usefull and I really don't know what is for.
You say "The reason why book-author is a tag, not a category"… If you have created a custom taxonomy book-author, this can't be a product category or either a product tag (woocommerce custom taxonomies)…
So you should try the following code instead (without any guaranty, as you don't provide all your related code, and this can't be tested):
add_filter('template_include', 'team_set_template');
function team_set_template( $template ){
$taxonomy = 'book-author';
if( ! is_tax( $taxonomy ) ) return $template;
$term_slug = get_query_var($taxonomy);
if ( empty( $term_slug ) ) return $template;
$term = get_term_by('slug', $term_slug, $taxonomy );
if ( is_wp_error( $term ) ) return $template;
$term_id = $prod_term->term_id;
$term_meta = get_option( 'taxonomy_'. $term_id );
// $term_meta['bookauthor_access_pin']; // ???
return wc_locate_template( 'archive-product.php' );
}

Related

Show custom AND standard products atrributes on Product Single page WooCommerce

I have searched all over for this, and I have found a great code to get the standard product attributes displayed, but the custom ones (that do not start with pa_) are not being displayed.
First things first, the setup:
I have created a product single template with Elementor and normally I can just add the module additional information and I am done. I've done it before. :)
But now I have created a lay-out where these product attributes are being displayed in an accordion in which you cannot place this module.
So after a loooooong search online I have found a snippet made by helgatheviking to create a shortcode to call all the product attributes:
/**
* Attributes shortcode callback on productpage.
*/
function so_39394127_attributes_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
), $atts );
// is pass an attributes param, turn into array
if( is_string( $args['attributes'] ) ){
$args['attributes'] = array_map( 'trim', explode( '|' , $args['attributes'] ) );
}
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( ! empty( $args['attributes'] ) ){
foreach ( $args['attributes'] as $attribute ) {
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a clickable list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= get_the_term_list( $product->get_id(), $taxonomy, '<li class="prod-attr">' . $attribute_label . ': ' , ', ', '</li>' );
}
}
// if we have anything to display, wrap it in a <ul> for proper markup
// OR: delete these lines if you only wish to return the <li> elements
if( $html ){
$html = '<ul class="product-attributes">' . $html . '</ul>';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );
After adding the shortcode [display_attributes] in the accordion, I get all the standard WC attributes shown that have been created via the standard way: products -> attributes -> create new. So that is great!
But not the custom attributes, that you create inside the productpage itself -> Product data -> custom product attribute -> add (screenshot of what I mean: https://gyazo.com/f2619c7f11d295f4897b38f08d76453b)
Screenshot product page custom attributes are not being shown on front: https://gyazo.com/4c126f7082c6a856d1cf30b712daa37f
I think this is because that these custom product attributes do not contain the pa_ part in front of the meta value like for instance pa_type-speaker (self-created standard attribute).
How can I get the custom attributes also shown? I have tried to remove the pa_ from the code above, but that does not work and I cannot find what to do next. Can you guys help me out here? thx!
Elementor has helped me out on this one!
The solution:
Open the product single template page, Add the additional information module in a row. Create a global widget of this object. Add the shortcode of this global widget in the tabbed module and your done. :)

Woocommerce Multiple single product templates using single-product.php to redirect

I've been pulling my hair out with this all day, please forgive the short description, I just need to validate my sanity!!
As the title says, I'm trying to create two or three different single-product layouts within woocommerce. The minimum is trying to achieve would be to have multiple single-product folders each with their own name and configurations.
No matter which way I try to override the single-product.php and make this file use logic to check for the product_cat and give out templates accordingly, I either the page not loading or what I write is skipped over and the default is loaded.
So far I've been through the following methods multiple times, trying to piece together what may be outdated code or otherwise causing all the fuss:
WooCommerce - How to create multiple single product template based on category?
Woocommerce single product - template by categories
Creating a different template file for certain Product Categories - Wordpress/Woocommerce?
I was more hoping someone may know something about this that I'm obviously missing as there are many articles out there on what to try and most claim success but I'm unable to do so.
[Update] using template_include code from #helgatheviking
No success just yet but here's where I'm up to;
File structure
team-shops is the category I'm trying to get
/mytheme/woocommerce/single-product.php - no changes
/mytheme/woocommerce/content-single-product.php
/mytheme/woocommerce/single-product-team-shops.php - changed line 37 to<?php wc_get_template_part( 'content', 'single-product-team-shops' ); ?>
/mytheme/woocommerce/content-single-product-team-shops.php - added additional id to #product-id (line 39)
/mytheme/woocommerce/single-product-team-shops/ folder with all single product files to change.
As I said above this isn't working but hopefully with what I've provided the problem may be more obvious.
Thanks again for any help :)
[Think I've got it]
Ok so I think I've something that works, at least for now it seems to, still have some further testing to do but any thoughts more than welcome, this is what I've got so far along with a single-product-team-shops folder in my theme
add_filter( 'woocommerce_locate_template', 'so_25789472_locate_template', 10, 3 );
function so_25789472_locate_template( $template, $template_name, $template_path ){
$term_id = 2854;
$taxonomy_name = 'product_cat';
$term_children = get_term_children( $term_id, $taxonomy_name );
foreach ( $term_children as $child ) {
// on single posts with mock category and only for single-product/something.php templates
if( is_product() && has_term( $child, 'product_cat' ) && strpos( $template_name, 'single-product/') !== false ){
// replace single-product with single-product-mock in template name
$mock_template_name = str_replace("single-product/", "single-product-team-shops/", $template_name );
// look for templates in the single-product-mock/ folder
$mock_template = locate_template(
array(
trailingslashit( $template_path ) . $mock_template_name,
$mock_template_name
)
);
// if found, replace template with that in the single-product-mock/ folder
if ( $mock_template ) {
$template = $mock_template;
}
}}
return $template;
}
Use a single-product-custom.php template for any product in the "custom" category:
add_filter( 'template_include', 'so_43621049_template_include' );
function so_43621049_template_include( $template ) {
if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-custom.php';
}
return $template;
}
NB: If you use the same action hooks in your single-product-custom.php template you will get the same look as the default single-product.php. You could 'rename' all the hooks and then could add existing functions (such as those for add to cart buttons, etc) to the new hooks in order to achieve a totally custom look.

Add fields in Json Response of Posts REST API

I want to add a custom field in a response of posts, but when I add new fields after a while it is deleted from the file what I do.
The file that I modify its class-wp-rest-posts-controller.php and I add the new field in prepare_item_for_response function
I add this lines, that works well, but it's deleted after a time
foreach ( $taxonomies as $taxonomy ) {
$base = "other";
$terms = get_the_terms( $post, $taxonomy->name );
$datres = $terms ? array_values( wp_list_pluck( $terms, 'name' ) ) : array();
$data[ $base ] = implode(" ", $datres);
}
Any idea why this happens?
In WordPress you shold not edit core files or core plugins, your changes are deleted becuase the files are updated. The right way do to it is thrugh Hooks and functions from a child theme / your own plugin.
WP Child theme
WP Rest api custom end point docs

woo commerece short codes not working on all posts

I have created a short code to display short description in woo commerce but it is not working on all posts. It is displaying the short description on some posts and not on others.
Function to create that short code in functions.php
function product_shortdesc_shortcode( $atts ){
// use shortcode_atts() to set defaults then extract() to variables
extract( shortcode_atts( array( 'id' => false ), $atts ) );
// if an $id was passed, and we could get a Post for it, and it's a product....
if ( ! empty( $id ) && null != ( $product = get_post( $id ) ) && $product->post_type = 'product' ){
// apply woocommerce filter to the excerpt
echo apply_filters( 'woocommerce_short_description', $product->post_excerpt );
}
}
// process [product_shortdesc] using product_shortdesc_shortcode()
add_shortcode( 'product_shortdesc', 'product_shortdesc_shortcode' );
The way i am getting the data in my single.php file
$custom = get_post_custom(get_the_ID());
$my_custom_field = $custom['woo_id'];
foreach ( $my_custom_field as $key => $value ) {
echo do_shortcode('[product_shortdesc id='.$value.']');
}
PS: in my normal post i have a custom field which has the value of product id of the product in woo commerece.
Your issue is that you are expecting shortcodes to function which no longer exist. On new installs, these pages won't be created, but if you are updating you may already have those pages in place.
Although the upgrade script does attempt to trash them for you, this might not have happened if they were customised. Delete them. Delete edit-account and change password, then go to your 'my account' page and click the change password/edit account links. You'll be taken to and endpoint which offers the same functionality.
Thanks
Short Code must not echo code instead return the things that needs to be rendered
Change this
echo apply_filters( 'woocommerce_short_description', $product->post_excerpt );
to
return apply_filters( 'woocommerce_short_description', $product->post_excerpt );

How do I make an archive template for custom woocommerce taxonomy?

I have created a new taxonomy for products on woocommerce.
Now, when I go to the taxonomy view and it shows 404.
how do i make it show the products just like in any other regular category on the site?
tnx ahead :)
I tried this code and it works for me.
I created a taxonomy name 'team' and here is my code.
add_filter('template_include', 'team_set_template');
function team_set_template( $template ){
if(is_tax('team')) :
$taxonomy = 'team';
$term = get_query_var($taxonomy);
$prod_term = get_terms($taxonomy, 'slug='.$term.'');
$term_slug = $prod_term[0]->slug;
$t_id = $prod_term[0]->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$term_meta['team_access_pin'];
wc_get_template( 'archive-product.php' );
else :
wc_get_template( 'archive-product.php' );
endif;
}
Note: I used this code in functions.php file. If anyone wants to use this code. Please replace the name 'team' with your custom taxonomy name.
Thanks,
Satya
This problem occurs when WordPress does not find any template for the custom taxonomy.
1.The solution is to create a WooCommerce template named as "taxonomy-tax_name.php" (ideally in a child theme).
2.For this you could copy the contents of an archive template like archive-product.php and modify it by doing a WP_Query() with arguments for post_type=>"product" and add a "tax_query" with the newly made custom taxonomy slug.
3.This would get you the required products and now can be displayed as required.

Categories