unable to get value from global variable in php - php

This is the code I have in one of my php plugin file.
add_filter('woocommerce_cart_totals_order_total_html','test_func');
function test_func() {
global $woocommerce, $totalship;
$cart_subtotal = (float)$woocommerce->cart->subtotal;
if( $cart_subtotal < 1000 ) {
$cart_subtotal01 = $woocommerce->cart->get_cart_subtotal();
$cart_subtotal11 = explode('</span>', $cart_subtotal01);
$text_tax ='';
if($cart_subtotal11[1]) {
$text_tax = $cart_subtotal11[1];
}
$allcarttotal = $cart_subtotal+$totalship;
$value = '<strong><span class="amount">Rs. ' . $allcarttotal . '</span>'.$text_tax.'</strong>';
$citrus_total_val = $value;
return $citrus_total_val;
//return $value;
}
else {
$docart_total = $cart_subtotal - $totalship;
$citrus_total_val = $docart_total;
return $citrus_total_val;
//return $docart_total;
}
}
global $citrus_total_val;
I am trying to pass the value of $citrus_total_val to another plugin for payment gateway.
This is the code:
global $citrus_total_val;
//Setup URL and signatue etc.
$currencycode = get_woocommerce_currency();
$merchantTxnId = $order_id;
$orderAmount = $citrus_total_val;
But the value is not passed here. What am I doing wrong?

Try this:
$citrus_total_val = '';
add_filter('woocommerce_cart_totals_order_total_html','test_func');
function test_func() {
global $woocommerce, $totalship;
global $citrus_total_val;
$cart_subtotal = (float)$woocommerce->cart->subtotal;
if( $cart_subtotal < 1000 ) {
$cart_subtotal01 = $woocommerce->cart->get_cart_subtotal();
$cart_subtotal11 = explode('</span>', $cart_subtotal01);
$text_tax ='';
if($cart_subtotal11[1]) {
$text_tax = $cart_subtotal11[1];
}
$allcarttotal = $cart_subtotal+$totalship;
$value = '<strong><span class="amount">Rs. ' . $allcarttotal . '</span>'.$text_tax.'</strong>';
$citrus_total_val = $value;
return $citrus_total_val;
//return $value;
}
else {
$docart_total = $cart_subtotal - $totalship;
$citrus_total_val = $docart_total;
return $citrus_total_val;
//return $docart_total;
}
}

try putting it in the function, like you did with global $woocommerce, $totalship;
add_filter('woocommerce_cart_totals_order_total_html','test_func');
function test_func() {
global $woocommerce, $totalship;
global $citrus_total_val;
$cart_subtotal = (float)$woocommerce->cart->subtotal;
if( $cart_subtotal < 1000 ) {
$cart_subtotal01 = $woocommerce->cart->get_cart_subtotal();
$cart_subtotal11 = explode('</span>', $cart_subtotal01);
$text_tax ='';
if($cart_subtotal11[1]) {
$text_tax = $cart_subtotal11[1];
}
$allcarttotal = $cart_subtotal+$totalship;
$value = '<strong><span class="amount">Rs. ' . $allcarttotal . '</span>'.$text_tax.'</strong>';
$citrus_total_val = $value;
return $citrus_total_val;
//return $value;
}
else {
$docart_total = $cart_subtotal - $totalship;
$citrus_total_val = $docart_total;
return $citrus_total_val;
//return $docart_total;
}
}

Related

How to change direction of calculation for values in a table using PHP

I have a table with columns
-Deposits
-Withdrawals
-Running Balance
I am using the values inside the first two columns to calculate the Running balance in the Running Balance column.
The issue.
The calculation is working fine - just that it's doing so in reverse. If you look at the "Entry Id" column, I have the entry Ids which I would like to use for the calculation, or whichever way works. If we use the Entry ID column then I need the calculation to happen from the smallest to the largest Entry ID.
Here is my code.
function display_form_entries_shortcode() {
global $wpdb;
$form_id = 73;
$current_user_id = get_current_user_id();
$entries = FrmEntry::getAll(array('form_id' => $form_id, 'user_id' => $current_user_id), ' ORDER BY created_at DESC', '', true, 999999);
if(empty($entries)) {
return 'No entries found for form ID '.$form_id.' for user ID '.$current_user_id;
}
$form_fields = FrmField::get_all_for_form($form_id);
$field_labels = array();
foreach ($form_fields as $field) {
$field_labels[$field->id] = $field->name;
}
$table_name_n = $wpdb->prefix . 'frm_fields';
$table_name = $wpdb->prefix . 'frm_item_metas';
// Add the new columns
$field_labels[787] = "Deposits";
$field_labels[788] = "Withdrawals";
$field_labels[789] = "Running Balance";
$output = '<table>';
$output .= '<tr><th>' . implode('</th><th>', $field_labels) . '</th></tr>';
$running_balance = 0;
global $m_field;
foreach ($entries as $entry) {
$entry_values = array();
foreach ($field_labels as $field_id => $field_label) {
$field_value = '';
if ($field_id == 746) { //return "Select Account" field
$row = $wpdb->get_row( "SELECT * FROM $table_name_n WHERE field_key = 'f2ego'" );
$options = unserialize( $row->options );
$fieldi_value = $options[FrmEntryMeta::get_entry_meta_by_field($entry->id, $field_id)];
}
//Running Balance Calculations
if ($field_id == 787) {
if ($entry->metas[783] == 'Money In' && $fieldi_value == $m_field) {
$field_value = $entry->metas[727] ? $entry->metas[727] : $entry->metas[738];
if ($entry->metas[736]) {
$field_value -= $entry->metas[736];
}
$field_value = $field_value > 0 ? $field_value : 0;
$running_balance += $field_value;
} else {
$field_value = 0;
}
} else if ($field_id == 788 && $fieldi_value == $m_field) {
if ($entry->metas[783] == 'Money Out') {
$field_value = $entry->metas[727] ? -$entry->metas[727] : -$entry->metas[738];
if ($entry->metas[736]) {
$field_value += $entry->metas[736];
}
$field_value = $field_value < 0 ? $field_value : 0;
$running_balance += $field_value;
} else {
$field_value = 0;
}
}
else if ($field_id == 789 && $fieldi_value == $m_field) {
$field_value = $running_balance;
}
else {
$field_value = FrmEntryMeta::get_entry_meta_by_field($entry->id, $field_id);
}
//End of calculations
$entry_values[] = $field_value;
}
if(!in_array($m_field, [$entry_values[1], $entry_values[11], $entry_values[16]])) {
continue;
}
if (!empty($entry_values)) {
$output .= '<tr><td>' . implode('</td><td>', $entry_values) . '</td></tr>';
}
}
$output .= '</table>';
return $output;
}
add_shortcode('display_form_entries', 'display_form_entries_shortcode');
I have run out of options trying to force the calculation in order of the Entry IDs. Any assistance is welcome.

Laravel - Refactor a giant if-statement

I have an app where the user is able to choose themes and plugins but they are also able to choose none of the two. So to check all the possible options I need a 4 clause if-statement to check every situation. In the meantime, this if-statement grew exponentially and now it's very large. I used Repository for some bits to refactor the if-statement a bit but its still way too large and uclear. This is the if-statement
public function store(Request $request)
{
$selectedPlugin = null;
$selectedPlugins = array();
$price = null;
foreach($request->input('plugin') as $key => $value) {
if ($value === 'selected') {
$selectedPlugin = $key;
$plugin = Product::find($selectedPlugin);
if($plugin == null)
{
continue;
} elseif ($plugin != null) {
$price += $plugin->price;
echo "ID: " . $plugin->id . "<br>";
$selectedPlugins[$plugin->id] = $plugin->toArray();
$request->session()->put('chosen_plugins.' . 'PluginID' . $plugin->id, $plugin->toArray());
$request->session()->put('chosen_plugins.' . 'PluginID' . $plugin->id .'.composer_package', $plugin->productable->composer_package);
}
}
}
if(session()->exists('chosen_plugins') == true) {
$products = Session::get('chosen_plugins');
if(session()->exists('chosen_theme') == true)
{
$products['theme'] = Session::get('chosen_theme');
$themePrice = Session::get('chosen_theme')['price'];
$subtotal = $price + $themePrice;
$vat = 21/100 * $subtotal;
$priceSum = $subtotal + $vat;
} elseif (session()->exists('chosen_theme') == false) {
$subtotal = $price;
$vat = 21/100 * $subtotal;
$priceSum = $subtotal + $vat;
}
$data = [$subtotal, $vat, $priceSum];
$order = $this->_orderRepository->createOrderIfSessionExist($data, $products);
return redirect()->to('/ordersummary/'. $order->id);
} elseif (session()->exists('chosen_plugins') == false ) {
if(session()->exists('chosen_theme') == true)
{
$theme = Session::get('chosen_theme');
$subtotal = $theme['price'];
$vat = 21/100 * $subtotal;
$priceSum = $subtotal + $vat;
} elseif (session()->exists('chosen_theme') == false) {
$subtotal = 35;
$vat = 21/100 * $subtotal;
$priceSum = $subtotal + $vat;
}
$data = [$subtotal, $vat, $priceSum];
$order = $this->_orderRepository->createOrderIfSessionDoesntExist($data);
if (session()->exists('chosen_theme') == true) {
$orderItems = new Orderitems;
$orderItems->order_id = $order->id;
$orderItems->product_id = $theme['id'];
$orderItems->price = $theme['price'];
$orderItems->save();
return redirect()->to('/ordersummary/'. $order->id);
} elseif (session()->exists('chosen_theme') == false) {
return redirect()->to('/ordersummary/'. $order->id);
}
}
}
I need some help to refactor this if-statement. Thanks in advance!

Invalid argument on php file

I'm getting an error that says:
"Warning: Invalid argument supplied for foreach() in /home/content/a2pewpnas01_data01/19/3920219/html/wp-content/plugins/estimated-dispatch-date-woocommerce/includes/plugin-functions.php on line 98"
The line 98 is this: foreach($eddwc_holiday as $eddwc_hday){
Here is the complete code:
<?php
if(!function_exists('eddwc_option')){
function eddwc_option($key){
$value = EDDWC()->get_option($key);
return $value;
}
}
if(!function_exists('eddwc_update_variable')){
function eddwc_update_variable($post_id,$value){
update_post_meta( $post_id, EDDWCP_METAKEY.'_variable', wc_clean($value) );
}
}
if(!function_exists('eddwc_update_simple')){
function eddwc_update_simple($post_id,$value){
update_post_meta( $post_id, EDDWCP_METAKEY.'_simple', wc_clean($value) );
}
}
if(!function_exists('eddwc_update_variation')){
function eddwc_update_variation($post_id,$value){
update_post_meta( $post_id, EDDWCP_METAKEY.'_variation', wc_clean($value) );
}
}
if(!function_exists('eddwc_get_variation')){
function eddwc_get_variation($post_id){
return get_post_meta($post_id,EDDWCP_METAKEY.'_variation',true);
}
}
if(!function_exists('eddwc_get_variable')){
function eddwc_get_variable($post_id){
return get_post_meta($post_id,EDDWCP_METAKEY.'_variable',true);
}
}
if(!function_exists('eddwc_get_simple')){
function eddwc_get_simple($post_id){
return get_post_meta($post_id,EDDWCP_METAKEY.'_simple',true);
}
}
if(!function_exists('eddwc_get_external')){
function eddwc_get_external($post_id){
return get_post_meta($post_id,EDDWCP_METAKEY.'_external',true);
}
}
if(!function_exists('eddwc_get_grouped')){
function eddwc_get_grouped($post_id){
return get_post_meta($post_id,EDDWCP_METAKEY.'_grouped',true);
}
}
if(!function_exists('eddwc_get_actual_date')){
function eddwc_get_actual_date($value){
$date = eddwc_get_dispatch_date($value);
return $date;
}
}
if(!function_exists('eddwc_get_general_date')){
function eddwc_get_general_date($value,$seperator = ' - '){
$general_option = eddwc_option('product_general_date_settings');
$val = explode(',', $value);
$final_date = '';
if(isset($val[0]) && !isset($val[1])){
$final_date = $val[0];
} else if(isset($val[0]) && isset($val[1])){
if($val[0] == $val[1]){
$final_date = $val[0];
} else {
if(isset($general_option['actual_date'])){
$final_date = eddwc_get_dispatch_date($val[0]) .$seperator. eddwc_get_dispatch_date($val[1]);
} else {
$final_date = $val[0] .$seperator. $val[1];
}
}
}
return $final_date;
}
}
if(!function_exists('eddwc_get_static_date')){
function eddwc_get_static_date($date = ''){
$eddwc_range = explode(',' , $date);
if(count($eddwc_range) > 1){
if(isset($eddwc_range[0]) && isset($eddwc_range[1]) && ($eddwc_range[0] > $eddwc_range[1]) ){
$eddwc_range = $eddwc_range[0];
} else {
$eddwc_range = $eddwc_range[1];
}
} else {
if(isset($eddwc_range[0])){
$eddwc_range = $eddwc_range[0];
}
}
return $eddwc_range;
}
}
if(!function_exists('eddwc_get_dispatch_date')){
function eddwc_get_dispatch_date($date) {
$eddwc_holiday = eddwc_option('holiday');
$eddwc_holidays = array();
foreach($eddwc_holiday as $eddwc_hday){
$eddwc_holidays[] = $eddwc_hday['date'];
}
$eddwc_workdays = eddwc_option('operation_days');
$cutOff = eddwc_option('day_cutoftime');
list($cut_hrs,$cut_min) = explode(':',$cutOff);
$cut_hrs = intval($cut_hrs);
$cut_min = intval($cut_min);
$wp_timezone_string = get_option('timezone_string');
$wp_timezone_offset = get_option('gmt_offset');
if ($wp_timezone_string) {
$eddwc_timezone = $wp_timezone_string;
} else {
$eddwc_timezone = ini_get('date.timezone');
}
date_default_timezone_set($eddwc_timezone);
$eddwc_date = new DateTime;
$eddwc_cut_off = $cutOff;
$eddwc_time = clone $eddwc_date;
$eddwc_time->setTime($cut_hrs,$cut_min);
$eddwc_next_date = clone $eddwc_date;
if ($eddwc_date >= $eddwc_time){
$eddwc_next_date->modify('+1 day');
}
$i = 0;
while ($i < $date){
$eddwc_next_date->modify('+1 day');
$ndate = strtolower($eddwc_next_date->format('D'));
if (in_array($ndate, $eddwc_workdays)) {
//$i++;
if (in_array($eddwc_next_date->format('d-m-Y'), $eddwc_holidays) == false) {
$i++;
}
}
}
return $eddwc_next_date->format(eddwc_option('date_display_format'));
}
}
?>
What is wrong with this code?
Thanks

Function/filter/action to replace product title for woocommerce

I have this function
function my_product_title($title, $id)
{
if(in_the_loop() && is_product())
{
return '<span class="border">FooBar</span>';
}
return $title;
}
add_filter( 'the_title', 'my_product_title', 5, 2);
and it can replace the product title with return '<span class="border">FooBar</span>';.
I also have a custom script in "mycustomtitle.php" that can modify the products titles and my script can echo that modified title as $mycustomtitle
I want to replace the original product title with my $mycustomtitle without changing anything in the core files.
I've tried to just change return '<span class="border">FooBar</span>'; to $mycustomtitle but it only removes the original title and gives no output at all...
Thanks!
UPDATE 2016-10-20 With custom code:
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/seo-engine/explode.php';
$tit1 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');
$tit2 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');
$title_keys = array($tit1,$tit2);
$title_key11 = $title_keys[array_rand($title_keys)];
if(!function_exists(spin11)){
function spin11($string11) {
while(true) {
if(!preg_match_all('/({([^\{]*?)\})/', $string11, $matches))
break;
foreach($matches[2] as $i => $match) {
$parts = explode('|', $match);
$string11 = str_replace_once11($matches[0][$i], $parts[mt_rand(0, count($parts)-1)], $string11);
}
}
return $string11;
}
}
if(!function_exists(str_replace_once11)){
function str_replace_once11($from,$to,$str)
{
$str = explode($from,$str,2);
return $str[0].$to.$str[1];
}
$title_id11 = get_the_ID();
$fileLocation11 = getenv("DOCUMENT_ROOT") . '/wp-content/plugins/seo-controlpanel/seo-cache/product-title-h1/'.$title_id11.'.txt';
if(!file_exists($fileLocation11)){
$file11 = fopen($fileLocation11,"w");
$content11 = spin11($title_key11);
fwrite($file11,$content11);
fclose($file11);
}
if(file_exists($fileLocation11)){
$myFile11 = $fileLocation11;
$fh11 = fopen($myFile11, 'r');
$theData11 = fread($fh11, filesize($myFile11));
fclose($fh11);
}
}
?>
Insert your custom code in your function.
function my_product_title($title, $id) {
include $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/seo-engine/explode.php';
$tit1 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');
$tit2 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');
$title_keys = array($tit1,$tit2);
$title_key11 = $title_keys[array_rand($title_keys)];
if(!function_exists(spin11)){
function spin11($string11) {
while(true) {
if(!preg_match_all('/({([^\{]*?)\})/', $string11, $matches))
break;
foreach($matches[2] as $i => $match) {
$parts = explode('|', $match);
$string11 = str_replace_once11($matches[0][$i], $parts[mt_rand(0, count($parts)-1)], $string11);
}
}
return $string11;
}
}
if(!function_exists(str_replace_once11)){
function str_replace_once11($from,$to,$str)
{
$str = explode($from,$str,2);
return $str[0].$to.$str[1];
}
$title_id11 = get_the_ID();
$fileLocation11 = getenv("DOCUMENT_ROOT") . '/wp-content/plugins/seo-controlpanel/seo-cache/product-title-h1/'.$title_id11.'.txt';
if(!file_exists($fileLocation11)){
$file11 = fopen($fileLocation11,"w");
$content11 = spin11($title_key11);
fwrite($file11,$content11);
fclose($file11);
}
if(file_exists($fileLocation11)){
$myFile11 = $fileLocation11;
$fh11 = fopen($myFile11, 'r');
$theData11 = fread($fh11, filesize($myFile11));
fclose($fh11);
}
}
if(in_the_loop() && is_product()) {
return $theData11;
}
return $title;
}
add_filter( 'the_title', 'my_product_title', 5, 2);
Is $mycustomtitle global variable or it is defined inside another function/method/class?
If it is global variable this may help:
function my_product_title($title, $id)
{
global $mycustomtitle;
if(in_the_loop() && is_product())
{
return $mycustomtitle;
}
return $title;
}
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/seo-engine/explode.php';
$tit1 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');
$tit2 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');
$title_keys = array($tit1,$tit2);
$title_key11 = $title_keys[array_rand($title_keys)];
if(!function_exists(spin11)){
function spin11($string11) {
while(true) {
if(!preg_match_all('/({([^\{]*?)\})/', $string11, $matches))
break;
foreach($matches[2] as $i => $match) {
$parts = explode('|', $match);
$string11 = str_replace_once11($matches[0][$i], $parts[mt_rand(0, count($parts)-1)], $string11);
}
}
return $string11;
}
}
if(!function_exists(str_replace_once11)){
function str_replace_once11($from,$to,$str)
{
$str = explode($from,$str,2);
return $str[0].$to.$str[1];
}
$title_id11 = get_the_ID();
$fileLocation11 = getenv("DOCUMENT_ROOT") . '/wp-content/plugins/seo-controlpanel/seo-cache/product-title-h1/'.$title_id11.'.txt';
if(!file_exists($fileLocation11)){
$file11 = fopen($fileLocation11,"w");
$content11 = spin11($title_key11);
fwrite($file11,$content11);
fclose($file11);
}
if(file_exists($fileLocation11)){
$myFile11 = $fileLocation11;
$fh11 = fopen($myFile11, 'r');
$theData11 = fread($fh11, filesize($myFile11));
fclose($fh11);
}
}
//echo $theData11;
?>

Woocommerce - Change currency depending on billing country

I'm trying to change the currency depending on the customer's billing country. Is it possible to do that maybe with an override of the "tax calculation" function as it is updated when the customer change the country ?
At the moment, I have this function (function.php) :
add_filter('wcml_client_currency','change_currency');
function change_currency($client_currency){
global $woocommerce;
$country = $woocommerce->customer->get_country();
if($country == 'CH') {
$client_currency = 'CHF'; //currency code
} else {
$client_currency = 'EUR'; //currency code
}
return $client_currency;
}
Thanks for your help.
You can use this following code:
add_filter('woocommerce_get_price', 'return_custom_price', $product, 2);
function return_custom_price($price, $product) {
global $post, $woocommerce;
$tot_qty = $woocommerce->cart->cart_contents_count;
// Array containing country codes
$county = array('NZ', 'AU', 'GB', 'IE');
// Amount to increase by
//$amount = 10;
change_existing_currency_symbol('NZD$', 'NZD');
if($woocommerce->customer->get_shipping_country() == 'NZ'){
$amount = 0.00;
$amount = 26.19;
}
else if($woocommerce->customer->get_shipping_country() == 'GB'){
$amount = 0.00;
$amount = 19.04;
}
else if($woocommerce->customer->get_shipping_country() == 'IE'){
$amount = 0.00;
$amount = 19.04;
}
else {
$amount = 0.00;
$amount = 28.19;
}
//echo $amount;
// If the custromers shipping country is in the array
if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ){
// Return the price plus the $amount
return $new_price = $amount;
die();
} else {
// Otherwise just return the normal price
return $price;
die();
}
}
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
global $post, $woocommerce;
$my_country = $woocommerce->customer->get_shipping_country();
switch( $my_country ) {
case 'GB': $currency_symbol = '£';
break;
case 'NZ': $currency_symbol = '$';
break;
case 'IE': $currency_symbol = '€';
break;
default:
$currency_symbol = '$';
}
return $currency_symbol;
}

Categories