【问题标题】:Wordpress Woocommerce - use update_post_meta to add multiple products from HTML POSTWordpress Woocommerce - 使用 update_post_meta 从 HTML POST 添加多个产品
【发布时间】:2015-08-18 19:47:08
【问题描述】:

首先我想对这个网站和贡献者表示感谢,在过去的几年里,它为我学习代码提供了丰富的知识。

这是我的问题/情况。

我编辑/编写了一些代码以将外部产品从 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' ) ) ) );

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

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

【问题讨论】:

  • 您需要循环遍历产品,在当前代码中我没有看到添加多个产品的循环
  • 您好阿南德,感谢您的回复。关于如何为此 php 脚本创建循环的任何提示和线索?谢谢
  • 我一直看到人们使用 foreach ($array as $key => $val) { 来做循环,但不确定输入什么以及放在哪里。谢谢

标签: php arrays wordpress woocommerce http-post


【解决方案1】:

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

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

【讨论】:

  • 太棒了!我会试试这个并感谢您的帮助!
  • 您提供的修改完美!非常感谢!
  • 我很高兴能帮上忙。请点击我的答案旁边的箭头,将答案标记为已接受。
猜你喜欢
  • 2014-06-20
  • 2020-06-16
  • 1970-01-01
  • 2022-08-19
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
相关资源
最近更新 更多