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 );
Related
I need to move certain articles on my website, all of them, to another unique category. I am using specific phrases which are at bottom of every article, but after code execution, only two articles are moved to a new category, not all of them.
Also when I search through all posts using the native Dashboard-All posts option, it returns only those two articles, not all of them. I assume that is because the text is not stored in the database because it shows only when the post renders itself on the front end.
How can I include dynamically created text in my code?
My code, just for reference:
<?php
// Move to the category Breaking News.
$target_category_id = 1982;
// Get all posts that contain the search phrase from Syndication.
$posts = get_posts(
array(
's' => 'and has been published here with permission',
'post_status' => 'any',
'posts_per_page' => -1, // This will retrieve all posts.
)
);
// Loop through the posts and move them to the target category.
foreach ( $posts as $post ) {
// Remove the post from any existing categories.
$current_categories = wp_get_post_categories( $post->ID );
wp_remove_object_terms( $post->ID, $current_categories, 'category' );
// Add the post to the target category.
wp_set_post_categories( $post->ID, array( $target_category_id ), true );
}
That specific phrase is created dynamically because I am using the WP Broadcast plugin to add text at the bottom of every broadcasted article, but I am not sure if that phrase is stored in a database or that text is dynamically populated (created) every time post is rendered on the frontend.
This is the code I am using to generate a message (and has been published here with permission) at the bottom of every article:
<?php
function broadcasted_from() {
// Check that Broadcast is enabled.
if ( ! function_exists( 'ThreeWP_Broadcast' ) ) {
return;
}
// Load the broadcast data for this post.
global $post;
$broadcast_data = ThreeWP_Broadcast()->get_post_broadcast_data( get_current_blog_id(), $post->ID );
// This post must be a child. Check for a parent.
$parent = $broadcast_data->get_linked_parent();
if ( ! $parent ) {
return;
}
// Fetch the permalink
switch_to_blog( $parent['blog_id'] );
$blog_name = get_bloginfo( 'name' );
$permalink = get_post_permalink( $parent['post_id'] );
restore_current_blog();
// And now assemble a text.
$r = sprintf( 'This article appeared in %s and has been published here with permission.', $permalink, $blog_name );
return $r;
}
add_shortcode( 'broadcasted_from', 'broadcasted_from' );
add_filter(
'the_content',
function( $content ) {
// Get the broadcast from the text.
$sc_text = do_shortcode( '[broadcasted_from]' );
// Add the text to the content.
$content .= $sc_text;
// Return the expanded content.
return $content;
}
);
When a new Wordpress post is being created using post category "A" (or "B" or "C"), I need to automatically check the Woocommerce-Memberships Disable restrictions checkbox with a PHP hook. This will allow all viewers unrestricted access to view the post.
checkbox picture[1]
I have tried adding an action to the "save_post" hook to add the "_wc_memberships_force_public" value of "yes" to the {wp_prefix}_post_meta table and the checkbox will show as checked because of this, however, when viewing the post it is still restricted. When I remove the action (by commenting it out from my functions.php file) and check the Disable restrictions checkbox manually before saving the post, the post can be viewed without restriction (as expected).
add_action( 'save_post', 'action_save_post_force_public', 99, 3);
function action_save_post_force_public($post_id, $post, $is_update){
$already_forced = get_post_meta( $post_id, '_wc_memberships_force_public', $single=true );
if ( 'yes' === $already_forced ){ return; }
$categories = get_the_category( $post_id );
if ( empty( $categories ) ){ return; }
$force_public_slugs = array('A', 'B', 'C');
foreach ( $categories as $term ){
if ( in_array( $term->slug, $force_public_slugs ) ){
update_post_meta( $post_id, '_wc_memberships_force_public', 'yes' );
break;
}
}
}
I expect all viewers to have unrestricted access to view the post content whether this box is checked automatically or manually. Instead it only works when manually checking the box.
Instead of using update_post_meta( $post_id, '_wc_memberships_force_public', 'yes' );, try this:
// Disable post restrictions (Woocommerce Membership)
$wc_membership = \WC_Memberships::instance();
$wc_membership->get_restrictions_instance()->set_content_public($post);
It works well for me ;)
You can find an example of this piece of code in the file woocommerce-memberships/includes/admin/class-wc-memberships-admin.php
In Woocommerce, I am using YITH WooCommerce Barcodes and QR Codes plugin and I would like to use a custom field as a value for an argument in this shortcode. Here is the related documentation.
That is what I would like (whereCUSTOMFILEDVALUEis the value of the custom field):
[yith_render_barcode value="CUSTOMFILEDVALUE" protocol="CODE39"]
Is it possible to include a custom field value in this kind of short code? How this can be done?
Any help will be appreciated.
To do that you can embed an existing Shortcode inside a custom Shortcode function. The following code is an example based on other similar functional answers.
In this code I get the post id of the pages, posts or custom posts. You can specify a post ID using the shortcode id argument as in the original shortcode.
The code:
function custom_yith_render_barcode( $atts ) {
// Shortcode attributes
$atts = shortcode_atts( array(
'id' => '0', // Product ID
'hide_if_empty' => '1',
'value' => '',
'protocol' => 'EAN8',
), $atts, 'render_barcode' );
global $post;
if( '0' === $atts['id'] && $post && is_object($post) )
$id = $post->ID;
elseif( $atts['id'] > 0 )
$id = $atts['id'];
$hide = $atts['hide_if_empty'];
$value = get_post_meta( $id, $atts['value'], true ) ? get_post_meta( $id, $atts['value'], true ) : $atts['value'];
$protocol = $atts['protocol'];
return do_shortcode( "[yith_render_barcode id='$id' hide_if_empty='$hide' value='$value' protocol='$protocol']" );
}
add_shortcode('render_barcode', 'custom_yith_render_barcode');
Code goes in function.php file of your active child theme (or active theme). It should works.
USAGE -
Below the meta_key has to be replaced with the meta key of your custom field. All other YITH Shortcode arguments are unchanged and available. Only value argument is used to pass the custom field meta key, allowing to get the custom field value in embedded YITH shortcode.
1) In the Wordpress page or post editor:
[render_barcode value="meta_key" protocol="CODE39"]
2) In PHP code:
echo do_shortcode( "[render_barcode value='meta_key' protocol='CODE39']" );
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' );
}
I'm trying to edit a field in the meta data of a post before it is being displayed on the screen.
I have been looking at the 'load-post.php' hook, but this is called before the post is loaded (if I've understood that correctly), so the post id and meta data are null.
I've tried other hooks, but I haven't been able to make this work.
The following post meta field needs to be changed before it is displayed on the edit page.
$post_price = get_post_meta(get_the_ID(), 'price', TRUE);
Example: Price = 10 in the database, but I want it to be Price = 15 when it is displayed on the post edit page.
Any links, tips and ideas are much appreciated. :)
Edit:
My current solution:
add_action('load-post.php','calculate_price');
function calculate_price(){
$post_id = $_GET['post'];
//get price from post by post_id and do stuff
}
Is this the correct way?
The best hook I found is load-post.php using $current_screen.
For Woocommerce product, that works :
add_action('load-post.php', "calculate_price" );
function calculate_price( ){
global $current_screen;
if( is_admin() && $current_screen->post_type === 'product' ){
$post_id = (int) $_GET['post'];
$post = get_post( $post_id );
//Do something
}
}
EDIT: okay I thought you just need to work with ID of post. If you need to change post objects (already loaded from db and ready to be printed), you can use 'the_post' instead. Since you just need to access to post id, I would do something like this:
function my_the_post_action( $post ) {
$screen = get_current_screen();
if( is_admin() && $screen->parent_base == 'edit' && get_post_type() == 'product' ) {
$post_id = $post->ID;
$price = (int) get_post_meta( $post_id, 'price', true );
update_post_meta( $post_id, 'price', $price + 5 );
}
}
add_action( 'the_post', 'my_the_post_action' );
This part:
get_post_type() == 'product'
is not necessary, but you should determine for which kind of post (based on post type, category, metafield etc.) you want to run this piece of code. Without it will be executed everytime in admin query. This code is not tested, if something is wrong feel free to report.