Filter out unwanted order item meta data from Woocommerce email notifications

前端 未结 4 1880
悲哀的现实
悲哀的现实 2021-01-20 17:20

In the order email templates (for example email-order-items.php), WooCommerce uses the function wc_display_item_meta to display product details in

4条回答
  •  天命终不由人
    2021-01-20 18:03

    I kind of agreed with @pstidsen argument. So I was thinking about how to solve this without to re-add all the metadata, since it kind of disturbed me not to handle it in the same way as it was added before. I have additional filters to add css classes and so on to the metadata. So there would've been a need to take care of.

    So here is my approach which gives you the opportunity to use it for emails, custom emails, pdf invoices or similar scenarios. I also uses a fallback to filter for our frontend or any situation we didn't consider. Please keep the order of if else in mind. I checked the admin filter the last, to make sure any other filter gets fired before. The situation for an email at example is: It's sent from the admin interface, so the admin filter is true but also is the email filter. Functionality for a different filter for admin emails is given as well.

    /**
    * This function filters all unwanted item metadata, if the specific filter are hooked in
    * we also use a fallback filter, if none of the hooks are fired
    *
    * @params array() $metadata
    *
    */
    add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'custom_filter_item_meta_data', 50, 1);
    function custom_filter_item_meta_data( $metadata ){
        if ( empty( $metadata ) ) return $metadata;
        $filter_array = array();
        if ( apply_filters( 'custom_filter_item_meta_email', false ) ){
            // email filter goes here
            $filter_array = array( 'whatever','you', 'wanna', 'filter, 'for', 'email' );
        }elseif ( apply_filters( 'wrs_filter_item_meta_admin_email', false ) ){
            // admin email filter goes here
            // pass
        elseif ( apply_filters( 'custom_filter_item_meta_invoice', false ) ){
            // invoice filter goes here
            $filter_array = array( 'whatever','you', 'wanna', 'filter, 'for', 'invoices' );
        }elseif ( apply_filters( 'custom_filter_item_meta_admin', false ) ){
            // general admin filter goes here
            $filter_array = array( 'whatever','you', 'wanna', 'filter, 'for', 'admin_backend' );
        }else{
            // fallback filter
            $filter_array = array( 'whatever','you', 'wanna', 'filter, 'for', 'fallback' );
        }
        foreach ( $metadata as $key => $meta ){
            if ( in_array( $meta->key, $filter_array ) ){
                unset ( $metadata[ $key ] );
            }
        }
        return $metadata;
    }
    
    /**
    * Is used to enable our item meta filter for our admin backend
    * Hooked:
    * @admin_init
    */
    add_action( 'admin_init', 'custom_init_item_meta_filter_admin', 50, 1 );
    function custom_init_item_meta_filter_admin(){
        add_filter( 'custom_filter_item_meta_admin', function(){ return true; });
    }
    
    /**
    * Is used to enable our item meta filter for emails
    * Hooked:
    * @woocommerce_email_order_details
    */
    add_action( 'woocommerce_email_order_details', 'custom_init_item_meta_filter_email' ), 10, 2);
    function wrs_init_item_meta_filter_email( $order, $sent_to_admin ){
        if ( $sent_to_admin ){
            add_filter('wrs_filter_item_meta_admin_email', function(){ return true; } );
        }else{
            add_filter('wrs_filter_item_meta_email', function(){ return true; } );
        }
    }
    
    /**
    * Is used to enable our item meta filter for invoices
    * Hooked:
    * @wpo_wcpdf_before_order_details
    */
    add_filter( 'wpo_wcpdf_before_order_details', 'custom_init_item_meta_filter_invoice', 10, 1);
    function custom_init_item_meta_filter_invoice(){
        add_filter( 'custom_filter_item_meta_invoice', function(){ return true; });
    }
    

    I din't test it in that "flattened" format. I used it within different classes of my oop coded plugin and edited it to post it here.

提交回复
热议问题