template_include stop working on woocommerce update - wordpress

The following code is to override the Product Details page template, It was working since the last update on WooCommerce. Can anyone help me out on this, thanks in advance.
add_filter('template_include', 'wpautomate_plugin_templates');
function wpautomate_plugin_templates( $template )
{
$plugin_path='';
$reflector = new ReflectionClass('Ze_Single_Product_Layout');
$file_name=plugin_dir_path($reflector->getFileName());
$plugin_path=$file_name;
$post_types = array('product');
$template_id=get_post_meta( get_the_ID(), '_product_layout', true );
if (is_singular('product') && !empty($template_id))
{
//render custom template for single product
$template = $plugin_path . 'template/woo-single-page.php';
}
return $template;
}//end of function

You need to call this filter
add_filter('template_include', 'wpautomate_plugin_templates');
with init action hook
add_action('init','load_custom_template_woo');
function load_custom_template_woo(){
add_filter('template_include', 'wpautomate_plugin_templates');
}
Thanks

For me, I had to set the priory for the hook callback greater than 10, like this
// priority = 11
add_action('template_include', 'wpautomate_plugin_templates', 11);

Related

WooCommerce line_total and line_subtotal

Is there any way to change the line_total and line_subtotal in WooCommerce once an order is in add to cart.
Thanks in advance.
There two hook for check out page where you can modify line total and line sub total.
woocommerce_calculate_totals
function action_woocommerce_calculate_totals( $fee ) {
// add your logic
};
// add the action
add_action('woocommerce_calculate_totals','action_woocommerce_calculate_totals', 10, 1 );
woocommerce_cart_subtotal
function filter_woocommerce_cart_subtotal( $array, $int, $int ) {
// implement your logic
return $array;
};
// add the filter
add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );

WooCommerce not letting me add custom column to shop_order post type

I just started writing a plugin and ran into a problem right away. I want to add a column to the order overview page in the WooCommerce admin. The straight forward filter below doesn't do anything. Replacing shop_order with post or product, however, does show the extra column on the respective overview page.
add_filter('manage_edit-shop_order_columns', 'add_sales_column');
function add_sales_column($columns) {
$columns['order_sales'] = "Sales";
return $columns;
}
Trying this on:
WC Version: 2.1.5
WP Version: 3.8.1
How to solve this?
I encountered a similar issue when adding a custom column to the WooCommerce Orders overview page from my theme's functions.php file. I was able to get a custom column to show up by increasing the filter priority above the default value of 10. Try replacing your code with the following:
add_filter('manage_edit-shop_order_columns', 'add_sales_column', 11);
function add_sales_column($columns) {
$columns['order_sales'] = "Sales";
return $columns;
}
Tested on WP 3.9.1 and WC 2.1.12.
Check out the WordPress Codex entry on add_filter for more details on the behavior of filters using the $priority parameter.
The problem is that we have to wait for WooCommerce to finish its setup. This goes two ways, first running all our hooks inside a plugins_loaded main hook, and then setting the priority of our hooks for something greater than WC's.
add_action( 'plugins_loaded', 'setup_so_22237380' );
function setup_so_22237380()
{
// Just to make clear how the filters work
$posttype = "shop_order";
// Priority 20, with 1 parameter (the 1 here is optional)
add_filter( "manage_edit-{$posttype}_columns", 'column_set_so_22237380', 20, 1 );
// Priority 20, with 2 parameters
add_action( "manage_{$posttype}_posts_custom_column", 'column_display_so_22237380', 20, 2 );
// Default priority, default parameters (zero or one)
add_filter( "manage_edit-{$posttype}_sortable_columns", 'column_sort_so_22237380' );
}
function column_set_so_22237380( $columns )
{
$columns['order_sales'] = "Sales";
return $columns;
}
function column_display_so_22237380( $column_name, $post_id )
{
if ( 'order_sales' != $column_name )
return;
$sales_information = 'Your custom get_order_sales_information($post_id)';
if ( $sales_information )
echo "<strong style='color:#f00;'> $sales_information </strong>";
}
function column_sort_so_22237380( $columns )
{
$columns['order_sales'] = 'order_sales';
return $columns;
}
Setting the column as sortable is optional (manage_edit-{$posttype}_sortable_columns) and needs an extra action hook to make it work (pre_get_posts). This may be a complex function to build and requires its own research.
It will working fine if you include all the order hook on
add_action( 'admin_init', 'setup_all_order_column_hook' );
function setup_all_order_column_hook(){
//Just to make clear how the filters work
$posttype = "shop_order";
//Priority 20, with 1 parameter (the 1 here is optional)
add_filter( "manage_edit-{$posttype}_columns", 'column_set_so_22237380', 20, 1 );
//Priority 20, with 2 parameters
add_action( "manage_{$posttype}_posts_custom_column",column_display_so_22237380', 20, 2 );
// Default priority, default parameters (zero or one)
add_filter( "manage_edit-{$posttype}_sortable_columns",'column_sort_so_22237380' );
}

Global variable is not working between functions

i am creating plugin for my project. i want to create one page when plugin gets activate and same way i want to delete that page when plugin gets deactivate... i am able to create page but i am facing problem while deleting page...
my code is
global $page_id;
register_activation_hook(__FILE__,'createPage');
register_deactivation_hook(__FILE__, 'dropPage');
function createPage()
{
global $page_id;
$page['post_type'] = 'page';
$page['post_content'] = 'hello this page created by plugin';
$page['post_status'] = 'publish';
$page['post_title'] = 'dpage';
$page_id = wp_insert_post ($page);
}
function dropPage()
{
global $page_id;
wp_delete_page($page_id);
}
it's not deleting page... if i give wp_delete_post('116') then it's working fine... i have assigning page id in global variable then also i am not able to retrieve it..
can any one suggest me how to do it?
Thanks in advance
the global $page_id you're adding will only contain the page ID when you're activating the plugin. To store the page ID, use the Options API.
register_activation_hook(__FILE__,'createPage');
register_deactivation_hook(__FILE__, 'dropPage');
function createPage()
{
$page['post_type'] = 'page';
$page['post_content'] = 'hello this page created by plugin';
$page['post_status'] = 'publish';
$page['post_title'] = 'dpage';
$page_id = wp_insert_post ($page);
update_option('the_page_id_i_created', $page_id );
}
function dropPage()
{
if( get_option('the_page_id_i_created') ){
wp_delete_page( get_option('the_page_id_i_created') );
}
}

Hide Shipping Options Woocommerce

So I'm trying to hide certain ship methods in Woocommerce based on a product tag. The main problem I face is my own lack PHP knowledge so I frankensteined the following code together with the help of some very friendly folks:
add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' , 10, 1 );
function check_cart_for_share() {
// load the contents of the cart into an array.
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
// loop through the array looking for the tag you set. Switch to true if the tag is found.
foreach ($cart as $array_item) {
if (isset($array_item['product_tag']) && $array_item['product_tag'] == "CHOSEN_TAG") { // Replace "CHOSEN_TAG" with what ever tag you want
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_tag( $available_methods ) {
// use the function abve to check the cart for the tag.
if ( check_cart_for_share() ) {
// remove the rate you want
unset( $available_methods['flat_rate'] ); // Replace "flar_rate" with the shipping option that yu want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
I understand that this code is by no means universal and thus the variables will be different from case to case but perhaps someone can tell me if something looks off in the code. Much appreciated
No doubt you have sorted this by now but your code was a good start for me ... and since I sorted it I have published it below. Your problem was that woocommerce doesn't have the product_tag in the cart array so you have to go and get it.
/* !Hide Shipping Options Woocommerce */
add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' , 10, 1 );
function check_cart_for_share() {
// load the contents of the cart into an array.
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
// loop through the array looking for the tag you set. Switch to true if the tag is found.
foreach ($cart as $array_item) {
$term_list = wp_get_post_terms( $array_item['product_id'], 'product_tag', array( "fields" => "names" ) );
if (in_array("Heavy",$term_list)) { // Replace "Heavy" with what ever tag you want
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_tag( $available_methods ) {
// use the function above to check the cart for the tag.
if ( check_cart_for_share() ) {
// remove the rate you want
unset( $available_methods['flat_rate'] ); // Replace "flat_rate" with the shipping option that you want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
You can use shipping class of Woocommerce to achieve this.
Here is the step by step instructions.
Create a shipping class (woocommerce->settings->shipping->shipping classes).
Associate this shipping class with products (that you wand to hide shipping methods for)
Use this snippet. (copy and pate to function.php).
For more information about the snippet is available here.

How can I select books randomly in Now Reading wordpress plugin?

I am using 'Now Reading' plugin in a wordpress project. In plugin's sidebar template I am using this query:
while( have_books('status=read&orderby=finished&num=2') ) : the_book();
to select 2 books.
What parameter should I pass to make it random? I tried with 'order=rand' and 'rand=true' but it did not work.
Any help will be appreciated!
Thanks in advance..
A random function doesn't actually exist. I have used this code, in my themes functions.php to allow random order before - not sure if it'll work in this situation, but worth a try.
Add this to your themes functions.php file:
function query_random_posts($query) {
return query_posts($query . '&random=true');
}
class RandomPosts {
function orderby($orderby) {
if ( get_query_var('random') == 'true' )
return "RAND()";
else
return $orderby;
}
function register_query_var($vars) {
$vars[] = 'random';
return $vars;
}
}
add_filter( 'posts_orderby', array('RandomPosts', 'orderby') );
add_filter( 'query_vars', array('RandomPosts', 'register_query_var') );
Then try this in your sidebar file:
while( have_books('status=read&orderby=finished&num=2&random=true') ) : the_book();
If not, my only other suggestion would be to get the 10 latest books, add them all to a new array, and then shuffle that array. May be a bit bloated though.

Resources