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