First I wanted to say thank you for this website & the contributors, it has provided a wealth of knowledge over the past years to myself for learning about code.
Here is my problem/situation.
I edited/wrote some code to ADD external products from a HTTP POST into Woocommerce. This is working, but it will only add one product (didn’t realize I needed it to add multiple).
So here is the HTTP Post code I was provided:
$_POST => Array
(
[description] => Array
(
[0] => Lloyd Ultimats Mat: UM4 3175560 1 Pc Front Mat ($124.90)
Mat Color:100 Black
Chevrolet Silverado 1500 2011
Model Feature: Extended Cab
Premium Binding: 508 Silver ($10.00)
Logo: 819257 ($33.00)
SS Large 2010 APP Black
UM4|3175560|446||100|5081||819257|||||
[1] => Lloyd Ultimats Mat: UM7 3175590 1 Pc 2nd Seat Mat ($74.90)
Product Feature: Front Bucket Seats
Mat Color:100 Black
Chevrolet Silverado 1500 2011
Model Feature: Extended Cab
Premium Binding: 508 Silver ($10.00)
Logo: 819257 ($33.00)
SS Large 2010 APP Black
UM7|3175590|4841||100|5081||819257|||||
)
[total] => Array
(
[0] => 167.9
[1] => 117.9
)
[wholesale] => Array
(
[0] => 100.9
[1] => 70.9
)
[retail] => Array
(
[0] => 167.9
[1] => 117.9
)
[part_number] => Array
(
[0] => UM4
[1] => UM7
)
[lloyd_code] => Array
(
[0] => UM4|3175560|446||100|5081||819257|||||
[1] => UM7|3175590|4841||100|5081||819257|||||
)
[weight] => Array
(
[0] => 9.00
[1] => 10.00
)
[total_rows] => 2
)
Here is my PHP code I created, but it only adds one product at a time.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
set_time_limit(0);
require(dirname(__FILE__) . '/wp-load.php');
add_action('init', '$_POST');
global $woocommerce;
session_start();
if (!isset($_SESSION['lloyd_code'])) {
$_SESSION['lloyd_code'] = Array();
}
if (!isset($_SESSION['weight'])) {
$_SESSION['weight'] = Array();
}
if (!isset($_SESSION['total'])) {
$_SESSION['total'] = array();
}
if (!isset($_SESSION['description'])) {
$_SESSION['description'] = array();
}
$lloyd_code = $_POST['lloyd_code']['0'];
$weight = $_POST['weight']['0'];
$description = $_POST['description']['0'];
$total = $_POST['total']['0'];
array_push($_SESSION['lloyd_code'], $lloyd_code);
array_push($_SESSION['weight'], $weight);
array_push($_SESSION['description'], $description);
array_push($_SESSION['total'], $total);
$_POST = array(
'post_author' => "AllAboutFloorMats",
'post_content' => $description,
'post_status' => "publish",
'post_title' => $_POST['description']['0'],
'post_parent' => '',
'post_type' => "product",
'post_excerpt' => $description,
);
//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, 'Lloyds', 'product_cat' );
wp_set_object_terms($post_id, 'simple', 'product_type');
wp_update_post($my_post);
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', 'no');
update_post_meta( $post_id, '_virtual', 'no');
update_post_meta( $post_id, '_regular_price', $total, true);
update_post_meta( $post_id, '_price', $total, true);
update_post_meta( $post_id, '_sale_price', $total, true);
update_post_meta( $post_id, '_purchase_note', $description );
update_post_meta( $post_id, '_featured', "no" );
update_post_meta( $post_id, '_weight', $weight, true );
update_post_meta( $post_id, '_length', "" );
update_post_meta( $post_id, '_width', "" );
update_post_meta( $post_id, '_height', "" );
update_post_meta($post_id, '_sku', $lloyd_code);
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, '_sold_individually', "" );
update_post_meta( $post_id, '_manage_stock', "no" );
update_post_meta( $post_id, '_backorders', "no" );
update_post_meta( $post_id, '_stock', "" );
$woocommerce->cart->add_to_cart( $post_id, $quantity=1 );
exit( wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) ) );
I’m kinda lost on how to add these multiple products at the same time.
Again thanks for any help, guidance, assistance you can provide. If you need any additional info, please let me know.
Basically you need to reference all the elements in the array, currently you are referencing the 1st element only
$lloyd_code = $_POST['lloyd_code']['0']; // 0 = first element
The following code shows how to loop over the products
Please note that down the line in your code you are overwriting the $_POST global, with the following line of code
Replace
$_POST = array (
with another variable, perhaps