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';
}
Related
I've successfully been able to create one custom metabox for WordPress post types using the following code:
function bandcamp_metabox(){
$posttypes = Array(
'discos',
'Projetos'
);
add_meta_box('bandcamp_link', 'bandcamp link', 'bandcamp_link_meta_callback', $posttypes, 'side');
}
function bandcamp_link_meta_callback( $post ) {
wp_nonce_field('save_bandcamp_link', 'bancamp_meta_box_nonce');
$value = get_post_meta($post->ID,'_bandcamp_link_value_key', true);
echo '<label for="bancamp_link_field">';
echo '<input type="url" id="bancamp_link_field" name="bancamp_link_field" value="' . esc_attr ( $value ) . '" size="25" />';
}
function save_bandcamp_link ( $post_id ) {
if( ! isset( $_POST['bancamp_meta_box_nonce'] ) ){
return;
}
if ( ! wp_verify_nonce( $_POST['bancamp_meta_box_nonce'], 'save_bandcamp_link' ) ){
return;
}
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return;
}
if( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
if( ! isset( $_POST['bancamp_link_field'] ) ) {
return;
}
$bandcamp_link = sanitize_text_field( $_POST['bancamp_link_field'] );
update_post_meta($post_id, '_bandcamp_link_value_key', $bandcamp_link);
}
add_action('add_meta_boxes', 'bandcamp_metabox');
add_action('save_post', 'save_bandcamp_link');
I was hoping (still kind of a noob) that through a 'foreach' function I'd be able to re-use the code to create multiple metaboxes at once like this:
$varsocial = Array(
'bandcamp',
'soundcloud',
'facebook',
'instagram',);
foreach($varsocial as $sociallink) {
function social_metabox(){
$posttypes = Array(
'discos',
'Projetos');
add_meta_box(''. $sociallink .'_link', ''. $sociallink .' link', 'social_link_meta_callback', $posttypes, 'side');
}
function social_link_meta_callback( $post ) {
wp_nonce_field('save_social_link', 'social_meta_box_nonce');
$value = get_post_meta($post->ID,'_'. $sociallink .'_link_value_key', true);
echo '<label for="'. $sociallink .'_link_field">';
echo '<input type="url" id="'. $sociallink .'_link_field" name="'. $sociallink .'_link_field" value="' . esc_attr ( $value ) . '" size="25" />';
}
function save_social_link ( $post_id ) {
if( ! isset( $_POST['social_meta_box_nonce'] ) ){
return;
}
if ( ! wp_verify_nonce( $_POST['social_meta_box_nonce'], 'save_social_link' ) ){
return;
}
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return;
}
if( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
if( ! isset( $_POST[''. $sociallink .'_link_field'] ) ) {
return;
}
$social_link = sanitize_text_field( $_POST[''. $sociallink .'_link_field'] );
update_post_meta($post_id, '_'. $sociallink .'_link_value_key', $social_link);}
return add_action('add_meta_boxes', 'social_metabox');
return add_action('save_post', 'save_social_link');
}
But such is not working. What am I doing wrong?
I´m working with this code but it's not working.
How can i fix it to redirect to 3 different pages for each category product (I need to set it for 3 different categories).
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase($order_product_categories) {
$product_categories1 = jobs;
$product_categories2 = courses;
$product_categories3 = exchange-post;
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
$cat_in_cart = false;
$order_id = isset( $wp->query_vars['order-received'] ) ? intval( $wp->query_vars['order-received'] ) : 0;
$order = new WC_Order( $order_id );
$product_categories = array('jobs', 'courses', 'exchange-post');
foreach( $order->get_items() as $item ){
if( has_term( $product_categories, 'product_cat', $item->get_product_id() ) ) {
$cat_in_cart = true;
break;
}
}
if ( $cat1_in_cart ) {
wp_redirect( 'https://steamroute.com/post-a-job-check-out/');
}
elseif ( $cat2_in_cart ) {
wp_redirect( 'https://steamroute.com/courses-checkout/');
}
elseif ( $cat3_in_cart ) {
wp_redirect( 'https://steamroute.com/exchange-checkout/');
}
exit;
}
}
I think you mean this, don't forget that this code is based on 1 category per product, otherwise you will have to adjust it further
p.s. maybe you should consider this method? instead of a redirect https://markjaquith.wordpress.com/2014/02/19/template_redirect-is-not-for-loading-templates/
function wc_custom_redirect_after_purchase() {
$product_categories1 = 'jobs';
$product_categories2 = 'courses';
$product_categories3 = 'exchange-post';
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
$cat_in_cart = false;
$order_id = isset( $wp->query_vars['order-received'] ) ? intval( $wp->query_vars['order-received'] ) : 0;
$order = new WC_Order( $order_id );
$product_categories = array( $product_categories1, $product_categories2, $product_categories3 );
foreach( $order->get_items() as $item ) {
$item_id = $item->get_product_id();
if( has_term( $product_categories, 'product_cat', $item_id ) ) {
$cat_in_cart = true;
$terms = get_the_terms( $item_id, 'product_cat' );
foreach ( $terms as $term ) {
// Categories by slug
$cat_name = $term->slug;
}
break;
}
}
if ( $cat_in_cart ) {
switch ($cat_name) {
case $product_categories1:
wp_redirect( 'https://steamroute.com/post-a-job-check-out/');
break;
case $product_categories2:
wp_redirect( 'https://steamroute.com/courses-checkout/');
break;
case $product_categories3:
wp_redirect( 'https://steamroute.com/exchange-checkout/');
break;
}
}
}
}
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
I am using javo directory theme and I cannot seem to figure this out. On the map page where there are a list of listings (after user does a search by category). However, only the first category is being displayed and I need all the categories of each listing to be displayed. Displaying only 1 category defeats the purpose of being able to search by category. Here is the code, let me know if you need other code to better understand what I am trying to do but I know the change has to be within this area. I have looked everywhere for a solution which is why I need some help Thanks a lot!
public function get_meta( $key, $default_value=false ) {
$strOutput = get_post_meta( $this->post_id, $key, true );
return empty( $strOutput ) ? $default_value : $strOutput;
}
public function m( $key, $value=false ){
return $this->get_meta( $key, $value );
}
public function get_term( $taxonomy=false, $sep=', ' )
{
$output_terms = Array();
if( $terms = wp_get_object_terms( $this->post_id, $taxonomy, Array( 'fields' => 'names' ) ) )
{
$output_terms = is_array( $terms ) ? join( $sep, $terms ) : null;
$output_terms = trim( $output_terms );
// $output_terms = substr( $output_terms, 0, -1 );
}else{
$output_terms = '';
}
return $output_terms;
}
public function c( $taxonomy=false, $default='', $single=true, $sep=', ' )
{
$strTerms = $this->get_term( $taxonomy, $sep );
if( $single && !empty( $strTerms ) ) {
$strTerms = #explode( $sep, $strTerms );
$strTerms = isset( $strTerms[0] ) ? $strTerms[0] : '' ;
}
return empty( $strTerms ) ? $default : $strTerms;
}
public function category() {
return $this->c(
apply_filters( 'jvfrm_spot_' . get_class( $this ) . '_featured_tax', 'category', $this->post_id ),
apply_filters( 'jvfrm_spot_' . get_class( $this ) . '_featured_no_tax', __( "No Category", 'javo' ), $this->post_id )
);
}
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;
?>
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++;
}
}
}