Legen Sie mithilfe einer Methode in Woocommerce programmgesteuert ein benutzerdefiniertes Produktfeld fest

Lesezeit: 3 Minuten

Pickerolls Benutzeravatar
Pickeroll

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


LoicTheAztecs Benutzeravatar
LoicTheAztec

Warum Sie nicht einfach verwenden WC_Data update_meta_data() Und get_meta() Methoden wie folgt:

$product->set_regular_price($_POST['price']); 

$product->update_meta_data("tipologiaAppunto", "Hey"); // <== Set (or update) the custom field value from a meta key
$product->save(); 

echo $product->get_meta("tipologiaAppunto"); // <== Get (or read) the custom field value from a meta key

Auch seit Woocommerce 3 können Sie ersetzen woocommerce_process_product_meta Aktionshaken, von woocommerce_admin_process_product_object Ihr Code lautet also stattdessen:

// Display admin product custom setting field(s)
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
function woocommerce_product_custom_fields() {
    global $product_object;

    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' // <== Not needed as you don't use a description
    ) );

    echo '</div>';
}

// Save admin product custom setting field(s) values
add_action('woocommerce_admin_process_product_object', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields_save( $product ) {
    if ( isset($_POST['tipologiaAppunto']) ) {
        $product->update_meta_data( 'tipologiaAppunto', sanitize_text_field( $_POST['tipologiaAppunto'] ) );
    }
}

1450920cookie-checkLegen Sie mithilfe einer Methode in Woocommerce programmgesteuert ein benutzerdefiniertes Produktfeld fest

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy