2016-10-25 9 views
-1

私はOOPの新機能ですが、私は基本がダウンしていると思っています。だから私は単純なワードプレスのプラグインを手続き型コードからクラス型メソッドに変換することにしましたが、私は致命的なエラーを受けています。ここ手続き型PHPをOOPに変換する

は動作します私の手続き型のコードは次のとおりです。

<?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 
?> 

は、これは私のOOPのコードですが、私はエラーを得続ける:

<?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 
?> 

誰かがImが間違っているものを指摘してくださいだろうか?私はそれがこのセクションの周りだと思う:関数woa_add_quantity_fields($ htmlの、$製品)

$ htmlがちょうどヌルvarと製品であることはwoocommerceによって事前に

おかげまず

+5

あなたはエラーが何であるかを言う必要があります。 – Carcigenicate

+0

はスコープ内で 'add_action'ですか? – nogad

+0

私がしたいことの一つは、 'woocommerce_loaded'フックで私のWooCommerce拡張を起動することです。そうすれば、すべてのWooCommerceファイル/機能がロードされ、利用可能であることを十分に保証する必要があります。しかし、問題が何であるか教えてくれるまで、私たちは本当に助けにならない。 – helgatheviking

答えて

1

グローバルVARです。
naming conventionsに従うと、クラス名は大文字で始まります。

第二: 一部

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

は絶対におそらくあなたがクラスオブジェクトを返す必要があり、意味がありません。 (おそらくエラーを得た)第三

: あなたのクラスが欠けているライン

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

通話initsetup()方法。我々は総括場合

だから、あなたがこれを取得します。また

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 

を、考慮にあなたの質問を取って、あなたはまだOOPのスキルを向上させるためのスペースを持っています。 thisに参加しているリソースを見てください。

+0

まあ、技術的には意味をなさない。ただ役に立たない。 – Carcigenicate

+0

@Carcigenicate、同意します。 –

関連する問題