How to get order id in Woocomerce Email - email

I try to get the order id when adding extra content in woocommerce email, is anyone knows what goes wrong with this...?
Thanks
add_action( 'woocommerce_email_after_order_table', 'add_content', 20 );
function add_content() {
global $woocommerce;
echo $order_id
echo $order->id;
}

you can pass the parameter $order and get $order->get_id();
something like this:
function add_content( $order ) {
echo $order->get_id();
}

Look in the source, $order is the first variable passed to the woocommerce_email_after_order_table hook.
add_action( 'woocommerce_email_after_order_table', 'so_43612005_add_content', 20, 4 );
function so_43612005_add_content( $order, $sent_to_admin, $plain_text, $email ) {
// WooCommerce 3.0
if( method_exists( $order, 'get_id' ) ) {
$order_id = $order->get_id();
// WooCommerce 2.6.x
} else {
$order_id = $order->id;
}
echo $order_id;
}

add_action( 'woocommerce_email_after_order_table', 'wdm_add_shipping_method_to_order_email', 10, 2 );
function wdm_add_shipping_method_to_order_email( $order, $is_admin_email ) {
echo '<p><h4>Order Id:</h4> ' . $order->get_order_number() . '</p>';
}

Related

Redirect to different thank you pages on specific shipping method selection

I have two different shipping method on my WooCommerce site COD & local pickup, I want to redirect on two different thank you page depending on which shipping method the buyer choose. I tried this but its showing an error after purchase
add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
function woo_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && !empty( $wp->query_vars['order-received'] )) {
if( $order->has_shipping_method('flat_rate:21') ){
wp_redirect( 'mysite.com/thank-you-1/' );
exit;
}
elseif( $order->has_shipping_method('local_pickup:24') ) {
wp_redirect( 'mysite.com/thank-you-2/' );
exit;
}
}
}
You can use woocommerce_thankyou
add_action( 'woocommerce_thankyou', 'woo_custom_redirect_after_purchase');
function woo_custom_redirect_after_purchase( $order_id ){
$order = wc_get_order( $order_id );
if( $order->has_shipping_method('flat_rate:21') ){
wp_redirect( 'mysite.com/thank-you-1/' );
exit;
}elseif( $order->has_shipping_method('local_pickup:24') ) {
wp_redirect( 'mysite.com/thank-you-2/' );
exit;
}
}

Company field not required woocommerce [duplicate]

I'd like to make billing company and shipping company not required in WooCommerce. For some reason the code I am using works for EVERYTHING except for the company part. It turns out a third party plugin is making the company name required, here is the full code of that plugin:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
require_once('legacy_pakkelabels_shipping_main.php');
/**
* #class Pakkelabels_Shipping_GLS_Business_Legacy
* #version 0.1.0
* #author Magnus Vejlø - Pakkelabels
*/
class Legacy_Pakkelabels_Shipping_GLS_Business extends Legacy_Pakkelabels_Shipping_Main
{
public function __construct($instance_id = 0)
{
$this->id = 'legacy_pakkelabels_shipping_gls_business';
$this->instance_id = absint($instance_id);
$this->method_title = __('GLS Business ', 'woocommerce-pakkelabels');
$this->method_description = __('Adds the option to ship with the GLS business to the checkout', 'woocommerce-pakkelabels');
$this->init();
}
/* add the diffrent actions */
function addActions()
{
//adds the shipping method to the WooCommerce
add_filter('woocommerce_shipping_methods', array($this, 'register_shipping_method'));
add_action('woocommerce_after_shipping_rate', array($this, 'pakkelabels_shipping_gls_business_show_below_shipping'));
add_action('woocommerce_checkout_process', array($this, 'pakkelabels_shipping_gls_business_field_process'));
}
function addFilters()
{
}
function pakkelabels_shipping_gls_business_field_process() {
global $woocommerce;
$choosen_shipping_method1 = preg_replace('/\d/', '', $woocommerce->session->chosen_shipping_methods[0] );
$choosen_shipping_method2 = preg_replace('/\d/', '', $woocommerce->session->chosen_shipping_methods );
if((isset($_POST['ship_to_different_address']) && ($_POST['shipping_company'] == '' || !isset($_POST['shipping_company']))) && ($choosen_shipping_method1 == "legacy_pakkelabels_shipping_gls_business" || $choosen_shipping_method2 == "legacy_pakkelabels_shipping_gls_business")){
if ( version_compare( $woocommerce->version, '2.1', '<' ) ) {
$woocommerce->add_error(__('Please fill out the Shipping company', 'woocommerce-pakkelabels'));
} else {
wc_add_notice( __('Please fill out the Shipping company', 'woocommerce-pakkelabels') , 'error');
}
}
if((!isset($_POST['ship_to_different_address']) && ($_POST['billing_company'] == '' || !isset($_POST['billing_company']))) && ($choosen_shipping_method1 == "legacy_pakkelabels_shipping_gls_business" || $choosen_shipping_method2 == "legacy_pakkelabels_shipping_gls_business")){
if ( version_compare( $woocommerce->version, '2.1', '<' ) ) {
$woocommerce->add_error(__('Please fill out the billing company', 'woocommerce-pakkelabels'));
} else {
wc_add_notice( __('Please fill out the billing company', 'woocommerce-pakkelabels') , 'error');
}
}
}
function pakkelabels_shipping_gls_business_show_below_shipping($rate){
global $woocommerce;
global $woocommerce;
$choosen_shipping_method1 = preg_replace('/\d/', '', $woocommerce->session->chosen_shipping_methods[0] );
$choosen_shipping_method2 = preg_replace('/\d/', '', $woocommerce->session->chosen_shipping_methods );
if($choosen_shipping_method1 == "legacy_pakkelabels_shipping_gls_business" || $choosen_shipping_method2 == "legacy_pakkelabels_shipping_gls_business"){
if($rate->method_id == 'legacy_pakkelabels_shipping_gls_business'){
echo '<div class="gls_shipping_method_text shipping_company_required">' . __('The company name is required.', 'woocommerce-pakkelabels').'</div>';
}
}
}
/* Register the shipping method in WooCommerce*/
function register_shipping_method($methods)
{
$methods['legacy_pakkelabels_shipping_gls_business'] = 'Legacy_Pakkelabels_Shipping_GLS_Business';
return $methods;
}
}
$pakkelabels_GLS_Business_Legacy = new Legacy_Pakkelabels_Shipping_GLS_Business();
$pakkelabels_GLS_Business_Legacy->mainAddActions();
$pakkelabels_GLS_Business_Legacy->addActions();
$pakkelabels_GLS_Business_Legacy->addFilters();
The plugin used is a delivery plugin, and since we're delivering to companies it requires a company name. However, since we are a B2B store we already have these company names registered and we do not want them nor need them in the checkout. So we have hidden the company name field in WooCommerce, but no matter what code we write we cannot make it not require it to be filled out.
Try the filter woocommerce_default_address_fields instead.
function modify_woocommerce_default_address_fields( $fields ) {
$fields['company']['required'] = false;
return $fields;
}
add_filter( 'woocommerce_default_address_fields', 'modify_woocommerce_default_address_fields', 100, 1 );
It states this in the documentation that there are specific fields that must be manipulated through this filter.
country
first_name
last_name
company
address_1
address_2
city
state
postcode
We can use a filter to remove the notice before it is added to the array specific just to that error message.
function modify_woocommerce_notices( $message ) {
if( stripos( $message, 'Please fill out the billing company' ) !== false ) {
return '';
}
}
add_filter( 'woocommerce_add_error', 'modify_woocommerce_notices' );
I haven't tested that code but that's the general idea of how you would suppress the error and prevent the required field for the billing company.

How to conditionally add function to completed order when custom field to order_id is set in woocommerce

I try to add a function to the completed order email in woocommerce if a specific custom field is set.
I am certain the custom field is attached to the order, as i can output it on the admin order page.
so far i have:
function test(){
global $woocommerce, $post;
$check = get_post_meta( $order->ID, '_digital', TRUE);
if (!empty($check)){
function order_completed_email_add_cc_bcc( $headers, $email_id, $order ) {
if ( 'customer_completed_order' == $email_id ) {
$headers .= "Bcc: admin <admin#example.com>" . "\r\n";
}
return $headers;
}
add_filter( 'woocommerce_email_headers', 'order_completed_email_add_cc_bcc', 9999, 3 );
}
}
However, this doesn't work...
Ok i found it.
function order_completed_email_add_cc_bcc( $headers, $email_id, $order ) {
$check = $order->get_meta('_digital',true);
if ( 'customer_completed_order' == $email_id && !empty($check)) {
$headers .= "Bcc: admin <admin#example.com>" . "\r\n";
}
return $headers;
}
add_filter( 'woocommerce_email_headers', 'order_completed_email_add_cc_bcc', 9999, 3 );

Gravity Forms - Get a Date value and adjust it, make it into a merge tag

So what I want to be able to do is take a field value, a date field, and add a set period of time to it and then make that into a merge tag that I can then add back into that value or use else where.
I know how to make a new merge tag, that's not the issue. My question is how do I get a field value to use in that calculation?
add_filter( 'gform_replace_merge_tags', 'new_date_plus_30', 10, 7 );
function new_date_plus_30( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
$merge_tag = '{date_plus_30}';
$new_date = date('m/d/Y', strtotime('+30 days'));
return str_replace( $merge_tag, $new_date, $text );
}
So where I do the new date calculation, I need to be able to pull in a field from that post and use it.
I was also thinking of doing a If / Else script where I would do the date calculation based on what was set in a form. So if a user said repeat this every 15 days, I would have something like:
add_filter( 'gform_replace_merge_tags', 'new_date_plus_30', 10, 7 );
function new_date_plus_30( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
if ( $form_id == 34 && $field_id == 2 && $value == 'add 30 days') {
$merge_tag = '{date_plus_30}';
$new_date = date('m/d/Y', strtotime('+30 days'));
}
else if ( $form_id == 34 && $field_id == 2 && $value == 'first of month') {
$merge_tag = '{first_of_month}';
$new_date = date('m/d/Y', strtotime('first of next month'));
}
}
return str_replace( $merge_tag, $new_date, $text );
}
But my issue is still the same. How can I use two filters at the same time? I assume I need to use the gform_get_input_value. Kindly review my code and give feedback is there other way?
Or maybe something like this...
add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $options, $field, $raw_value ) {
if ( $field->id == '2' && $value == 'first of the month') {
$merge_tag = '{the_next_date}';
$thedatetochange = 'Not sure how to get the date value here...';
$value = date('m/d/Y', strtotime($thedatetochange . 'first of the next month'));
return $value;
}
else if ( $field->id == '2' && $value == 'the 15th') {
$merge_tag = '{the_next_date}';
$thedatetochange = 'Not sure how to get the date value here...';
$the_first_date = date('m/d/Y', strtotime($thedatetochange . 'first of the next month' ));
$value = date('m/d/Y', strtotime($the_first_date . '+15 days' ));
return $value;
}
}, 10, 5 );
So after doing more digging, would I be able to use something like this to get the value of the field?
$theDateToChange = rgar( $entry, ‘3’);
This assumes that the field 3 is a date value. Would this work for retrieving the current entry date?
The $entry is passed through the gform_replace_merge_tags filter. You can fetch any field value from the $entry by its field ID. For example, if your field ID was 1:
$value = $entry[1];
Alternately, if you're open to capturing this modified date as a secondary Date field in your form, we have a snippet that can handle the functionality for you.
https://gravitywiz.com/populate-dates-gravity-form-fields/
new GW_Populate_Date( array(
'form_id' => 1,
'target_field_id' => 2,
'modifier' => '+30 days'
) );
So here is my current working code...
add_action( 'gform_admin_pre_render', 'add_merge_tags' );
function add_merge_tags( $form ) {
?>
<script type="text/javascript">
gform.addFilter('gform_merge_tags', 'add_merge_tags');
function add_merge_tags(mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option){
mergeTags["custom"].tags.push({ tag: '{the_next_date}', label: 'The Next Date' });
return mergeTags;
}
</script>
<?php
//return the form object from the php hook
return $form;
}
add_action('wp', 'add_merge_tags');
/** MY MERGE TAGS HERE */
add_filter( 'gform_replace_merge_tags', 'new_date', 10, 7 );
function new_date( $value, $merge_tag, $options, $field, $raw_value, $entry, $text, $form, $url_encode, $esc_html, $nl2br, $format ) {
$pmoptions = $entry[7];
if ( $pmoptions == 'Monthly') {
$merge_tag = '{the_next_date}';
$old_date = $entry[2];
$new_date = date('m/d/Y', strtotime( $old_date . '+1 month'));
return str_replace( $merge_tag, $new_date, $text );
}
else if ( $pmoptions == 'Quarterly') {
$merge_tag = '{the_next_date}';
$old_date = $entry[2];
$new_date = date('m/d/Y', strtotime( $old_date . '+3 month'));
return str_replace($merge_tag, $new_date, $text);
}
}
apply_filters( 'gform_replace_merge_tags', $value, $merge_tag, $options, $field, $raw_value, $entry, $text, $form, $url_encode, $esc_html, $nl2br, $format );

How do I attach a pdf file to a Gravity Forms Notification?

Gravity forms offers a way to attach files from the file uploader (See code below), but how would I change this code to simply attach my own PDF file from either a hidden field value or simply paste the pdf file within this code? I tried a few things but it didn't work. Any help would be appreciated!
add_filter( 'gform_notification', 'change_user_notification_attachments', 10, 3 );
function change_user_notification_attachments( $notification, $form, $entry ) {
//There is no concept of user notifications anymore, so we will need to target notifications based on other criteria, such as name
if ( $notification['name'] == 'User Notification' ) {
$fileupload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) );
if(!is_array($fileupload_fields))
return $notification;
$attachments = array();
$upload_root = RGFormsModel::get_upload_root();
foreach( $fileupload_fields as $field ) {
$url = $entry[ $field['id'] ];
$attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $url );
if ( $attachment ) {
$attachments[] = $attachment;
}
}
$notification['attachments'] = $attachments;
}
return $notification;
}
Based on that code, something like this should work. Replace the $url value with the URL to your PDF.
add_filter( 'gform_notification', 'change_user_notification_attachments', 10, 3 );
function change_user_notification_attachments( $notification, $form, $entry ) {
if ( $notification['name'] == 'User Notification' ) {
$url = 'http://yoursite.com/path/to/file.pdf';
$notification['attachments'][] = $url;
}
return $notification;
}