WooCommerce Producten & PHP-API
Voorbeeld: 1 Product vanuit PHP
Hier wordt vanuit code een product aangemaakt, zonder database-koppeling oid.
<?php require_once("/var/www/kbo3.dvb/wp-load.php"); #################################################################### # Create product #################################################################### # # Define ###################### # $product_array=array ( 'post_title' => 'Great new product', 'post_content' => 'Here is the content of this new product', 'post_status' => 'publish', 'post_type' => 'product' ); # Create ###################### # $post_id=wp_insert_post($product_array); #################################################################### # Update product #################################################################### # # "simple_product" ################## # wp_set_object_terms($post_id, 'simple', 'product_type'); # Various meta fields ##################### # update_post_meta( $post_id, '_stock_status', 'instock'); update_post_meta( $post_id, 'total_sales', '0' ); update_post_meta( $post_id, '_downloadable', 'no' ); update_post_meta( $post_id, '_virtual', 'no' ); update_post_meta( $post_id, '_regular_price', '12.50' ); update_post_meta( $post_id, '_sale_price', '12.50' ); 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', '0100x' ); # 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', '12.50' ); update_post_meta( $post_id, '_sold_individually', 'yes' ); update_post_meta( $post_id, '_manage_stock', 'no' ); update_post_meta( $post_id, '_backorders', 'no' ); update_post_meta( $post_id, '_stock', '' ); update_post_meta($post_id, 'ean_code', '1234ean5678'); update_post_meta($post_id, 'stuks_per_verpakking', 2);
Procedure voor db-based import
Mbv. PHP-code wil ik productgegevens uit een database peuteren, en die importeren. Da's niet moeilijk. Het import-gedeelte:
############################################################### # Connect with dwh through PDO ############################################################### # $servername = "localhost"; $dbname = "rt_dwh"; $username = "supervrouw"; $password = "incorrect"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8", $username, $password); # set the PDO error mode to exception ##################################### # $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully\n"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage()."\n"; } ############################################################### # Fetch & process table "product_tmp" ############################################################### # $sql = $conn->prepare("select * from product_tmp"); $sql->execute(); $rows = $sql->fetchAll(); print_r($rows); foreach ($rows as $row) { echo $row['sku']."\n"; }
Zie ook PHP & MySQL.
Producten & taal (Polylang)
Ik gebruik Polylang voor een meertalige site. Hoe kan ik...
- Bij import van een product, de taal specificeren?
- Dit product koppelen aan identieke producten (met dezelfde SKU) maar een andere taal?
Taal specificeren van een object
pll_set_post_language
[1]:
Sets the language of a post or page (or custom post type post) Usage: pll_set_post_language($post_id, $lang); * ‘$post_id’ => (required) id of the post for which you want to set the language * ‘$lang’ => (required) language code
Objecten koppelen
pll_save_post_translations
[2]:
Defines which posts are translations of each other Usage: pll_save_post_translations($arr); ‘$arr’ => (required) associative array of translations with language code as key and post id as value
Denk aan zoiets voor elke tuple (zie ook voorbeeld verderop):
$vertalingen = array ( "nl" => $post_id_0, "en" => $post_id, "de" => $post_id_2 ); pll_save_post_translations($vertalingen);
In actie
Tjakka!
<?php # # Create 1 product programmatically ################################################################### # ############################################################### # Call wp-load.php ############################################################### # require_once("/home/strompf/www/rt.dvb/wp-load.php"); #################################################################### # Create product - EN #################################################################### # # Define ###################### # $product_array=array ( 'post_title' => 'Great new product', 'post_content' => 'Here is the content of this new product', 'post_status' => 'publish', 'post_type' => 'product' ); # Create ###################### # $post_id=wp_insert_post($product_array); #################################################################### # Update product #################################################################### # # "simple_product" ################## # wp_set_object_terms($post_id, 'simple', 'product_type'); # Various meta fields ##################### # update_post_meta( $post_id, '_stock_status', 'instock'); update_post_meta( $post_id, 'total_sales', '0' ); update_post_meta( $post_id, '_downloadable', 'no' ); update_post_meta( $post_id, '_virtual', 'no' ); update_post_meta( $post_id, '_regular_price', '12.50' ); update_post_meta( $post_id, '_sale_price', '12.50' ); 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', '0100x' ); update_post_meta( $post_id, '_sale_price_dates_from', '' ); update_post_meta( $post_id, '_sale_price_dates_to', '' ); update_post_meta( $post_id, '_price', '12.50' ); update_post_meta( $post_id, '_sold_individually', 'yes' ); update_post_meta( $post_id, '_manage_stock', 'no' ); update_post_meta( $post_id, '_backorders', 'no' ); update_post_meta( $post_id, '_stock', '' ); update_post_meta($post_id, 'ean_code', '1234ean5678'); update_post_meta($post_id, 'stuks_per_verpakking', 2); # Set language #####################3 # pll_set_post_language($post_id, "en"); #################################################################### # Create product - DE #################################################################### # # Define ###################### # $product_array=array ( 'post_title' => 'Großartiges neues Product', 'post_content' => 'Un hier sind die Bescheiten', 'post_status' => 'publish', 'post_type' => 'product' ); # Create ###################### # $post_id_2=wp_insert_post($product_array); #################################################################### # Update product #################################################################### # # "simple_product" ################## # wp_set_object_terms($post_id_2, 'simple', 'product_type'); # Various meta fields ##################### # update_post_meta( $post_id_2, '_stock_status', 'instock'); update_post_meta( $post_id_2, 'total_sales', '0' ); update_post_meta( $post_id_2, '_downloadable', 'no' ); update_post_meta( $post_id_2, '_virtual', 'no' ); update_post_meta( $post_id_2, '_regular_price', '12.50' ); update_post_meta( $post_id_2, '_sale_price', '12.50' ); update_post_meta( $post_id_2, '_purchase_note', '' ); update_post_meta( $post_id_2, '_featured', 'no' ); update_post_meta( $post_id_2, '_weight', '' ); update_post_meta( $post_id_2, '_length', '' ); update_post_meta( $post_id_2, '_width', '' ); update_post_meta( $post_id_2, '_height', '' ); update_post_meta( $post_id_2, '_sku', '0100x' ); update_post_meta( $post_id_2, '_sale_price_dates_from', '' ); update_post_meta( $post_id_2, '_sale_price_dates_to', '' ); update_post_meta( $post_id_2, '_price', '12.50' ); update_post_meta( $post_id_2, '_sold_individually', 'yes' ); update_post_meta( $post_id_2, '_manage_stock', 'no' ); update_post_meta( $post_id_2, '_backorders', 'no' ); update_post_meta( $post_id_2, '_stock', '' ); update_post_meta($post_id_2, 'ean_code', '1234ean5678'); update_post_meta($post_id_2, 'stuks_per_verpakking', 2); # Set language #####################3 # pll_set_post_language($post_id_2, "de"); #################################################################### # Connect translations #################################################################### # $vertalingen = array ( "en" => $post_id, "de" => $post_id_2 ); pll_save_post_translations($vertalingen);
Zie ook
- Db.bulk (WordPress) - Experimenten tav. producten & taal
- PHP & MySQL
- Productvelden (WooCommerce)
Bronnen
- https://royduineveld.nl/creating-your-own-wordpress-import/
- https://developer.wordpress.org/reference/functions/wp_insert_post/
- http://www.beyondprograms.ca/how-to-add-products-to-wordpress-woocommerce-with-php/
- https://stackoverflow.com/questions/11503646/woocommerce-create-product-by-code
- https://lukasznowicki.info/insert-new-woocommerce-product-programmatically/
- https://devnetwork.io/add-woocommerce-product-programmatically/