Redirect to different thank you pages on specific shipping method selection - redirect

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;
}
}

Related

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 do you specify timeout in perl get requests?

I'm pretty new to Perl so I might be missing something obvious but; I'm debugging a bug and I've narrowed down the problem to the following piece of code.
my $fetch_urls = [];
for my $input_medium ( #{ $input_media } )
{
$input_medium->{ medium } = MediaWords::DBI::Media::Lookup::find_medium_by_url( $db, $input_medium->{ url } );
if ( $input_medium->{ medium } )
{
$input_medium->{ status } = 'existing';
}
else
{
if ( MediaWords::Util::URL::is_http_url( $input_medium->{ url } ) )
{
push( #{ $fetch_urls }, $input_medium->{ url } );
}
else
{
WARN "URL is not HTTP(s): " . $input_medium->{ url };
}
}
}
the piece of code is supposed to go through all the input_media which are URLs and check if it exists in the system if not it tries to check if the URL is valid using the is_http_url function (at least this is what I think it's doing). What I want to do is add a timeout after which if the URL hasn't responded, I push the message URL was unreachable
Any Ideas/suggestions will be highly appreciated.

how to block direct access using url to multiple thank you pages?

I have multiple contact forms in my wordpress website and have a different thankyou page for each. So, i want to prevent direct access of thankyou pages by just entering urls. I want if the user fills the form than it is only redirected to that forms thankyou page.
I have working code for 1 thankyou page but i don't know how to set up for multiple contact forms & thankyou pages.
if ( ! is_page(1911)) {
return;
}
// coming from the form, so all is fine
if (wp_get_referer() == 'https://www.example.com/contact') {
return;
}
// we are on thank you page
// visitor is not coming from form
// so redirect to home
wp_redirect( get_home_url() );
exit;
} );
Other thankyou page id's: 1269, 1825, 1623
Other contact form page urls: https://www.example.com/contact-form-2, https://www.example.com/contact-form-3, https://www.example.com/contact-form-3
I have solved the issue by myself. I am posting the solution here in case if someone gets the same problem.
function wpse15677455_redirect() {
$ref = wp_get_referer();
if (is_page(1911) && $ref !== "https://www.example.com/contact"){
wp_redirect( get_home_url() );
}
else if(is_page(1269) && $ref !== "https://www.example.com/contact-form-2"){
wp_redirect( get_home_url() );
}
else if(is_page(1825) && $ref !== "https://www.example.com/contact-form-3"){
wp_redirect( get_home_url() );
}
else if(is_page(1623) && $ref !== "https://www.example.com/contact-form-4"){
wp_redirect( get_home_url() ); exit();
};

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 );

How to get order id in Woocomerce 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>';
}