Ich bin sehr neu zu OOP, aber ich denke, ich habe die Grundlagen und ich versuche zu lernen, indem Sie tun. Also entschied ich mich, mein einfaches Wordpress-Plugin vom prozeduralen Code in eine Klassenmethode zu konvertieren, aber ich erhalte immer wieder fatale Fehler.Konvertieren von prozeduralen PHP zu OOP
hier ist mein prozeduralen Code, das funktioniert:
<?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
?>
Das ist mein OOP-Code ist, aber ich erhalte immer Fehler:
<?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
?>
könnte jemand bitte darauf hinweisen, was Im falsch? Ich glaube, es um diesen Abschnitt ist: function woa_add_quantity_fields ($ html, $ Produkt)
$ html ist nur ein Null-var und Produkt ist ein global var von WooCommerce
Vielen Dank im Voraus
Sie müssen sagen, was die Fehler sind. – Carcigenicate
ist 'add_action' im Bereich? – nogad
Eine Sache, die ich gerne mache, ist meine WooCommerce-Erweiterung auf dem 'woocommerce_loaded'-Hook zu starten. So können Sie sicher sein, dass alle WooCommerce Dateien/Funktionen geladen und verfügbar sind. Aber wir können wirklich nicht helfen, bis Sie uns sagen, was das Problem ist. – helgatheviking