Asked  7 Months ago    Answers:  5   Viewed   92 times

I am trying to set the email address when have a new order. And I stored the new email in wp_postmeta.

How to get the $order_id when using woocommerce_email_headers?

I need to get the order_id to use it with get_post_meta() function.

Here is my code:

function techie_custom_wooemail_headers( $headers, $object) {

    $email = get_post_meta( $order_id, '_approver_email', true );

    // Replace the emails below to your desire email
    $emails = array('eee@hotmail.com', $email);


    switch($object) {
        case 'new_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "rn";
            break;
        case 'customer_processing_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "rn";
            break;
        case 'customer_completed_order':
        case 'customer_invoice':
            $headers .= 'Bcc: ' . implode(',', $emails) . "rn";
            break;

        default:
    }

    return $headers;
}

add_filter( 'woocommerce_email_headers', 'techie_custom_wooemail_headers', 10, 2);

How do I get back the data?

Thanks.

 Answers

93

Updated: Added compatibility with Woocommerce version 3+

I have made some tests trying to output raw data from $order object without success. After some other tests I got now the correct order ID. I have use the code below for my test to be sure. Replace the value of $your_email by your own email. Then you will receive an email with the order ID in the header name:

function testing_hook_headers( $headers, $id, $order ) {
    // The order ID | Compatibility with WC version +3
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    $your_email = '<name@email.com>';
    $headers = "To: Order Num $order_id $your_email";
    return $headers;
}
add_filter( 'woocommerce_email_headers', 'testing_hook_headers', 10, 3);

So Here is your code:

function techie_custom_wooemail_headers( $headers, $email_id, $order ) {

    // The order ID | Compatibility with WC version +3
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    $email = get_post_meta( $order_id, '_approver_email', true );

    // Replace the emails below to your desire email
    $emails = array('eee@hotmail.com', $email);

    switch( $email_id ) {
        case 'new_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "rn";
            break;
        case 'customer_processing_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "rn";
            break;
        case 'customer_completed_order':
        case 'customer_invoice':
            $headers .= 'Bcc: ' . implode(',', $emails) . "rn";
            break;

        default:
    }

    return $headers;
}

add_filter( 'woocommerce_email_headers', 'techie_custom_wooemail_headers', 10, 3);

I havent test your code as it's particular, but you have the right manner to get order ID.

Wednesday, March 31, 2021
 
mnagel
answered 7 Months ago
55

To make it work you just need to define $custom_price variables as global in your function, this way:

$custom_price = 200; 

add_action( 'woocommerce_before_calculate_totals', 'add_custom_item_price', 10, 1 );
function add_custom_item_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    global $custom_price;

    foreach (  $cart_object->get_cart() as $item_values ) {
        $item_values['data']->price = $custom_price;
    }
} 

This code is tested and working (for woocommerce versions 2.5+ and 2.6+).

Naturally this code goes on function.php file of your active child theme or theme.

Wednesday, March 31, 2021
 
Taptronic
answered 7 Months ago
84

There is 2 ways to get the order key:

1) From an instance of WC_Order object class using the method get_order_key(), this way:

// Get an instance of the WC_Order object
$order_obj = WC_get_order($order_id);

// Get the order key
$order_key = $test_order->get_order_key();
$returnURL = site_url().'/checkout/order-received/'.$order_id.'/'.$order_key;

2) Using the WordPress get_post_meta() function from the $order_id, this way:

// Get the order key
$order_key = get_post_meta( $order_id, '_order_key', true);
$returnURL = site_url().'/checkout/order-received/'.$order_id.'/'.$order_key;

The Order number is the Order ID in general…

Saturday, May 29, 2021
 
Len_D
answered 5 Months ago
98

Not all modems support caller ID. And for those that do, the implementation varies between manufacturers.

There caller ID is passed through the serial data so you will have to use the TAPI library (or Windows' HyperTerminal to test it). The caller ID number typically appears between the first and the second ring.

You will need to issue a command to the modem to activate caller ID. Typically:

AT#CID=1 (or AT+VCID=1)

OK

Check the documentation for your modem.

When a call comes in, the modem will receive the a call string. Typically:

RING

Then the caller ID text will come in. If I am remembering correctly, it will be in the form:

NMBR=XXXXXXXXXX

[I am looking for a reference. I will post a link when I can find it]

UPDATE: Ah, found one. Check out this page for the commands and connection strings for for various modems:

How to Test a Modem for Caller ID Support

Monday, August 2, 2021
 
Zizoo
answered 3 Months ago
93

Here below are the hooks involved in WC_Cart add_to_cart() method:

A) Before an item is added to cart:

  1. Validation filter hook woocommerce_add_to_cart_validation
  2. Item Quantity change filter hook woocommerce_add_to_cart_quantity (not with ajax)
  3. Item Data change filter hook woocommerce_add_cart_item_data (not with ajax)
  4. and some others related to "sold individually" products (see here)

A) After an item is added to cart:

  1. Change cart Item filter hook woocommerce_add_cart_item
  2. Add an event, action hook woocommerce_add_to_cart

To be clear in your case:

  1. As you can see now woocommerce_add_to_cart is not a function but only an action hook.
  2. Hook location: It's located inside WC_Cart add_to_cart() method (at the end of the source code).
  3. When the hook is fired: It's fired once the WC_Cart add_to_cart() method is executed.
  4. What is the purpose: To execute some custom code when this method is executed (event).

Regarding your code:
It should be better to use the dedicated filter hook woocommerce_add_to_cart_validation that will stop customer that want to add a new item to the cart if there is already a product in cart from a different vendor, displaying optionally a custom message:

add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 3 );
function filter_add_to_cart_validation( $passed, $product_id, $quantity ) { 
    if ( WC()->cart->is_empty() ) return $passed;

    // Get the VendorId of the product being added to the cart.
    $current_vendor = get_wcmp_product_vendors($product_id);

    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Get the vendor Id of the item
        $cart_vendor = get_wcmp_product_vendors($cart_item['product_id']);

        // If two products do not have the same Vendor
        if( $current_vendor->id != $cart_vendor->id ) {
            // We set 'passed' argument to false
            $passed = false ;

            // Displaying a custom message
            $message = __( "This is your custom message", "woocommerce" );
            wc_add_notice( $message, 'error' );
            // We stop the loop
            break; 
        }
    }
    return $passed;
}

Code goes in function.php file of your active child theme (or active theme) or in any plugin file.

Tested and works.


Sunday, August 15, 2021
 
BetaRide
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :