Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter eum3l

    (@eum3l)

    Hi,

    i found another solution and it is solved now. It is not regulary solution but it is working. Just puting this code to the funcitons.php of woocommerce let you display the unused/new coupon codes into the invoice.

    Then you just put at Custom content blogs from PDF-Invoice this code: {{unused_coupon_code}} and coupons are displayed. Maybe you can add this for next update as already included.

    function inject_last_unused_coupon_code_to_pdf_invoice_macros($macros, $data)
    {
            $user        = wp_get_current_user();
        $coupon_args = array(
            'meta_query'     => array(
                'relation'    => 'OR',
                array(
                    'key'     => 'wc_eoc_role_coupon_user_id',
                    'value'   => $user->ID,
                ),
                array(
                    'key'     => 'customer_email',
                    'value'   => $user->user_email,
                    'compare' => 'LIKE'
                )
            )
        );
        $coupons = array();
        $args    = wp_parse_args(
            $coupon_args,
            array(
                'post_type'   => 'shop_coupon',
                'post_status' => 'publish',
                'numberposts' => -1
            )
        );
    
        // Go through coupons
        foreach( get_posts( $args ) as $coupon ) {
            $coupons[ $coupon->post_title ] = $coupon->post_title;
        }
    
        $unused_coupons = array();
    
        // Collect coupons
        foreach( $coupons as $coupon_code ) {
            $coupon = new WC_Coupon( $coupon_code );
    
            if( $coupon->is_valid() ) {
                $unused_coupons[] = $coupon->code;
            }
        }
    
        $macros['{{unused_coupon_code}}'] = $unused_coupons[0];
    
        return $macros;
    }
    add_filter('woo_pdf_macros', 'inject_last_unused_coupon_code_to_pdf_invoice_macros', 100, 2);

    Or RAW Paste data:

    function inject_last_unused_coupon_code_to_pdf_invoice_macros($macros, $data)
    {
            $user        = wp_get_current_user();
        $coupon_args = array(
            'meta_query'     => array(
                'relation'    => 'OR',
                array(
                    'key'     => 'wc_eoc_role_coupon_user_id',
                    'value'   => $user->ID,

Viewing 1 replies (of 1 total)