Order properties should not be accessed directly - WooCommerce 3.0 - php

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

Related

DOMNode::isEqualNode(): Not yet implemented

I made this function in php to check if a parent XML has already got a xml to insert it or not :
function has_xml_node( \DOMElement $needle, \DOMElement $node ) {
if( $node->hasChildNodes() ){
foreach ( $node->childNodes as $childNode ) {
if( $needle->isEqualNode( $childNode ) ){
return true;
}
}
}
return false;
}
I got this php error :
DOMNode::isEqualNode(): Not yet implemented in ...
How can I fix this ?
I found the solution here.
Use $needle->isSameNode( $childNode );
BUT it seems to check 'baseURI' as well so if you have two same tags but from different xml files, $needle->isSameNode( $childNode ); return false.
I made a function to work around this problem.
function is_same_node( \DOMNode $node_to_compare, \DOMNode $node_refered, $with_attributes = true ) {
if( $node_to_compare->tagName !== $node_refered->tagName )
return false;
if( $node_to_compare->nodeValue !== $node_refered->nodeValue )
return false;
if( $node_to_compare->prefix !== $node_refered->prefix )
return false;
if( $with_attributes ){
if( !$node_to_compare->hasAttributes() && $node_refered->hasAttributes() ){
return false;
}else{
if( !$node_refered->hasAttributes() && $node_to_compare->hasAttributes() ){
return false;
}else{
foreach ( $node_to_compare->attributes as $attribute ){
$find_attribute_with_same_value = false;
foreach ( $node_refered->attributes as $attribute_refered ){
if(
$attribute_refered->name === $attribute->name
&& ( $attribute_refered->value === $attribute->value )
){
$find_attribute_with_same_value = true;
}
}
if( !$find_attribute_with_same_value )
return false;
}
}
}
}
return true;
}

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.

mysqli_get_server_info(): invalid object or resource mysqli

I'm getting mysqli_get_server_info(): invalid object or resource mysqli error, when running wpdb outside of wordpress in a standalone 'plugin'.
Here's the code that returns the error:
private $use_mysqli = false;
if ( function_exists( 'mysqli_connect' ) ) {
if ( defined( 'WP_USE_EXT_MYSQL' ) ) {
$this->use_mysqli = ! WP_USE_EXT_MYSQL;
} elseif ( version_compare( phpversion(), '5.5', '>=' ) ||
! function_exists( 'mysql_connect' ) ) {
$this->use_mysqli = true;
} elseif ( false !== strpos( $GLOBALS['wp_version'], '-' ) ) {
$this->use_mysqli = true;
}
}
public function db_version() {
if ( $this->use_mysqli ) {
$server_info = mysqli_get_server_info( $this->dbh );
} else {
$server_info = mysql_get_server_info( $this->dbh );
}
return preg_replace( '/[^0-9.].*/', '', $server_info );
}
Why am I getting this error? How should I fix it?

Redux WP Framework. Cannot disable 'dev_mode' with if statment

I have a problem.
I want to disable 'dev_mode' if 'opt_name' not redux_demo, using the if statment, but it did not work ... where does the fault ??
I found the code on Here
and I turn it into like this
if ( ! function_exists( 'redux_disable_dev_mode_plugin' ) ) {
function redux_disable_dev_mode_plugin( $redux ) {
if ( $redux->args['opt_name'] != 'redux_demo' ) {
if ( $redux->args['dev_mode'] == true ) {
$redux->args['dev_mode'] = false;
}
} else {
if ( $redux->args['dev_mode'] == false ) {
$redux->args['dev_mode'] = true;
}
}
}
add_action( 'redux/construct', 'redux_disable_dev_mode_plugin' );
}
the above code, I input in config.php
Thanks B4 and sorry my english is not good. :D
You can also filter the args JUST for your opt_name using this:
apply_filters( "redux/args/{$this->args['opt_name']}", $this->args );
if ( ! function_exists( 'redux_disable_dev_mode_plugin' ) ) {
function redux_disable_dev_mode_plugin( $redux ) {
if ( $redux->args['opt_name'] != 'redux_demo' ) {
$redux->args['dev_mode'] = false;
}
}
add_action( 'redux/construct', 'redux_disable_dev_mode_plugin' );
}
In framework.php (for me it's from line 1281). It worked for me after making those two attr false.
// Force dev_mode on WP_DEBUG = true and if it's a local server
if ( Redux_Helpers::isLocalHost() || ( Redux_Helpers::isWpDebug() ) ) {
if ( $this->args['dev_mode'] != true ) {
$this->args['update_notice'] = false;
}
$this->dev_mode_forced = true; // make it false
$this->args['dev_mode'] = true; //make it false
}

CakePHP not completely decrypting virtual field

I'm running into an issue in my CakePHP (2.4.1) app where the virtual field in one of my models is not properly being decrypted using Cake's Security::rijndael(). In my User model, the display field is defined as
public $virtualFields = array( 'name' => 'CONCAT(User.first_name, " ", User.last_name)' );
Both User.first_name and User.last_name are encrypted, and User.name is the displayField for the model. In the AssignedShift related model (User hasMany AssignedShift), the User.name field is not being properly decrypted. Here is an example return:
Array(
[1] => Test|�d�F�3��������������S0�Dy=>�dJ���sP�4�F�n�؄s-#���7P���n�ʙ.�C�#���˷C�z��(;Eu)�
)
This is the only model that this happens on, so I'm pretty confident my afterFind method is working properly. In any case, below are the functions for encrypt/decrypt:
/*AppModel.php*/
function _afterFind($results, $primary) {
if( $primary ) {
foreach( $results as $key => $val) {
if( isset( $val[$this->alias] ) ) {
$results[$key][$this->alias] = $this->doAfterFind( $results[$key][$this->alias] );
}
}
} else {
if( isset( $results['id']) ) {
$results = $this->doAfterFind($results);
} else {
foreach( $results as $key => $val ) {
if( isset( $val[$this->alias] ) ) {
if( isset( $val[$this->alias]['id'] ) ) {
$results[$key][$this->alias] = $this->doAfterFind( $results[$key][$this->alias] );
} else {
foreach( $results[$key][$this->alias] as $key2 => $val2 ) {
$results[$key][$this->alias][$key2] = $this->doAfterFind( $results[$key][$this->alias][$key2] );
}
}
}
}
}
}
return $results;
}
public function doAfterFind($data) {
foreach( $data as $key => $val) {
if( !empty( $val ) && strlen( $val ) >= 88) {
$data[$key] = Security::rijndael( base64_decode( $val ), Configure::read( 'Security.cipherSeed' ), 'decrypt' );
}
}
//My attempt at hacking it back together
if(array_key_exists('first_name', $data)) {
$data['name'] = $data['first_name'] ." ". $data['last_name'];
}
return $data;
}
/*AssignedShift.php*/
public function afterFind($results, $primary = false) {
return $this->_afterFind($results, $primary);
}
Both models were created from the console - User has been modified but I haven't touched the relationships, and AssignedShift is completely untouched. Any ideas? Thanks in advance.

Categories