Company field not required woocommerce [duplicate] - forms

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.

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

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

WooCommerce: Change payment gateway if Cart Total amount = 0

I want users to have a free checkout for my subscription service if they have 100% discount coupon code for the product. So if cart total gets equal to zero, then there should be no payment gateway and users should be able to signup directly.
I think WooCommerce don't allow working without payment gateway or can be difficult to modify, so I want to change the current payment gateway to "Cash on Delivery" if cart total equals to 0 . I shall change Cash on delivery text to something else later.
Here is link to my dummy development.
Use Couponcode: abcd1234
Here is the function that I'm using to hide payment gateways in my functions.php file :
add_filter( 'woocommerce_available_payment_gateways', 'paypal_100' );
function paypal_100($available_gateways) {
if ( WC()->cart->total == '0' ) {
unset( $available_gateways);
}
return $available_gateways;
}
Thanks in advance,
Yash
Well, after 6 hours of R & D here is the solution.
I made a minor change in plugins\woocommerce-subscriptions\classes\class-wc-subscriptions-cart.php And everything is working as I wanted. No payment method is required for subscription products if cart total equal to 0.
Before code (Line - 280 ):
public static function cart_needs_payment( $needs_payment, $cart ) {
if ( self::cart_contains_subscription() ) {
$is_for_one_period = ( self::get_cart_subscription_length() > 0 && self::get_cart_subscription_length() == self::get_cart_subscription_interval() ) ? true : false;
if ( $cart->total == 0 && false === $needs_payment && $cart->recurring_total > 0 && false === $is_for_one_period && 'yes' !== get_option( WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'no' ) ) {
$needs_payment = true;
}
}
After Code:
public static function cart_needs_payment( $needs_payment, $cart ) {
if ( self::cart_contains_subscription() ) {
$is_for_one_period = ( self::get_cart_subscription_length() > 0 && self::get_cart_subscription_length() == self::get_cart_subscription_interval() ) ? true : false;
if ( $cart->total == 0 && false === $needs_payment && $cart->recurring_total > 0 && false === $is_for_one_period && 'yes' !== get_option( WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'no' ) ) {
$needs_payment = false;
}
}
I just changed "$needs_payment = true;" to "$needs_payment = false;"
You can use the following code snippet.
So we check if the a specific pay method is available and check if the total amount in the shopping cart is equal to some amount. Then we can hide that payment method. In your case, you can hide all available methods.
function payment_gateway_disable_total_amount( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['paypal'] ) && $woocommerce->cart->total == 0 ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_total_amount' );

Custom Joomla 1.5 View cannot find the layout default.php file

I have been developing templates for Joomla 1.5 (obsolete one, yes I knew it. Unfortunately, I've been spending some time working with it and move on to newer version will be like starting from zero).
I tried to understand how Joomla 1.5 components work, especially for those using MVC principle, and somehow I managed to make things work, except now I have been stuck with the layout of a customized view. For unknown reason, Joomla could not read or find the layout (default.php) inside the folder com_hello2/views/viewed/tmpl and give '500 error Layout default not found'.
I tried to move the default.php file from folder to folder, looking if I could figure out where Joomla want me to put the default.php. It DOES work for the default view, located on com_hello2/views/hello2/tmpl.
Could anyone please help me with this problem?
Here are the codes from the controllers:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
class Hello2Controller extends JController
{
function display($tpl = null)
{
$task = JRequest::getVar('task', null, 'default', 'cmd');
switch ($task){
case 'view':
$model = &$this->getModel('Viewed');
$view = &$this->getView('viewed','html');
$mylayout = 'default';
break;
case 'newstatus':
break;
default:
$view = &$this->getView('hello2','html');
$model = &$this->getModel();
$mylayout = 'default';
break;
}
$view->setModel($model,true);
$view->setLayout($mylayout);
$view->display();
}
}
?>
I created two models, one is the default Hello2ModelHello2, the new one is Hello2ModelViewed. I seperated the two models based on the tasks in the controllers.
the Hello2ModelHello2:
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );
jimport('joomla.application.component.model');
class Hello2ModelHello2 extends JModel
{
//add CC
function getComments()
{
global $mainframe;
$db = JFactory::getDBO();
$id = JRequest::getVar('id', 0, '', 'int');
$query = 'SELECT a.*'.
' FROM #__hello1 AS a '.
' LEFT JOIN #__content AS b ON a.article_id = b.id'.
' WHERE a.state = 1';
$db->setQuery( $query );
$comments = $db->loadObjectList();
return $comments;
}
}
?>
now the Hello2ModelViewed
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );
jimport('joomla.application.component.model');
class Hello2ModelViewed extends JModel
{
function __construct()
{
parent::__construct();
$id = JRequest::getVar('id', 0, '', 'int');
$this->setId((int)$id);
}
function setId($id)
{
// Set new article ID and wipe data
$this->_id = $id;
$this->_article = null;
}
//get certain comment only.
function getSingle()
{
global $mainframe;
$db = JFactory::getDBO();
$id = JRequest::getVar('id', 0, '', 'int');
$query = 'SELECT a.*'.
' FROM #__hello1 AS a' .
' LEFT JOIN #__content AS b ON a.article_id = b.id'.
' WHERE a.id = '.$id.
' AND a.state = 1';
$db->setQuery( $query );
$comments = $db->loadObjectList();
return $comments;
}
}
?>
the view.html of Viewed View is
<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.view');
jimport('joomla.environment.request');
class Hello2View extends JView
{
function __construct($config = array())
{
parent::__construct($config);
}
}
class Hello2ViewViewed extends Hello2View{
function display($tpl = null)
{
$model = &$this->getModel();
$id = JRequest::getVar('id', 0, '', 'int');
$comments = $model->getSingle();
JRequest::setVar('row',$comments);
parent::display($tpl);
}
}
?>
and view.html of Hello2 View is
<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.view');
jimport('joomla.environment.request');
class Hello2ViewHello2 extends JView{
function display($tpl = null)
{
$model = &$this->getModel();
$comments = $model->getComments();
JRequest::setVar('rows',$comments);
parent::display($tpl);
if($this->getLayout() == 'view') {
$this->view($tpl);
return;
}
}
}
?>
It works for me to execute index.php?option=com_hello2&task=display
but gets error when execute index.php?option=com_hello2&task=view
eventhough the default.php is already inside the directory views/viewed/
any input appreciated! :)