Wordpress Woocommerce -使用update_post_meta从HTML POST添加多个产品


Wordpress Woocommerce - use update_post_meta to add multiple products from HTML POST

首先我想说谢谢你的网站& &;在过去的几年里,它为我自己学习代码提供了丰富的知识。

这是我的问题/情况。

我编辑/编写了一些代码,从HTTP POST添加外部产品到Woocommerce。这是工作的,但它只会添加一个产品(没有意识到我需要它来添加多个)。

这里是提供给我的HTTP Post代码:

$_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
)

这是我创建的PHP代码,但它一次只添加一个产品。

 <?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' ) ) ) );

我有点不知道如何同时添加这些多个产品。

再次感谢您提供的任何帮助、指导和帮助。如果您需要任何其他信息,请告诉我。

基本上你需要引用数组中的所有元素,目前你只引用第一个元素

$lloyd_code = $_POST['lloyd_code']['0']; // 0 = first element

下面的代码显示了如何遍历产品

$i = 0;
for ( $i; $i < $_POST['total_rows']; $i++ ) {
$lloyd_code = $_POST['lloyd_code'][$i];
$weight = $_POST['weight'][$i];
$description = $_POST['description'][$i];
$total = $_POST['total'][$i];
// rest of the code continues...
}  //end for loop

请注意,在您的代码中,您将使用以下代码行覆盖$_POST全局变量

$_POST = array(
 'post_author' => "AllAboutFloorMats",
 'post_content' => $description,
 'post_status' => "publish",
 'post_title' => $_POST['description']['0'],
 'post_parent' => '',
 'post_type' => "product",
 'post_excerpt' => $description,
 );
 $post_id = wp_insert_post( $_POST, $wp_error );

$_POST = array (替换为另一个变量,例如

$my_post = array(
 'post_author' => "AllAboutFloorMats",
 'post_content' => $description,
 'post_status' => "publish",
 'post_title' => $_POST['description'][$i],
 'post_parent' => '',
 'post_type' => "product",
 'post_excerpt' => $description,
 );
 $post_id = wp_insert_post( $my_post, $wp_error );