Modify WooCommerce product columns - php

I'm trying to override, or simply customize, the admin orders list view.
I understood the method to customize is render_shop_order_columns in includes/admin/class-wc-admin-post-types.php but I cannot remove the action (method) from theme functions.php neither by a custom plugin in the plugins_loaded hook: always get bool(false) on
var_dump(remove_action( 'manage_shop_order_posts_custom_column', array( $GLOBALS['wc_admin_post_type'], 'render_shop_order_columns' ) ));
I see there is the woocommerce_order_item_name filter, but if I add a picture there (that's what I need), I get a wrong output since it is used in the title attribute of link to product too.
Could anyone please advice?
Thank you!

I was getting a wrong way...
Maybe the right one is to unset the column and add your own.
See here:
https://wordpress.org/support/topic/hooking-and-adding-new-column-on-woocommerce-order-admin-page
basically:
add_filter('manage_edit-shop_order_columns', 'show_custom_product_column', 15);
function show_custom_column($columns) {
$new_columns = (is_array($columns)) ? $columns : array();
//remove column
unset($new_columns['column_to_unset']);
//add custom column
$new_columns['custom_column'] = __( 'Translation', 'woocommerce' );
return $new_columns;
}
add_action('manage_shop_order_posts_custom_column', 'my_custom_column', 10, 2);
function my_custom_column($column) {
global $post, $woocommerce, $the_order;
switch ($column) {
case 'custom_column' :
// Custom code
break;
}
}

Related

Checkout Field Editor plugin filter hook to set custom display rules for checkout fields

I have just purchased the plugin Checkout Field Editor for WooCommerce from Theme High.
I need to create a custom display rule condition for fields.
They provide the filters hook:
apply_filters( 'thwcfe_show_field', $show, $field_name );
I have tried the following code, but it does not work.
Can someone help?
function display( $field_name='test' ) {
$show =true;
return $show;
}
add_filter('thwcfe_show_field', 'display');
The true / false options works but I can't make it specific to the $field_name = 'test'.
You are not using this filter in a correct way… First there are 2 arguments for the hooked function (one is missing).
To restrict the filter to a specific field name, you need an IF statement and between $field_name and the value to be tested, you need to use == or === comparison operators, but not =.
So your code is going to be:
add_filter('thwcfe_show_field', 'display_test_field', 10, 2 );
function display_test_field( $show, $field_name ) {
if ( 'test' === $field_name ) {
$show = true;
}
return $show;
}
where 10 is the hook priority and 2 the number of arguments for this hook.
It should better work now.
Documented WordPress add_filter() function

WooCommerce - Add content on the thank you page by the shipping method

I would like to place my own content (in my case a shortcode) on the thank you page at WooCommerce after completing the order, which depends on the shipping method.
So for example:
Shipping Method 1: Content 1
Shipping Method 2: Content 2
Shipping Method 3: No content
I have already found something for text here, but i dont get the shortcode inserted. Alternatively I have tested what works with the shortcode here, where the dependency on the shipping method is missing.
Maybe someone can help me to implement this.
Ideally the content should be above the table with the order information.
Thanks a lot!
May be the way you added the shortcode to the thank you text can be the issue.
See the following shortcode function called avengers which i created for demo.
function call_avangers( $atts ){
return "<p>Avengers . . . . Assemble !!!</p>";
}
add_shortcode( 'avengers', 'call_avangers' );
You can display this shortcode in thank you page using the below function
add_filter( 'woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 20, 2 );
function woo_change_order_received_text( $text, $order ) {
if( $order->get_shipping_method() == 'your_shipping_method_here' ) {
$text .= do_shortcode('[avengers]');
}
return $text;
}
The basic thing is you have to call do_shortcode('your_shortcode_name')

How to start some function elsewhere but not in file function.php Wordpress

I'm writing a small plugin.
I'm deleting a payment method if there is a chechout of some categories of goods.
I have some function, and filter:
function filter_gateways($gateways){
$payment_NAME = 'paypal'; // <-- some payment method
$category_ID_1 = '6'; // <-- some category of products
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
// Get the terms, i.e. category list using the ID of the product
$terms = get_the_terms( $values['product_id'], 'product_cat' );
// List of the products category for a match
foreach ($terms as $term) {
// $category_ID_1 is the ID of the category for which i want to remove the payment gateway
if($term->term_id == $category_ID_1){
unset($gateways[$payment_NAME]);
break;
}
break;
}
}
return $gateways;
}
add_filter('woocommerce_available_payment_gateways','filter_gateways');
This filter works in the file functions.php on the folder theme.
But if I use it in my plugin files - filter doesn't work.
What am I doing wrong?How to make it work?
For, I pass my some variables in this filter.
Your problem is one of timing. Likely your plugin is loading before Woocommerce has had a chance to set up that filter so it just fails to do anything. Try wrapping the filter call inside an action to delay it's activation. I think after_setup_theme would be a good choice, but you may need to use a different one. You can see all of the default available actions here https://codex.wordpress.org/Plugin_API/Action_Reference.
add_action( 'after_setup_theme', 'do_filter_gateways' );
function do_filter_gateways()
{
add_filter('woocommerce_available_payment_gateways','filter_gateways');
}

Remove Wordpress Plugin functionality from admin side

I'm using a WordPress plugin https://github.com/lesterchan/wp-postratings.
It also showing ratings on admin, when i visit http://domain.com/wp-admin/edit.php.
How do i remove those ratings from admin side.
You can use the below function in your functions.php file with the manage_posts_columns filter. I'm assuming your custom post type id 'tools' and the column is referenced by 'ratings'. If they are different you can just change that in the code.
add_filter( 'manage_posts_columns', 'custom_post_columns', 10, 2 );
function custom_post_columns( $columns, $post_type ) {
switch ( $post_type ) {
//assuming the id for the custom post type is 'tools'
case 'tools':
unset(
//I'm using 'ratings' but you'll have to check and see what the name for the column really is.
$columns['ratings']
);
break;
}
return $columns;
}

Modify WooCommerce Is_Purchasable

I was working on implementing my own pre-order system, where I set a is_preorder custom field for each product.
I was trying to modify the WooCommerce's Is_Purchasable option so that, if the product has pre-order status and it's already passed the pre-order deadline, it shouldn't be able to be purchased. I've tried a bunch of ways, but nothing seems working.
Here's something that I did (rough idea)
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable');
function preorder_is_purchasable() {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder');
if($is_preorder && "not yet passed deadline")
return true;
else
return false;
}
I don't just wanna disable the add_to_cart button, I also want to disable the functionality (should prompt error if user tried to add product by hardcoding in url).
How should I go on with this?
===========================================================================
Here's my final code:
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable', 10, 2);
function preorder_is_purchasable( $is_purchasable, $object ) {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder', $object->id);
// if product is Pre-Order
if($is_preorder)
{
$today = date('Ymd');
// another field added using 'Advance Custom Fields' plugin
$preorder_deadline = get_field('preorder_deadline', $object->id);
if($today <= $preorder_deadline) // if not yet pass deadline
return true;
else
return false;
}
else
return $is_purchasable; // normal
Update 2019: please see dev_masta answer for correct solution nowadays.
Not sure if it solves the issue as this has to be tested on your own custom set up. But you're using get_field wrong: if it is not used inside a Loop, you should provide the post ID.
Analyzing the filter woocommerce_is_purchasable, we see that it takes two parameters, a boolean (is_purchasable) and an object (WC_Product).
Try this:
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable', 10, 2);
function preorder_is_purchasable( $is_purchasable, $object ) {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder', $object->id);
if($is_preorder && $is_purchasable)
return true;
else
return false;
}
The accepted answer is a bit outdated today.
Instead of using $object->id you should use $object->get_id(), otherwise you'll get a PHP notice about incorrect use.
function disable_purchased_products( $is_purchasable, $object ){
// custom function to get the array of purchased products ID's
$already_purchased = get_purchased_products();
if( in_array( $object->get_id(), $already_purchased ) ){
return false;
} else {
return $is_purchasable;
}
}
add_filter( 'woocommerce_is_purchasable', 'disable_purchased_products', 10, 2 );
I hope this will help someone, I've seen this (outdated) code all around the net..

Categories