-1

I am very new to OOP but I think I have the basics down and I am trying to learn by doing. So I decided to convert my simple wordpress plugin from procedural code to a class method but I keep getting fatal errors.

here is my procedural code that works:

<?php
if ( ! defined( 'ABSPATH' ) ) { 
    exit; // Exit if accessed directly
}

//Check if WooCommerce is active
if ( is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) {

//Load plugin styles
function woa_wqfsp_stylesheet() 
{
    wp_enqueue_style( 'wqfspCSS', plugin_dir_path( 'css/style.css', __FILE__ ) );
}
add_action('wp_enqueue_scripts', 'woa_wqfsp_stylesheet');

//add quantity fields
add_filter( 'woocommerce_loop_add_to_cart_link', 'woa_add_quantity_fields', 10, 2 );
function woa_add_quantity_fields( $html, $product ) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        $html .= woocommerce_quantity_input( array(), $product, false );
        $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    return $html;
}

}//main end
?>

This is my OOP code but I keep getting errors:

<?php

if ( ! defined( 'ABSPATH' ) ) { 
    exit; // Exit if accessed directly
}

function woa_wqfsp() {
    return woa_wqfsp();
} // End woa_wqfsp()

woa_wqfsp();

class woa_wqfsp {

    private static $_instance = null;
    private $html;
    private $product;

    public function __construct() {

        add_action( 'init', array( $this, 'setup' ) );
    }

    public static function instance() {
        if ( is_null( self::$_instance ) )
            self::$_instance = new self();
        return self::$_instance;
    }

//Load plugin styles
function woa_wqfsp_stylesheet() 
{
    wp_enqueue_style( 'wqfspCSS', plugin_dir_path( 'css/style.css', __FILE__ ) );
}



function woa_add_quantity_fields( $html, $product ) {
    if ( $this->$product && $this->$product->is_type( 'simple' ) && $this->$product->is_purchasable() && $this->$product->is_in_stock() && ! $this->$product->is_sold_individually() ) {
        $html = '<form action="' . esc_url( $this->$product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        $html .= woocommerce_quantity_input( array(), $this->$product, false );
        $html .= '<button type="submit" class="button alt">' . esc_html( $this->$product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    return $html;
}

function woa_wqfsp_run() {
//Check if WooCommerce is active
if ( is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) {
//add style for quantity field
add_action('wp_enqueue_scripts', $this->woa_wqfsp_stylesheet);
//add quantity fields
add_filter( 'woocommerce_loop_add_to_cart_link', $this->woa_add_quantity_fields, 10, 2 );

}
}

}//class end
?>

could someone please point out what Im doing wrong? I think it is around this section: function woa_add_quantity_fields( $html, $product )

$html is just a null var and product is a global var by woocommerce

THanks in advance

  • 5
    You need to say what the errors are. – Carcigenicate Oct 25 '16 at 00:57
  • is `add_action` in scope? –  Oct 25 '16 at 01:02
  • One thing I like to do is launch my WooCommerce extension on the `woocommerce_loaded` hook. That way you should be pretty well assured that all WooCommerce files/functions are loaded and available. But we really can't help until you tell us what the problem is. – helgatheviking Oct 25 '16 at 01:09
  • Define OOP first. There is no point in rewriting a procedural code to a procedural code with the `class` keyword. – Michas Oct 25 '16 at 01:13

1 Answers1

1

Firstly:
Follow some naming conventions, begin class names with an uppercase letter.

Secondly: The part

function woa_wqfsp() {
    return woa_wqfsp();
} // End woa_wqfsp()

absolutely does not make sense, probably you should return the class object.

Thirdly (where you probably got an error): The line

add_action( 'init', array( $this, 'setup' ) ); 

calls setup() method on init, which your class is lacking.

So if we sum up, you will get this:

function woa_wqfsp() {
    return new Woa_Wqfsp();
} // End woa_wqfsp()

woa_wqfsp();

class Woa_Wqfsp {

    private $html;
    private $product;

    public function __construct() {

        add_action( 'init', array( $this, 'setup' ) );
    }

    public function setup(){
        // do setup
    }

    //Load plugin styles
    function woa_wqfsp_stylesheet()
    {
        wp_enqueue_style( 'wqfspCSS', plugin_dir_path( 'css/style.css', __FILE__ ) );
    }

    function woa_add_quantity_fields( $html, $product ) {
        if ( $this->$product && $this->$product->is_type( 'simple' ) && $this->$product->is_purchasable() && $this->$product->is_in_stock() && ! $this->$product->is_sold_individually() ) {
            $html = '<form action="' . esc_url( $this->$product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
            $html .= woocommerce_quantity_input( array(), $this->$product, false );
            $html .= '<button type="submit" class="button alt">' . esc_html( $this->$product->add_to_cart_text() ) . '</button>';
            $html .= '</form>';
        }
        return $html;
    }

    function woa_wqfsp_run() {
        //Check if WooCommerce is active
            if ( is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) {
            //add style for quantity field
            add_action('wp_enqueue_scripts', $this->woa_wqfsp_stylesheet);
            //add quantity fields
            add_filter( 'woocommerce_loop_add_to_cart_link', $this->woa_add_quantity_fields, 10, 2 );

        }
    }

}//class end

Also, taking into account your question, you still have a space to improve your OOP skills. Take a look at resources enlisted in this SO answer.

Community
  • 1
  • 1
Avag Sargsyan
  • 2,437
  • 3
  • 28
  • 41