Depending on the WordPress blog-URL, I need to define multiple variables (i.e. customer-group IDs). Some of the varaibles are integers and some are arrays. I want to use those variables in differnet functions within functions.php.
I need to know: is my solution good method, if I want to use multiple variables within different functions depending on the blog-URL?
I don’t want to use globals and constants don’t seem to work anymore in PHP-8 like they did in 7.2.
Function I - blog URL:
The variables change, depending on the blog URL. Function could look like this:
add_action( 'after_setup_theme', 'my_site_url' );
function my_site_url(){
$site_url = get_bloginfo('wpurl');
$stage_url = 'https://los.examplos.mx';
if ($site_url == $stage_url) {
/* - customer group ids for use in functions */
$wiederverkaeufer = 1234;
/* Versteckte Kategorien */
$hidecategory_wvk = [24, 863, 51, 87];
$return_array = [$wiederverkaeufer, $hidecategory_wvk];
} else {
/* - customer group ids for use in functions */
$wiederverkaeufer = 5678;
/* Versteckte Kategorien */
$hidecategory_wvk = [51, 48, 42];
$return_array = [$wiederverkaeufer, $hidecategory_wvk];
}
return $return_array;
}
Function II
Now I want to access those variables with some of the other functions within my functions.php. (This worked with constants before). For example
add_action( 'woocommerce_before_calculate_totals', 'cart_shipping_class_message', 20, 1 );
function cart_items_shipping_class_message( $cart ){
$my_ids = my_site_url();
$my_ids[0] = $wiederverkaeufer;
// Customer group current user
$group_id = BM_Conditionals::get_validated_customer_group();
// if is group "wiederverkaeufer" return
if ( $wiederverkaeufer == $group_id )
return;
//Do something
}
Function III: Test function:
I built a little test-function to check if I can call multiple variables and arrays from one function.
function my_shop_meldung(){
$a = "Moin";
$b = "Servus";
$c = "Hallo";
$d = ["22", "44", "55"];
$return_array = [$a, $b, $c, $d];
return $return_array;
}
function add_custom_text() {
?>
<div id="my_shopmeldung">
<?php
$my_results = my_shop_meldung();
$a = $my_results[0];
$b = $my_results[1];
$c = $my_results[2];
$d = $my_results[3][1];
//echo $a . $b . $c . $d;
echo $d;
?>
</div>
<?php
}
add_action('kt_beforeheader', 'add_custom_text', 10, 0);
I think this works as I could see the output "44" from the array.
Works for me:
This function shows product categories in WordPress Woocommerce, depending on the "MarketPress" customer group. Please note: this example will work only if you use the plugin Marketpress.
Function 1:
//add_action( 'wp_enqueue_scripts', 'my_site_url' );
add_action( 'after_setup_theme', 'my_site_url' );
function my_site_url(){
$site_url = get_bloginfo('wpurl');
//$site_url = get_home_url();
$stage_url = 'https://los.examplos.dk';
$stage_url_2 = 'https://los.examplos.dk';
//global $wiederverkaeufer, $hidecategory_wvk, $hidecategory_customer,
$boxkategorien, $gast_id, $kunde_id;
if ($site_url == $stage_url || $site_url == $stage_url_2 ) {
/* CONSTANTS - customer group ids for use in functions */
$wiederverkaeufer = 11111;
$gast_id = 22222;
$kunde_id = 33333;
/* Versteckte Kategorien */
$hidecategory_wvk = [11, 22, 33, 44];
$hidecategory_customer = [11, 22, 33, 44];
/* BOXEN */
$boxkategorien = = [11, 22, 33, 44];
} else {
/* CONSTANTS - customer group ids for use in functions */
$wiederverkaeufer = 3333;
$gast_id = 4444;
$kunde_id = 5555;
/* Versteckte Kategorien */
$hidecategory_wvk = = [11, 22, 33, 44];
$hidecategory_customer = = [11, 22, 33, 44];
/* BOXEN */
$box_kategorien = = [11, 22, 33, 44];];
}
$return_array = [
$wiederverkaeufer,
$gast_id,
$kunde_id,
$hidecategory_wvk,
$hidecategory_customer,
$boxkategorien
];
return $return_array;
}
Function II
Hide Woocommerce product categories depending on MarketPress customer group.
Vustomer group identified via "$group_id = BM_Conditionals::get_validated_customer_group();"
add_filter( 'get_terms', 'exclude_cats_from_shop', 10, 3 );
function exclude_cats_from_shop($terms, $taxonomies, $args ){
if ( is_admin() && ! defined('DOING_AJAX') )
return $terms;
$group_id = BM_Conditionals::get_validated_customer_group();
//get the customer group ID of current user
$customer_group = my_site_url();
//call function my_site_url(), get the array, and save to $customer_group
$wiederverkaeufer = $customer_group[0];
//save customer group id to $wiederverkaeufer
$hidecategory_wvk = $customer_group[3];
$hidecategory_customer = $customer_group[4];
//get those product group ids
if ( $wiederverkaeufer == $group_id ){
//if wiederverkaeufer
$new_terms = array();
//$hide_category_wvk = HIDECATEGORY_WVK;
// Ids der Produktkategorien die ausgeschlossen werden sollen
if ( in_array( 'product_cat', $taxonomies ) && is_shop() || is_product() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->term_id, $hidecategory_wvk ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}// Ende ist Wiederverkaeufer
// if is customer group customer
else {
$new_terms = array();
// hide productcategories
if ( in_array( 'product_cat', $taxonomies ) && is_shop() || is_product() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->term_id, $hidecategory_customer ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
} //End
I'm trying to write php code for woocommerce that would check if specific product is in cart and if it is true, then some checkout fields would be disabled. What I have now:
add_action( 'woocommerce_before_checkout_form', 'find_product_in_cart' );
function find_product_in_cart() {
$product_id = 989; //product id which would trigger
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id ) $in_cart = true; // checks if the product is in cart
}
if ( $in_cart ) {
add_filter( 'woocommerce_billing_fields', 'woo_filter_state_billing');
function woo_filter_state_billing( $address_fields ) {
//sets fields to not required
$address_fields['billing_state']['required'] = false;
$address_fields['billing_country']['required'] = false;
$address_fields['billing_address_1']['required'] = false;
$address_fields['billing_city']['required'] = false;
$address_fields['billing_postcode']['required'] = false;
$address_fields['billing_phone']['required'] = false;
return $address_fields;
}
add_filter( 'woocommerce_checkout_fields' , 'disabling' );
function disabling($fields){
//hides fields
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_phone']);
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_state']);
return $fields;
}
}
}
So the problem is that for some reason fields stay required even if hidden. If I cut out the whole:
add_filter( 'woocommerce_billing_fields', 'woo_filter_state_billing');
function woo_filter_state_billing( $address_fields ) {
//sets fields to not required
$address_fields['billing_state']['required'] = false;
$address_fields['billing_country']['required'] = false;
$address_fields['billing_address_1']['required'] = false;
$address_fields['billing_city']['required'] = false;
$address_fields['billing_postcode']['required'] = false;
$address_fields['billing_phone']['required'] = false;
return $address_fields;
}
and paste it just as it is in functions.php everything works just fine, but it applies to every product and that is not good.
So, how could I make it work?
So I managed to redo everything in a cleaner way and it works!
add_filter( 'woocommerce_checkout_fields' , 'disabling' );
function disabling($fields){
$product_id = 989; //product id which would trigger
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id ) $in_cart = true; // checks if the product is in cart
}
if ( $in_cart ){
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_phone']);
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_state']);
return $fields;
}
else {
return $fields;
}
}
So in the end I think that unset function failed to work properly because of the top add_action:
add_action( 'woocommerce_before_checkout_form', 'find_product_in_cart' );
Trying to always have the "Ship to a different adress" cleared when a user is buying in WooCommerce Checkout. The following code is though doing aboslutely nothing. Does it have todo with cookies?
add_filter( 'woocommerce_checkout_get_value', 'remove_clear_checkout_shipping_fields', 10, 2 );
function remove_clear_checkout_shipping_fields( $value, $input ) {
if ( strpos( $input, 'shipping_' ) !== FALSE ) {
$value = '';
}
}
This way you can modify each field:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_first_name'] = '';
$fields['shipping']['shipping_last_name'] = '';
$fields['shipping']['shipping_company'] = '';
$fields['shipping']['shipping_address_1'] = '';
$fields['shipping']['shipping_address_2'] = '';
$fields['shipping']['shipping_city'] = '';
$fields['shipping']['shipping_postcode'] = '';
$fields['shipping']['shipping_country'] = '';
$fields['shipping']['shipping_state'] = '';
return $fields;
}
Just remove unnecessary fields.
Where is the .php file located to edit the billing fields?
I want to change the text for each heading; for example Last Name to Family Name.
This is using Woocommerce in the Avada theme.
I've managed to change the fields using this code, but city, state and postcode won't change:
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_first_name']['label'] = '名';
$fields['billing']['billing_last_name']['label'] = '姓';
$fields['billing']['billing_email']['label'] = 'メールアドレス';
$fields['billing']['billing_phone']['label'] = '電話番号';
$fields['billing']['billing_country']['label'] = '国';
$fields['billing']['billing_city']['label'] = '名!';
$fields['billing']['billing_city']['placeholder'] = '市町村';
$fields['billing']['billing_state']['label'] = '都道府県';
$fields['billing']['billing_state']['placeholder'] = '都道府県';
$fields['billing']['billing_postcode']['label'] = '郵便番号';
$fields['billing']['billing_postcode']['placeholder'] = '郵便番号';
$fields['billing']['billing_address_1']['label'] = '住所';
$fields['billing']['billing_address_1']['placeholder'] = '住所';
$fields['billing']['billing_address_2']['placeholder'] = 'アパート名等';
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
Try increase your priority used:
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields', 50 );
Can't find working solution that would add an example column to backend Orders list. Already tried these (within child theme's functions.php):
add_filter('manage_edit-shop_order_columns', 'extra_column');
function extra_column($columns) {
$columns['title'] = 'Product-x';
return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION($columns){
$new_columns = (is_array($columns)) ? $columns : array();
unset( $new_columns['order_actions'] );
//edit this for you column(s)
//all of your columns will be added before the actions column
$new_columns['MY_COLUMN_ID_1'] = 'MY_COLUMN_1_TITLE';
// $new_columns['MY_COLUMN_ID_2'] = 'MY_COLUMN_2_TITLE';
//stop editing
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION($column){
global $post;
$data = get_post_meta( $post->ID );
//start editing, I was saving my fields for the orders as custom post meta
//if you did the same, follow this code
if ( $column == 'MY_COLUMN_ID_1' ) {
echo (isset($data['MY_COLUMN_1_POST_META_ID']) ? $data['MY_COLUMN_1_POST_META_ID'] : '');
}
if ( $column == 'MY_COLUMN_ID_2' ) {
echo (isset($data['MY_COLUMN_2_POST_META_ID']) ? $data['MY_COLUMN_2_POST_META_ID'] : '');
}
}
add_filter( 'manage_edit-shop_order_columns', 'imarcon_set_custom_column_order_columns');
function imarcon_set_custom_column_order_columns($columns) {
// global $woocommerce;
$nieuwearray = array();
foreach($columns as $key => $title) {
if ($key=='billing_address') // in front of the Billing column
$nieuwearray['order_producten'] = __( 'Products', 'woocommerce' );
$nieuwearray[$key] = $title;
}
return $nieuwearray ;
}
I am using WP 4.1.1, Avada 3.7.3 with Child Theme enabled, Gravity Forms 1.8.22 + few WooCommerce addons and other plugins.
OK got it - Woocommerce update solved it.