Personentypen programmgesteuert bei der Erstellung buchbarer Produkte in Woocommerce 3 hinzufügen

Lesezeit: 2 Minuten

Benutzeravatar von Pierre-Alexis
Pierre-Alexis

Grundsätzlich versuche ich, ein neues buchbares Produkt in Woocommerce hinzuzufügen, indem ich ein benutzerdefiniertes Formular verwende, das ich erstellt habe, also muss ich die Produkte programmgesteuert hinzufügen. Das Erstellen des Produkts ist in Ordnung, das Problem ist, dass ich nicht herausfinden kann, wie ich meinem Produkt einen neuen Personentyp hinzufügen kann, damit ich mehrere Preise festlegen kann.

Hat jemand eine Idee, wie man das macht?

Das ist der Code, den ich bisher habe.

$post_id = wp_insert_post( array(
    'post_title' => $_POST["title"],
    'post_content' => $_POST["description"],
    'post_status' => 'publish',
    'post_type' => "product",
) );
wp_set_object_terms( $post_id, 'booking', 'product_type' );

  • Was genau meinst du mit Personentyp? Ich verstehe deine Frage nicht richtig. Kannst du das bitte näher erläutern?

    – Kashalo

    5. Oktober 2018 um 20:08 Uhr

  • @kashalo In Woocommerce-Buchungen können Sie für jedes Buchungsprodukt unterschiedliche Personentypen festlegen…

    – LoicTheAztec

    5. Oktober 2018 um 20:35 Uhr


  • @LoicTheAztec danke für die Erklärung. Ich werde das Buchungs-Plugin installieren und mich umsehen

    – Kashalo

    5. Oktober 2018 um 20:37 Uhr

Benutzeravatar von LoicTheAztec
LoicTheAztec

Folgendes aktiviert “hat Personen” und erstellt Ihre Personentypen für ein buchbares Produkt:

// Create the product
$product_id = wp_insert_post( array(
    'post_title' => $_POST["title"],
    'post_content' => $_POST["description"],
    'post_status' => 'publish',
    'post_type' => "product",
) );

// Set the product type
wp_set_object_terms( $product_id, 'booking', 'product_type' );

// Get an instance of the WC_Product Object
$product = wc_get_product($product_id);

// Enable persons and save
$product->set_has_persons(true);
$product->save();

// Here define your person types (one array by person type)
$persons_type_data =  array(
    // First person type
    array( 
        'block_cost'  => 0,
        'cost'        => 16.50,
        'description' => '',
        'max'         => '',
        'min'         => '',
        'name'        => __('Adults'),
    ),
    // Second person type
    array(
        'block_cost'  => 0,
        'cost'        => 9.20,
        'description' => '',
        'max'         => '',
        'min'         => '',
        'name'        => __('Childs'),
    ),
);

// Loop Through persons type data
foreach( $persons_type_data as $key => $values ){
    $person_type = new WC_Product_Booking_Person_Type();
    $person_type->set_block_cost($values['block_cost']);
    $person_type->set_cost($values['cost']);
    $person_type->set_description($values['description']);
    $person_type->set_max($values['max']);
    $person_type->set_min($values['min']);
    $person_type->set_name($values['name']);
    $person_type->set_parent_id($product_id);
    $person_type->set_sort_order($key); // Sorting is based on the array order

    // Save the person type
    $person_type->save();

    // Add the person type to the array
    $persons[] = $person_type;
}
// Set person types and save the product
$product->set_person_types($persons);
$product->save();

Getestet und funktioniert.

1435450cookie-checkPersonentypen programmgesteuert bei der Erstellung buchbarer Produkte in Woocommerce 3 hinzufügen

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

Privacy policy