php troubles with coding variables - php

I am trying to echo certain values if the variable $cardtype ==
$paymentmethod = if( $cardtype == 'visa' ) echo 'VSA';
elseif ( $cardtype == 'mastercard' ) echo 'MSC';
elseif ( $cardtype == 'mastercard' ) echo 'MSC';
elseif ( $cardtype == 'maestro' ) echo 'MAE';
elseif ( $cardtype== 'amex' ) echo 'AMX';
How would I do this???

$types = array( 'visa' => 'VSA', 'mastercard' => 'MSC',
'maestro' => 'MAE', 'amex' => 'AMX' );
echo ( isset( $types[ $cardtype ] ) ) ? $types[ $cardtype ] : 'Wrong card type';

You could use a function containing a switch statement for this:
function GetPaymentMethod( $cardtype )
{
switch( $cardtype )
{
case 'visa':
return 'VSA';
case 'mastercard':
return 'MSC';
case 'maestro':
return 'MAE';
case 'amex':
return 'AMX';
default:
return '<Invalid card type>';
}
}
Test:
echo GetPaymentMethod( 'visa' ); // VSA

Here is one way to do it:
switch($cardtype) {
case 'visa':
echo 'VSA';
break;
case 'mastercard':
echo 'MSC';
break;
}
And so on

for your own code, you have to just remove strange $paymentmethod = from the beginning.
if( $cardtype == 'visa' ) echo 'VSA';
elseif ( $cardtype == 'mastercard' ) echo 'MSC';
elseif ( $cardtype == 'maestro' ) echo 'MAE';
elseif ( $cardtype== 'amex' ) echo 'AMX';
it will work too.

Related

syntax error, unexpected '&&' (T_BOOLEAN_AND)

I wonder what's wrong with this code:
if ( isset( $_POST['add-to-cart'] ) ) && $matched_id = (int) $_POST['add-to-cart'] ) {
$url = 'https://website.com/cart/?add-to-cart=363053';
}
It returned a syntax error, unexpected '&&' (T_BOOLEAN_AND)
You missed some brackets...
Try adding brackets like :
if ( (isset( $_POST['add-to-cart'])) && ($matched_id = (int) $_POST['add-to-cart']) ) {
$url = 'https://website.com/cart/?add-to-cart=363053';
}else{
echo "Error";
}
Or removing some brackets like :
if ( isset( $_POST['add-to-cart'] ) && $matched_id = (int) $_POST['add-to-cart'] ) {
$url = 'https://website.com/cart/?add-to-cart=363053';
}else{
echo "Error";
}
Remove following part from example I have given when issue fixed...
else{
echo "Error";
}
if ( isset( $_POST['add-to-cart'] ) && $matched_id = (int) $_POST['add-to-cart'] ) { $url = 'https://website.com/cart/?add-to-cart=363053'; }
I think that you have to remove the bracket.

Replace meta value

I searched for a while and I tried several options but I didn't find any solution. So, i have the following string that gets the status of a costum field to export to other websites.
<category><![CDATA[<?php listingpress_listing_status(); ?>]]></category>
It outputs something like this:
<category><![CDATA[For sale]]></category>
And what i need is to turn this value into a number.
Ex:
For sale » 100
For Rent » 110
Sold » 120
This is the original function:
if ( ! function_exists( 'listingpress_listing_status' ) ) :
/**
* Prints listing status
*
* #since ListingPress 1.0
*
* #uses listingpress_get_listing_status() To get listing status
*/
function listingpress_listing_status() {
echo listingpress_get_listing_status( 'name' );
}
endif; // listingpress_listing_status
if ( ! function_exists( 'listingpress_get_listing_status' ) ) :
function listingpress_get_listing_status( $fields = 'name' ) {
global $meta_prefix, $post;
if ( of_get_option( 'enable_listing_status', true ) ) {
$status = get_post_meta( $post->ID, $meta_prefix . 'status', true );
if ( $status == 'sold' ) {
if ( $fields == 'name' )
return __( 'Sold', 'listingpress' );
elseif ( $fields == 'slug' )
return 'sold';
} elseif ( $status == 'for-sale' ) {
if ( $fields == 'name' )
return __( 'For sale', 'listingpress' );
elseif ( $fields == 'slug' )
return 'for-sale';
} elseif ( $status == 'for-rent' ) {
if ( $fields == 'name' )
return __( 'For rent', 'listingpress' );
elseif ( $fields == 'slug' )
return 'for-rent';
}
} else {
return 'no-status';
}
}
endif; // listingpress_get_listing_status
Could someone please help me with this?
Thank you in advance.
So basically you can do something as mentioned below:
<?php
$listing_status = '';
if(listingpress_listing_status() == 'For sale') {
$listing_status = 100;
} elseif(listingpress_listing_status() == 'For rent') {
$listing_status = 110;
} elseif(listingpress_listing_status() == 'For rent') {
$listing_status = 120;
} else {
$listing_status = listingpress_listing_status();
}
?>
<category><![CDATA[<?php echo $listing_status; ?>]]></category>
A word of advice, share the problem directly with code from the beginning so that you can get help quicker.

Order properties should not be accessed directly - WooCommerce 3.0

I've just upgraded my local WooCommerce website to 3.0. Everything works perfectly as normal, but I've noticed with debugging turned on that I'm getting hundreds of the following notices:
[05-Apr-2017 12:25:00 UTC] PHP Notice: id was called <strong>incorrectly</strong>. Order properties should not be accessed directly. Please see Debugging in WordPress for more information. (This message was added in version 3.0.) in C:\xampp\htdocs\dev\wp-includes\functions.php on line 4137
So it looks like WooCommerce are pulling back being able to directly call order data. One example this code is being triggered by is this function in my functions.php file:
function eden_woocommerce_order_number($original, $order)
{
return 'EDN-' . str_pad($order->id, 10, 0, STR_PAD_LEFT);
}
This function simply adds "EDN" to the start of the order ID and pads it by 10 characters, but WooCommerce doesn't like how I'm calling $order - what would be the best way to rewrite such a function that 3.0 is happy with?
it says "id was called incorrectly. Order properties should not be accessed directly."
Try $order->get_id()
Maybe its helpful for others too. Here's the some stuff regarding to all the functions of directly accessed values through the magic function.
This function is from Woocommerce 3.0
if ( 'completed_date' === $key ) {
return $this->get_date_completed() ? gmdate( 'Y-m-d H:i:s', $this->get_date_completed()->getOffsetTimestamp() ) : '';
} elseif ( 'paid_date' === $key ) {
return $this->get_date_paid() ? gmdate( 'Y-m-d H:i:s', $this->get_date_paid()->getOffsetTimestamp() ) : '';
} elseif ( 'modified_date' === $key ) {
return $this->get_date_modified() ? gmdate( 'Y-m-d H:i:s', $this->get_date_modified()->getOffsetTimestamp() ) : '';
} elseif ( 'order_date' === $key ) {
return $this->get_date_created() ? gmdate( 'Y-m-d H:i:s', $this->get_date_created()->getOffsetTimestamp() ) : '';
} elseif ( 'id' === $key ) {
return $this->get_id();
} elseif ( 'post' === $key ) {
return get_post( $this->get_id() );
} elseif ( 'status' === $key ) {
return $this->get_status();
} elseif ( 'post_status' === $key ) {
return get_post_status( $this->get_id() );
} elseif ( 'customer_message' === $key || 'customer_note' === $key ) {
return $this->get_customer_note();
} elseif ( in_array( $key, array( 'user_id', 'customer_user' ) ) ) {
return $this->get_customer_id();
} elseif ( 'tax_display_cart' === $key ) {
return get_option( 'woocommerce_tax_display_cart' );
} elseif ( 'display_totals_ex_tax' === $key ) {
return 'excl' === get_option( 'woocommerce_tax_display_cart' );
} elseif ( 'display_cart_ex_tax' === $key ) {
return 'excl' === get_option( 'woocommerce_tax_display_cart' );
} elseif ( 'cart_discount' === $key ) {
return $this->get_total_discount();
} elseif ( 'cart_discount_tax' === $key ) {
return $this->get_discount_tax();
} elseif ( 'order_tax' === $key ) {
return $this->get_cart_tax();
} elseif ( 'order_shipping_tax' === $key ) {
return $this->get_shipping_tax();
} elseif ( 'order_shipping' === $key ) {
return $this->get_shipping_total();
} elseif ( 'order_total' === $key ) {
return $this->get_total();
} elseif ( 'order_type' === $key ) {
return $this->get_type();
} elseif ( 'order_currency' === $key ) {
return $this->get_currency();
} elseif ( 'order_version' === $key ) {
return $this->get_version();
} elseif ( is_callable( array( $this, "get_{$key}" ) ) ) {
return $this->{"get_{$key}"}();
} else {
return get_post_meta( $this->get_id(), '_' . $key, true );
}
You should call the woo get function. add get_ ()
For example, change:
$order->status to $order->get_status()

Very basic PHP - IF AND IF...?

My current code is as follows:
if ( ( $status == 'active' ) ||
( $status == 'full' ) ) {
I need to also include an AND statement. So if $status is either full or active AND $position matches 'need photo' or 'completed' then it displays. How do I include an AND statement?
I tried the following but it didn't seem to work:
if ( ( $status == 'active' ) ||
( $status == 'full' ) &&
( $position == 'need photo' ) ||
( ( $position == 'completed' ) ) {
Any help? Thank you! :-) I'm fairly new to all of this. I tried Google but couldn't find a clear answer.
&& has higher precedence than || so the code you tried is the same as:
if ($status == 'active' || ($status == 'full' && $position == 'need photo') || $position == 'completed') {
Which in plain English means, if either status is active, or both status is full and position is need photo, or position is completed.
But you want:
if (($status == 'active' || $status == 'full') && ($position == 'need photo' || $position == 'completed')) {
Which means, if either status is active or status is full, and either position is need photo or position is completed.
According to the PHP documentation on operator precedence, AND takes precedence over OR, so you need to group the OR expressions with parentheses:
if ( ($status == 'active || $status == 'full) && ($position == 'need photo' || $position == 'completed') ) {
...
I think you are just missing some brackets. What you want is if ((A) && (B)), where A and B are complex expressions (an expression containing two sub-expressions).
In your case: A = ( $status == 'active' ) || ( $status == 'full' ) and B = ( $position == 'need photo' ) || ( $position == 'completed' )
So, try this:
if ( **(** ( $status == 'active' ) || ( $status == 'full' ) **)** && **(** ( $position == 'need photo' ) || ( $position == 'completed' ) **)** ) {

Colon operator in PHP

This is part of the wordpress code and I don't understand it:
if ( is_404() && $template = get_404_template() ) :
elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_tax() && $template = get_taxonomy_template() ) :
elseif ( is_front_page() && $template = get_front_page_template() ) :
elseif ( is_home() && $template = get_home_template() ) :
elseif ( is_attachment() && $template = get_attachment_template() ) :
remove_filter('the_content', 'prepend_attachment');
elseif ( is_single() && $template = get_single_template() ) :
elseif ( is_page() && $template = get_page_template() ) :
elseif ( is_category() && $template = get_category_template() ) :
elseif ( is_tag() && $template = get_tag_template() ) :
elseif ( is_author() && $template = get_author_template() ) :
elseif ( is_date() && $template = get_date_template() ) :
elseif ( is_archive() && $template = get_archive_template() ) :
elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
elseif ( is_paged() && $template = get_paged_template() ) :
else :
$template = get_index_template();
endif;
A colon can replace a curly bracket in PHP. So if I substitute the colons, I get this:
if ( is_404() && $template = get_404_template() ) {
elseif ( is_search() && $template = get_search_template() ) {
elseif ( is_tax() && $template = get_taxonomy_template() ) {
...
}
}
}
else
Makes no sense to me, because each elseif is missing its opening if.
Reggie,
colons in if/else statements in PHP : it's NOT about replacing braces
but a PAIR of braces.
Example :
if ($a) : doThis();
elseif ($b) : doThat();
else : doTheOther();
endif;
would become
if ($a) { doThis(); }
elseif ($b) { doThat(); }
else { doTheOther(); }
OR (since it's just one statement and not a block of statements)
if ($a) doThis();
elseif($b) doThat();
else doTheOther();
Reference : Alternative Syntax for Control Structures
As for this specific piece of code :
if ( is_404() && $template = get_404_template() ) :
elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_tax() && $template = get_taxonomy_template() ) :
it translates to
if ( is_404() && $template = get_404_template() )
{ /* DO NOTHING */ }
elseif ( is_search() && $template = get_search_template() )
{ /* DO NOTHING */ }
Hint : The elseif statement does NOT include the other elseif statements. (like elseif ($a) { elseif($b) {} })

Categories