Ich versuche, im allgemeinen Bereich eines Produkts ein benutzerdefiniertes Feld hinzuzufügen und es mit einem Frontend-Formular zu initialisieren.
Ich hatte kein Problem damit, es hinzuzufügen, aber ich weiß nicht, wie ich es einstellen soll.
SO HINZUFÜGEN UND SPEICHERN SIE DAS BENUTZERDEFINIERTE FELD:
// The code for displaying WooCommerce Product Custom Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Following code Saves WooCommerce Product Custom Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class=" product_custom_field ">';
//Custom Product Text Field
woocommerce_wp_text_input(
array(
'id' => 'tipologiaAppunto',
'label' => __('Tipologia appunto:', 'woocommerce'),
'placeholder' => '',
'desc_tip' => 'true'
)
);
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['tipologiaAppunto'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, 'tipologiaAppunto', esc_attr($woocommerce_custom_product_text_field));
}
Um es einzustellen, habe ich versucht, das zu erweitern WC_PRODUCT CLASS
ERWEITERUNG DER WC_PRODUCT-KLASSE:
add_action('init', 'register_myclass');
function register_myclass()
{
class WC_ProductExtended extends WC_Product
{
function __construct() {
parent::__construct();
if (!array_key_exists("tipologiaAppunto", $this->data)) {
$this->data["tipologiaAppunto"] = "";
}
}
/**
* Set product tipologiaAppunto.
*
* @since 3.0.0
* @param string $tipologiaAppunto Product tipologiaAppunto.
*/
public function set_tipologiaAppunto($tipologiaAppunto)
{
$this->set_prop( 'tipologiaAppunto', $tipologiaAppunto );
}
}
}
Und jetzt hatte ich gehofft, durch den Aufruf des Setters das benutzerdefinierte Produktfeld zu initialisieren, aber es passierte nichts.
$product->set_regular_price($_POST['price']);
$productExtendend = new WC_ProductExtended();
$productExtendend->set_tipologiaAppunto("Hey");
$productExtendend->save();
Ich weiß nicht, ob das der richtige Weg ist. Ich habe viele Tutorials zum Hinzufügen des benutzerdefinierten Felds gefunden, aber so wenig darüber, wie man es vom Frontend aus initialisiert. Ich habe versucht, das Verhalten der vorhandenen Felder nachzuahmen aber ohne Glück.
Da dies nur für den Administrator-Support gedacht ist, füge ich ein Ticket hinzu, das auch das Speichern von Kundendaten unterstützt: stackoverflow.com/questions/56497597/…
– Angst
27. April um 20:37 Uhr