Woocommerce override plugin action - wordpress

The file email-order-items.php has the following line of code:
echo "\n" . sprintf( __( 'Cost: %s', 'woocommerce' ), $order->get_formatted_line_subtotal( $item ) );
The following action hook has been added to a plugin I am using (Woocommerce Composite Products):
add_action( 'woocommerce_order_formatted_line_subtotal', array( $this, 'wc_cp_order_item_subtotal' ), 10, 3 );
I would like to override the function wc_cp_order_item_subtotal to change the way to item subtotal is displayed. I have tried adding the following to my child theme functions.php, but it doesn't do anything.
remove_action( 'woocommerce_order_formatted_line_subtotal', 'wc_cp_order_item_subtotal', 10);
add_action( 'woocommerce_order_formatted_line_subtotal', 'child_wc_cp_order_item_subtotal', 10, 3);
function child_wc_cp_add_order_item_meta( $order_item_id, $cart_item_values, $cart_item_key = '' ) {
return 'xxxxxxx';
}
Any tips to help me get this working would be greatly appreciated.

It isn't mentioned in the Codex, but I usually call the remove_action() function from a hook.
Also, as covered in the Codex example for action added from classes, you need to access the class instance or variable.
I don't see wc_cp_order_item_subtotal in the Composite plugin anywhere, so I presume you aren't using Woo's version. In which case, I don't have access to the code and can't tell you exactly how to access the class variable.
But if you were using Woo's Composite Products it would be as follows:
Edited for Composites 2.4
function so_remove_action(){
global $woocommerce_composite_products;
remove_action( 'woocommerce_order_formatted_line_subtotal', array( $woocommerce_composite_products->order, 'wc_cp_order_item_subtotal' ), 10, 3 ); //not sure why it isn't a filter, but also not sure if there is a huge difference
}
add_action('init', 'so_remove_action');

Related

Remove an action - Wordpress (Specifically Woocommerce PIP plugin)

I have Woocommerce website with the plugin Print Invoices/Packing Lists installed.
I'm trying to remove an action and I've narrowed down the action to this (shortened) code;
class WC_PIP_Document_Invoice extends WC_PIP_Document {
public function __construct( array $args ) {
parent::__construct( $args );
// add a "View Invoice" link on order processing/complete emails sent to customer
add_action( 'woocommerce_email_order_meta', array( $this, 'order_paid_email_view_invoice_link' ), 40, 3 );
}
}
So I'm looking at removing this using; https://codex.wordpress.org/Function_Reference/remove_action
However as the action as added within a class, I can't quite work out what to pass into the function name. Would it be something like;
remove_action( 'woocommerce_email_order_meta', array( 'WC_PIP_Document_Invoice', 'order_paid_email_view_invoice_link' ), 40 );
Can anyone point me in the right direction?
Many thanks
Try following code that should work for you.
add_action( 'init', 'wpse_106269_remove_hooks', 11 );
function wpse_106269_remove_hooks(){
remove_action( 'woocommerce_email_order_meta', 'action_woocommerce_email_order_meta', 10, 4 );
}

custom plugin bombing Edit Order page when trying wc_get_order

I'm writing a tiny custom plugin to add a meta box so I can freely interface with EasyPost. It's included in the Edit Order page. I'm trying to use wc_get_order to get basic order details and the page is bombing. This looks like it should work flawlessly and be very simple, but I can't find the answer anywhere.
I'm using this code in:
/wp-content/plugins/custom-easypost/jens-custom-easypost.php
Sorry if this is a dumb question, but I'm stumped.
This code is NOT WORKING:
$order = wc_get_order( $order_id );
This code IS working:
// Add meta box
add_action( 'add_meta_boxes', 'jens_epbox' );
function jens_epbox() {
add_meta_box(
'jens-epbox-modal',
'Jens EP Box',
'jens_meta_box_callback',
'shop_order',
'side',
'high'
);
}
// Callback
function jens_meta_box_callback( $post )
{
echo 'Get Label ' . $post->ID. '';
}

How to add another Add To Cart and product variant selection in Woocommerce

How can I add another add to cart button as well as product variant selection above product description in my Woocommerce product page?
This will add another variable product selector above the short description. For simple products they will remain unchanged. This code was taken from the function woocommerce_variable_add_to_cart which I have basically copied and just removed the wp_enqueue_script call as this is already included in the usual variation selector already present. Paste this code into your theme's functions.php file.
function add_product_variation_selector() {
global $product;
// Enqueue variation scripts.
//wp_enqueue_script( 'wc-add-to-cart-variation' );
if( $product->is_type( 'variable' ) ) :
// Get Available variations?
$get_variations = count( $product->get_children() ) <= apply_filters( 'woocommerce_ajax_variation_threshold', 30, $product );
// Load the template.
wc_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $get_variations ? $product->get_available_variations() : false,
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_default_attributes(),
) );
endif;
}
add_action( 'woocommerce_single_product_summary', 'add_product_variation_selector', 9 );
Clone the plugins/woocommerce/templates/content-single-product.php to your theme following the instructions from the file.
Change it according to your needs.

wc_print_notices is not displaying any messages in woocommerce pages?

I have used default woocommerce templates for shop, cart and single page.
I have not removed any hooks either but also I am not getting any message.
Any Idea?
add_action( 'woocommerce_before_single_product', 'Cusotm_wc_print_notices', 10 );
function Cusotm_wc_print_notices()
{
echo 'Hook is working fine';
}
I am getting this message 'Hook is working fine' but not wc_print_notices();.
I'm not really sure what is the problem. Your question needs further details. With that said, can you try adding this code to your functions.php of your current theme.
add_action( 'template_redirect', 'test' );
function test() {
wc_add_notice( __( 'Sorry there was a problem.', 'woocommerce' ), 'error' );
}
Let me know if it does something.
UPDATE
If you have something like this:
add_action( 'woocommerce_before_single_product', 'Cusotm_wc_print_notices', 10 );
function Cusotm_wc_print_notices()
{
$notices = WC()->session->get('wc_notices');
print_r($notices);
}
it will not work because $notices will be empty once wc_print_notices() is called.
try changing priority and you will get something. Should be something like this:
add_action( 'woocommerce_before_single_product', 'Cusotm_wc_print_notices', 9 );
use priority below 10. Because WooCommerce is using 10.
add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );

Wordpress ACF plugin: trouble getting field when saving post

Using ACF; I am making a plugin that hooks into WP on 'draft_to_publish' and on 'pending_to_publish' this part works fine. But when I try to get the ACF generated fields they return as blank, as if the fields generated by ACF haven't been set yet.
Short version of my plugin looks like this:
function scheduelMailChimp( $post ) {
// get post data and preb it for mail
$post_ID = get_the_ID();
$content_post = get_post( $post_ID );
$content = $content_post->post_content;
$postTitle = get_the_title( $post_ID );
//log debuginfo til debug.log
log_me(
array(
'get field date' => get_field($field_name, $post_id, $format_value)
)
);
}
add_action( 'draft_to_publish', 'scheduelMailChimp', 10, 1 );
add_action( 'pending_to_publish', 'scheduelMailChimp', 10, 1 );
The above code outputs empty. If I try to output something that is generated by WP and not ACF everything works like a charm.
all and any bright ideas are more then welcome :)
In case other people are looking for the same answer: After diving into a ton of Google searches, it seems that ACF haven't saved the data at the time draft_to_publish is fired. So I tried using the 'save_post' and it worked like a charm.

Resources