【发布时间】: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