So fügen Sie ein Produkt im Woocommerce mit dem PHP-Code hinzu
-
-
Das Hinzufügen von Produkten über PHP wirdeine Menge Arbeitbedeuten,da viele verschiedene Dingeeingefügt/aktualisiert werdenmüssen.Vielleicht helfen [diese Antwort] (http://stackoverflow.com/a/12658584/1815847) und die zugehörigen Plugins Ihnen dabei,die Arbeiteinfacher zuerledigen :)Adding products via PHP will be quite a lot of work as there a lot of different things to insert/update. Maybe [this answer](http://stackoverflow.com/a/12658584/1815847) and the related plugins will help you to get the job done more easily :)
- 1
- 2014-03-10
- Sven
-
Verwenden Sie 2017 die REST-API,wie unter https://stackoverflow.com/a/40133117/5749914 vorgeschlagen.In 2017, use the REST API as suggested in https://stackoverflow.com/a/40133117/5749914.
- 2
- 2017-06-17
- Warlike Chimpanzee
-
1 Antworten
- Stimmen
-
- 2014-03-11
Esist ziemlicheinfach,wenn Sie dieim Post-Meta hinzugefügten Daten herausgearbeitet haben. Ich habe Problemebeim Herunterladen herunterladbarer Produkte zum Store.
untenist der Code,denich verwende,listet alle Post-Meta auf,die von woo Commerce verwendet werden. Dadurch wirdein Produkt veröffentlicht,der Download-Link wirdjedochnicht angehängt.
Ursprünglich habeichbeim Starteinen Fehlermit dem Arraygemacht,in dem der Download-Linkgespeichertist,wodurchein fehlerhafter Link "b"erzeugt wurde,gefolgt voneiner zweiten korrekten Download-Datei. Nachdem das Array so angepasst wurde,dassesmit demeinesmanuell hinzugefügten Produkts übereinstimmt,zeigt kein Einzelgängereine Datei an. Wennjemand Informationen dazu hat,wäreer sehr dankbar
$post = array( 'post_author' => $user_id, 'post_content' => '', 'post_status' => "publish", 'post_title' => $product->part_num, 'post_parent' => '', 'post_type' => "product", ); //Create post $post_id = wp_insert_post( $post, $wp_error ); if($post_id){ $attach_id = get_post_meta($product->parent_id, "_thumbnail_id", true); add_post_meta($post_id, '_thumbnail_id', $attach_id); } wp_set_object_terms( $post_id, 'Races', 'product_cat' ); wp_set_object_terms( $post_id, 'simple', 'product_type'); update_post_meta( $post_id, '_visibility', 'visible' ); update_post_meta( $post_id, '_stock_status', 'instock'); update_post_meta( $post_id, 'total_sales', '0'); update_post_meta( $post_id, '_downloadable', 'yes'); update_post_meta( $post_id, '_virtual', 'yes'); update_post_meta( $post_id, '_regular_price', "1" ); update_post_meta( $post_id, '_sale_price', "1" ); update_post_meta( $post_id, '_purchase_note', "" ); update_post_meta( $post_id, '_featured', "no" ); update_post_meta( $post_id, '_weight', "" ); update_post_meta( $post_id, '_length', "" ); update_post_meta( $post_id, '_width', "" ); update_post_meta( $post_id, '_height', "" ); update_post_meta( $post_id, '_sku', ""); update_post_meta( $post_id, '_product_attributes', array()); update_post_meta( $post_id, '_sale_price_dates_from', "" ); update_post_meta( $post_id, '_sale_price_dates_to', "" ); update_post_meta( $post_id, '_price', "1" ); update_post_meta( $post_id, '_sold_individually', "" ); update_post_meta( $post_id, '_manage_stock', "no" ); update_post_meta( $post_id, '_backorders', "no" ); update_post_meta( $post_id, '_stock', "" ); // file paths will be stored in an array keyed off md5(file path) $downdloadArray =array('name'=>"Test", 'file' => $uploadDIR['baseurl']."/video/".$video); $file_path =md5($uploadDIR['baseurl']."/video/".$video); $_file_paths[ $file_path ] = $downdloadArray; // grant permission to any newly added files on any existing orders for this product // do_action( 'woocommerce_process_product_file_download_paths', $post_id, 0, $downdloadArray ); update_post_meta( $post_id, '_downloadable_files', $_file_paths); update_post_meta( $post_id, '_download_limit', ''); update_post_meta( $post_id, '_download_expiry', ''); update_post_meta( $post_id, '_download_type', ''); update_post_meta( $post_id, '_product_image_gallery', '');
hoffe,diesentspricht dem Qualitätsstandard :)
Its pretty easy one you have worked out the data added in the post meta. Trouble I am having is adding downloadable products to the store.
below is the code i am using it lists all the post meta that is used by woo commerce. This publishes a product however the download link will not attach.
Originally when i started i made an error with the array that stores the download link producing a bad link "b" followed by a second download file that was correct. After fixing the array to match that of a product manually added it no loner will show a file. If anyone has info on this it would be greatly appreciated
$post = array( 'post_author' => $user_id, 'post_content' => '', 'post_status' => "publish", 'post_title' => $product->part_num, 'post_parent' => '', 'post_type' => "product", ); //Create post $post_id = wp_insert_post( $post, $wp_error ); if($post_id){ $attach_id = get_post_meta($product->parent_id, "_thumbnail_id", true); add_post_meta($post_id, '_thumbnail_id', $attach_id); } wp_set_object_terms( $post_id, 'Races', 'product_cat' ); wp_set_object_terms( $post_id, 'simple', 'product_type'); update_post_meta( $post_id, '_visibility', 'visible' ); update_post_meta( $post_id, '_stock_status', 'instock'); update_post_meta( $post_id, 'total_sales', '0'); update_post_meta( $post_id, '_downloadable', 'yes'); update_post_meta( $post_id, '_virtual', 'yes'); update_post_meta( $post_id, '_regular_price', "1" ); update_post_meta( $post_id, '_sale_price', "1" ); update_post_meta( $post_id, '_purchase_note', "" ); update_post_meta( $post_id, '_featured', "no" ); update_post_meta( $post_id, '_weight', "" ); update_post_meta( $post_id, '_length', "" ); update_post_meta( $post_id, '_width', "" ); update_post_meta( $post_id, '_height', "" ); update_post_meta( $post_id, '_sku', ""); update_post_meta( $post_id, '_product_attributes', array()); update_post_meta( $post_id, '_sale_price_dates_from', "" ); update_post_meta( $post_id, '_sale_price_dates_to', "" ); update_post_meta( $post_id, '_price', "1" ); update_post_meta( $post_id, '_sold_individually', "" ); update_post_meta( $post_id, '_manage_stock', "no" ); update_post_meta( $post_id, '_backorders', "no" ); update_post_meta( $post_id, '_stock', "" ); // file paths will be stored in an array keyed off md5(file path) $downdloadArray =array('name'=>"Test", 'file' => $uploadDIR['baseurl']."/video/".$video); $file_path =md5($uploadDIR['baseurl']."/video/".$video); $_file_paths[ $file_path ] = $downdloadArray; // grant permission to any newly added files on any existing orders for this product // do_action( 'woocommerce_process_product_file_download_paths', $post_id, 0, $downdloadArray ); update_post_meta( $post_id, '_downloadable_files', $_file_paths); update_post_meta( $post_id, '_download_limit', ''); update_post_meta( $post_id, '_download_expiry', ''); update_post_meta( $post_id, '_download_type', ''); update_post_meta( $post_id, '_product_image_gallery', '');
hope this conforms to the quality standard :)
-
Bearbeitennach wochenlangem Suchen stellt sich heraus,dassichein Leerzeichennach den "_downloadable_files" habe,so dasses von woo Commercenichterkannt wurde.Außerdem habeichgelesen,dass die Dateienim Ordnerfür Uploads von woo Commercegespeichert werden.Edit after weeks of searching it turns out that i have a space after the "_downloadable_files" so it wasnt recognized by woo commerce. Also i have read that the files my be stored under the woo commerce uploads folder.
- 0
- 2014-03-11
- user3361421
-
Mit all diesen update_post_meta habeich keine Möglichkeitgefunden,die Kurzbeschreibung des hinzugefügten Produktsfestzulegen ... Wie kannich die Kurzbeschreibungeines Produktsmit PHP-Codefestlegen?With all those update_post_meta I didn't find a way to set the short description of the product added...How can I set the short description of a product with php code?
- 0
- 2014-09-25
- prelite
-
Ich habe anetwas Ähnlichemgearbeitet,aberfestgestellt,dassnach der Verwendung von wp_insert_post der Beitragerstellt und Dateneingegeben werden,der Beitragjedochnicht auf der Woo-Shop-Seite und die Kategorienichtin der Seitenleiste angezeigt wird.Sehr seltsam,da der Beitrag und alle seine Datenim Hintergrund vorhanden sind.I've been working on something similar to this, but found that after using wp_insert_post the post is created and data entered, but the post doesn't appear in the woo shop page, and the category doesn't appear in the sidebar. Very strange as the post and all of it's data exists in the backed.
- 2
- 2014-12-03
- EHerman
-
@preliteist derpost_excerptnicht die Kurzbeschreibung?@prelite isn't the post_excerpt the short-description?
- 0
- 2017-03-03
- Daniel
-
Funktioniertgenau wieerwartetWorks exactly as expected
- 0
- 2018-12-10
- Alaksandar Jesus Gene
-
Jetzt können Sie 'meta_input' verwenden,um alle Metasinnerhalb der `wp_insert_post ()` -Methodefestzulegen.Now you can use 'meta_input' to set all the meta within the `wp_insert_post()` method.
- 1
- 2018-12-21
- Bjorn
-
Wie stelleich das Hauptbild und die Galerie des Produktsein?How to set product main image and gallery?
- 0
- 2020-02-14
- Muzaffar Mahmood
Ichmöchte Produktemit PHP-Code wiefolgt hinzufügen:
aber dieser Code optimiertfür WooCommerce wie Beitragstyp und Guid und Metadaten und ... Kannjemand helfen?