• Resolved Muhammad Muhaimin

    (@warnetsabily)


    I got this error in alert box when I take a course:

    <b>Fatal error</b>: Cannot use object of type LPR_Cart as array in <b>/home3/widyagm/public_html/elektro/wp-content/plugins/learnpress/inc/cart/class.lpr-cart.php</b> on line <b>64</b>

    Maybe the problem is with the PHP version, because my current website is hosted on PHP 5.2.17 (64bit).

    https://www.ads-software.com/plugins/learnpress/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Muhammad Muhaimin

    (@warnetsabily)

    I don’t know whether this is a good idea or not.
    I tried to modify /inc/cart/class.lpr-cart.php based on this solution:

    https://stackoverflow.com/questions/18535885/why-does-my-php-array-give-me-cannot-use-object-of-type-stdclass-as-array

    I converted all the $_SESSION assignments into object hierarchy.

    <?php
    
    /**
     * Class LPR_Cart
     *
     * Simple Cart object for now. Maybe need to expand later
     */
    class LPR_Cart{
        private static $instance = false;
        function __construct(){
            if( self::$instance ) return;
    
            if( !session_id() ) session_start();
            if( empty( $_SESSION->learn_press_cart ) ){
                $_SESSION->learn_press_cart = array(
                    'cart_id'   => $this->generate_cart_id(),
                    'products'  => array()
                );
            }
        }
    
        function get_cart_id(){
            return $_SESSION->learn_press_cart->cart_id;
        }
    
        function get_products(){
            return $_SESSION->learn_press_cart->products;
        }
    
        function get_sub_total(){
            $sub_total = 0;
            $products = $this->get_products();
            if( $products ) foreach( $products as $product ){
                $sub_total += learn_press_is_free_course( $product['id'] ) ? 0 : floatval( learn_press_get_course_price( $product['id'] ) );
            }
            learn_press_format_price( $sub_total );
            return apply_filters( 'learn_press_get_cart_subtotal', $sub_total, $this->get_cart_id() );
        }
    
        function get_total(){
            $sub_total  = $this->get_sub_total();
            $total      = $sub_total;
            return apply_filters( 'learn_press_get_cart_total', $total, $this->get_cart_id() );
        }
    
        function generate_cart_id(){
            return md5( time() );
        }
    
        function add_to_cart( $course_id ){
    
            $course = get_post( $course_id );
            $price  = learn_press_get_course_price( $course_id );
            $quantity = 1;
    
            $_SESSION->learn_press_cart->products->$course_id = array(
                'id'        => $course_id,
                'quantity'  => $quantity,
                'price'     => $price
            );
        }
    
        function empty_cart(){
            unset( $_SESSION->learn_press_cart->products );
            return $this;
        }
    
        function destroy(){
            unset( $_SESSION->learn_press_cart );
        }
        static function instance( $prop = false, $args = false ){
            if( !self::$instance ){
                self::$instance = new self();
            }
            $ins = self::$instance;
            if( $prop ) {
                $prop = 'get_' . $prop;
            }
            return $prop && is_callable( array( $ins, $prop ) ) ? call_user_func_array( array( $ins, $prop ), (array)$args ) : $ins;
        }
    }
    if( !is_admin() ) {
        $GLOBALS['learn_press_cart'] = LPR_Cart::instance();
    }
    
    function learn_press_get_cart( $prop = null ){
        return LPR_Cart::instance( $prop );
    }
    
    function learn_press_get_cart_description(){
        $products = learn_press_get_cart( 'products');
        $description = '';
        if( $products ){
            foreach( $products as $prop ){
                $description .= get_the_title( $prop['id'] );
            }
        }
        return apply_filters( 'learn_press_cart_description', $description );
    }
    
    function learn_press_get_cart_course_url(){
        $products = learn_press_get_cart( 'products');
        $return = '';
        if( $products ){
            foreach( $products as $prop ){
                $return = get_permalink( $prop['id'] ); break;
            }
        }
        return apply_filters( 'learn_press_cart_course_url', $return );
    }
    
    function learn_press_get_cart_total(){
        return learn_press_get_cart( 'total');
    }
    //learn_press_get_cart_description();

    But I got this warning when I load a course page:

    Warning: Attempt to assign property of non-object in /home3/widyagm/public_html/elektro/wp-content/plugins/learnpress/inc/cart/class.lpr-cart.php on line 15

    The page was successfully loaded, then I tried take the course again, and I got this message in the page:

    Congratulation ! You have finished this course

    But I still haven’t enrolled to the course.
    It’s not that easy ??

    Plugin Contributor Ken Nguyen

    (@kendy73)

    Hi Muhammad,

    Sorry for my late response because of last weekend. I will discuss with dev team then find solution.

    I will get back to you after it done.

    Regards,

    Ken

    Plugin Contributor Ken Nguyen

    (@kendy73)

    Hi Muhammad,

    The bug will be fixed in next update. Thanks for your feedback.

    Regards,

    Ken

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Cannot use object of type LPR_Cart as array’ is closed to new replies.