syntax error, unexpected '&&' (T_BOOLEAN_AND) - php

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.

Related

Facing PHP 8 Warning: Attempt to read property "query" on null

Getting a PHP warning for this section of code, whilst running PHP 8:
function wpsc_body_class( $classes ) {
global $wp_query, $wpsc_query;
$post_id = get_the_ID();
if ( $post_id ) {
$page_url = get_permalink( $post_id );
// If on a product or category page...
if ( get_option( 'product_list_url' ) == $page_url || get_post_type( $post_id ) === 'wpsc-product' ) {
$classes[] = 'wp-e-commerce';
if ( ! is_array( $wpsc_query->query ) ) {
$classes[] = 'wpsc-home';
}
if ( wpsc_is_single_product() ) {
$object = $wp_query->get_queried_object();
$classes[] = 'wpsc-single-product';
if ( absint( $object->ID ) > 0 ) {
$classes[] = 'wpsc-single-product-' . absint( $object->ID );
}
}
if ( wpsc_is_in_category() && ! wpsc_is_single_product() ) {
$classes[] = 'wpsc-category';
$tax_object = $wp_query->get_queried_object();
$classes[] = 'wpsc-category-' . esc_attr( $tax_object->slug );
}
if ( wpsc_is_in_tag() && ! wpsc_is_single_product() ) {
$classes[] = 'wpsc-tag';
$tax_object = $wp_query->get_queried_object();
$classes[] = 'wpsc-tag-' . esc_attr( $tax_object->slug );
}
}
$page_url = set_url_scheme( $page_url, 'relative' );
// If viewing the shopping cart...
if ( set_url_scheme( get_option( 'shopping_cart_url' ), 'relative' ) === $page_url ) {
$classes[] = 'wp-e-commerce';
$classes[] = 'wpsc-shopping-cart';
}
// If viewing the transaction...
if ( set_url_scheme( get_option( 'transact_url' ), 'relative' ) === $page_url ) {
$classes[] = 'wp-e-commerce';
$classes[] = 'wpsc-transaction-details';
}
// If viewing your account...
if ( set_url_scheme( get_option( 'user_account_url' ), 'relative' ) === $page_url ) {
$classes[] = 'wp-e-commerce';
$classes[] = 'wpsc-user-account';
}
}
return $classes;
}
Error pointing to this line of code:
if ( ! is_array( $wpsc_query->query ) ) {
Looking for the best way of resolving this, and changing this code, to overcome the php 8 warning for attempt to read property "query" on null.
You should check if the query method exists in the first place and add that check to your if statement like so:
Edit: updated answer to check for null.
$queryMethodExists = method_exists($wpsc_query, 'query');
if (
$wpsc_query === null
|| ($queryMethodExists && ! is_array( $wpsc_query->query ))
) {
$classes[] = 'wpsc-home';
}

Sanitizing the code as per wpcs for yoda condition checks

i am trying to cleanup the file with phpcs --standard=Wordpress ../filename.php . But it seems phpcbf is unable to perform the action. So, can anyone share some help as how can i manually fix these doc comment and yoda errors. Attached is related chunk of code and the snip of the actual php file and the errors encountered. Any help would be highly appreciated.
<?php
function is_blog() {
return ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag() ) && 'post' == get_post_type();
}
?>
<?php do_action( 'generate_before_footer' ); ?>
<div <?php generate_footer_class(); ?>>
<?php
do_action( 'generate_before_footer_content' );
// Get how many widgets to show.
$widgets = generate_get_footer_widgets();
if ( ! empty( $widgets ) && 0 !== $widgets ) :
// Set up the widget width.
$widget_width = '';
if ( $widgets == 1 ) {
$widget_width = '100';
}
if ( $widgets == 2 ) {
$widget_width = '50';
}
if ( $widgets == 3 ) {
$widget_width = '33';
}
if ( $widgets == 4 ) {
$widget_width = '25';
}
if ( $widgets == 5 ) {
$widget_width = '20';
}
?>
Above the is_blog() function, you'll need to add a docblock:
/**
* Function comment here.
*
*/
function is_blog() {
}
Yoda conditions are when you write the value to compare against before the variable you are comparing. So instead of:
if ( $widgets == 1 ) {
$widget_width = '100';
}
It wants you to write:
if ( 1 == $widgets ) {
$widget_width = '100';
}
Changing those things should get the file passing the checks.

Getting this error "Trying to get property of non-object" for a function

This is a function to removed some banners, I know the error is happening around the if ( ! in_array( $current_screen->post_type, $post_types ) ) but I don't know to properly fix it.
function lsx_tec_disable_lsx_banner( $disabled ) {
global $current_screen;
$post_types = apply_filters( 'tribe_is_post_type_screen_post_types', Tribe__Main::get_post_types() );
if ( ! in_array( $current_screen->post_type, $post_types ) ) {
$disabled = true;
}
if ( is_null( $id ) && false !== strpos( $current_screen->id, 'tribe' ) )
{
$disabled = true;
}
if ( is_single() && tribe_is_event() ) {
$disabled = true;
}
return $disabled;
}
please check below link and change your code.
https://codex.wordpress.org/Function_Reference/get_current_screen
<?php
$current_screen = get_current_screen(); // use this instead of global $current_screen;
?>

woocommerce override of product

I have a problem with the following code when I make the price variable the function doesn't work but when I hardcode a price it works. (note I am in a testing face and haven't worried about security)
this, down't work:
function add_custom_price( $cart_obj ) {
global $product, $woocommerce ,$wpdb;
if ( isset( $_POST['fra'] ) ){
$GLOBALS['$_fra'] = urldecode( $_POST["fra"] );
} else {
$GLOBALS['$_fra'] = "";
}
if ( isset( $_POST['til'] ) ){
$GLOBALS['$_til'] = urldecode($_POST["til"]);
} else {
$GLOBALS['$_til'] = "";
}
if ( $GLOBALS['$_til'] !="" && $GLOBALS['$_fra'] !="" ){
$t1 = urldecode( $GLOBALS['$_til'] );
$t2 = urldecode( $GLOBALS['$_fra']);
$objArray = $wpdb->get_results("SELECT $t1 FROM test_priser WHERE city = '$t2'");
if ( isset($objArray[0]->$t1) ){
$priser = explode("/",$objArray[0]->$t1);
if ( !isset($priser[1]) ){
$priser[1] = intval($priser[0]);
}
}
echo "priser[1] = ".$priser[1]; //this outputs the expected value
}
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
return;
}
foreach ( $cart_obj->get_cart() as $key => $value ) {
$value['data']->set_price( $priser[1] );
}
}
When I add this line, and override the variable, it works:
$priser[1] = 1000;
I am quite confused as to what the problem might be.
for anyone else who might stumble upon this. Seems I need to pass the variable price in the session then it works.
you can find the answer here: custom price on woocommerce product

How to solve the key variable on $unset key

I am running a PHP script, and keep getting errors like:
Notice: Undefined variable: key in D:\0-MYBLOG\SERVER-MYBLOG\InstantWP_4.3.1\iwpserver\htdocs\wordpress\wp-content\plugins\sama-author-review\admin\metabox.php on line 310
Line 310 looks like this:
unset( $new['items_review'][$key] );
Here the code:
if ( $new['items_review'] && is_array( $new['items_review'] )) {
foreach ( $new['items_review'] as $review ) {
if ( empty( $review['label'] ) ) {
unset( $new['items_review'][$key] );
} else {
$review['value'] = absint( $review['value'] );
$review['label'] = esc_attr( $review['label'] );
if ( empty( $review['slug'] )) {
$review['slug'] = sanitize_title( $review['label'] );
} else {
$review['slug'] = sanitize_title( $review['slug'] );
}
$review['style'] = esc_attr( $review['style'] );
$items_review[$i] = $review;
$i++;
}
}
}
Is there a quick fix to resolve these error?
Really appreciate for any help
Thank you
I think I get what the problem is, you have forgotten to put the key parameter on your foreach statement.
I admit its a bit of a guess as I have no idea what is in $new but it seams a logical possibility.
if ( $new['items_review'] && is_array( $new['items_review'] )) {
// foreach ( $new['items_review'] as $review ) {
// replace foreach with this line
foreach ( $new['items_review'] as $key => $review ) {
if ( empty( $review['label'] ) ) {
unset( $new['items_review'][$key] );
} else {
$review['value'] = absint( $review['value'] );
$review['label'] = esc_attr( $review['label'] );
if ( empty( $review['slug'] )) {
$review['slug'] = sanitize_title( $review['label'] );
} else {
$review['slug'] = sanitize_title( $review['slug'] );
}
$review['style'] = esc_attr( $review['style'] );
$items_review[$i] = $review;
$i++;
}
}
}

Categories