Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过编程方式将产品添加到woocommerce(设置产品属性和图像)需要花费很多时间_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 通过编程方式将产品添加到woocommerce(设置产品属性和图像)需要花费很多时间

Php 通过编程方式将产品添加到woocommerce(设置产品属性和图像)需要花费很多时间,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我的项目要求是从远程服务器拉取一个映像,并使用该映像创建一个大小属性为5个大小的产品,例如10X10、20X20、30X30、40X40、50X50将拉取的映像设置为产品并重定向到该产品详细信息页面。 以下是“购买”按钮的URL,单击此处可重定向到您的站点相应页面:您的站点URL?img=您的图片\u URL&cap=您的图片标题&cat=您的图片类别&isrc=图片\u来源\u站点 这是我的密码: $caption = isset($_GET['cap']) ? $_GET['cap'

我的项目要求是从远程服务器拉取一个映像,并使用该映像创建一个大小属性为5个大小的产品,例如10X10、20X20、30X30、40X40、50X50将拉取的映像设置为产品并重定向到该产品详细信息页面。
以下是“购买”按钮的URL,单击此处可重定向到您的站点相应页面:

您的站点URL?img=您的图片\u URL&cap=您的图片标题&cat=您的图片类别&isrc=图片\u来源\u站点
这是我的密码:

    $caption = isset($_GET['cap']) ? $_GET['cap'] : '';
    $url_category = isset($_GET['cat']) ? $_GET['cat'] : '';
    $image_source = isset($_GET['isrc']) ? $_GET['isrc'] : '';
    $diff_paths = wp_upload_dir();
    $saveto = $diff_paths['path'] . '/' . md5(rand(0, 1000)) . time() . '.jpg';
    $image = $_GET['img'];      
    try {
        $ch = curl_init($image);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        $raw = curl_exec($ch);

        if ($raw === false) {
            echo "<h3>Something went wrong, kindly try again later. Error :" . curl_error($ch) . " </h3>";
            die();
        } else
        if (file_exists($saveto)) {
            unlink($saveto);
        }
        curl_close($ch);
        $fp = fopen($saveto, 'x');
        fwrite($fp, $raw);
        fclose($fp);
    } catch (Exception $e) {
        echo $e . getMessage();
        die();
    }
    $prod_info = addaprdct_to_woo($saveto, $caption, $url_category);

    $wpdb->insert('wp_product_imageurl_mapping', array(
        'product_id' => $prod_info['prod_id'],
        'image_url' => urlencode($_GET['img']),
        'image_source' => urlencode($image_source)
    ));
    ob_start();
    header("location:" . $prod_info['prod_url']);
    ob_end_flush();
    die();
$caption=isset($\u GET['cap'])$_获取['cap']:'';
$url\u category=isset($\u GET['cat'])$_获取['cat']:'';
$image\u source=isset($\u GET['isrc'])$_获取['isrc']:'';
$diff_path=wp_upload_dir();
$saveto=$diff_路径['path']./'。md5(兰特(0,1000))。时间()。jpg';
$image=$_GET['img'];
试一试{
$ch=curl_init($image);
curl_setopt($ch,CURLOPT_头,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,1);
$raw=curl\u exec($ch);
如果($raw==false){
echo“出现问题,请稍后再试。错误:“.curl_Error($ch)”;
模具();
}否则
如果(文件_存在($saveto)){
取消链接($saveto);
}
卷曲关闭($ch);
$fp=fopen($saveto,'x');
fwrite($fp,$raw);
fclose($fp);
}捕获(例外$e){
echo$e.getMessage();
模具();
}
$prod\u info=addaprdct\u to\u woo($saveto,$caption,$url\u category);
$wpdb->insert('wp\u product\u imageurl\u mapping',数组(
'product_id'=>$prod_info['prod_id'],
'image\u url'=>urlencode($\u GET['img']),
'image\u source'=>urlencode($image\u source)
));
ob_start();
标题(“位置:.$prod_info['prod_url']);
ob_end_flush();
模具();


function addaprdct_to_woo($prod_img,$caption,$url_category) { 
global $wpdb;    
$insertLog = "insert_product_logs.txt"; //name the log file in wp-admin folder
$post = array();
    $post = array(
    'post_title' => "TB#".time(),
    'post_content' => $caption,
    'post_status' => "publish",
     //        'post_excerpt' => "product excerpt content...",
    'post_name' => "TB#".time(), //name/slug
    'post_type' => "product"
);

    //Create product/post:
$new_post_id = wp_insert_post($post, $wp_error);
$logtxt = "PrdctID: $new_post_id\n";

    //make product type be variable:
wp_set_object_terms($new_post_id, 'variable', 'product_type');
   //add category to product:
wp_set_object_terms($new_post_id, $url_category, 'product_cat');

    //################### Add size attributes to main product: ####################
    //Array for setting attributes
$avail_attributes = array(
    '8"x10"',
    '16"x20"',
    '20"x24"',
    '20"x30"',
    '24"x24"',
    '36"x48"'
);
wp_set_object_terms($new_post_id, $avail_attributes, 'pa_size');

$thedata = Array('pa_size' => Array(
        'name' => 'pa_size',
        'value' => '',
        'is_visible' => '1',
        'is_variation' => '1',
        'is_taxonomy' => '1'
));
update_post_meta($new_post_id, '_product_attributes', $thedata);
//########################## Done adding attributes to product #################
 //set product values:
update_post_meta($new_post_id, '_stock_status', 'instock');
 //    update_post_meta($new_post_id, '_weight', "0.06");
 //    update_post_meta($new_post_id, '_sku', "skutest1");
update_post_meta($new_post_id, '_stock', "100");
update_post_meta($new_post_id, '_visibility', 'visible');

  //###################### Add Variation post types for sizes #############################
  //insert 5 variations post_types for 8"x10", 16"x20", 20"x30", 20"x24", 24"x24":
$i = 1;
while ($i <= 6) {//while creates 5 posts(1 for ea. size variation 8"x10", 16"x20" etc):
    $my_post = array(
        'post_title' => 'Variation #' . $i . ' of 6 for prdct#' . $new_post_id,
        'post_name' => 'product-' . $new_post_id . '-variation-' . $i,
        'post_status' => 'publish',
        'post_parent' => $new_post_id, //post is a child post of product post
        'post_type' => 'product_variation', //set post type to product_variation
        'guid' => home_url() . '/?product_variation=product-' . $new_post_id . '-variation-' . $i
    );

    //Insert ea. post/variation into database:
    $attID = wp_insert_post($my_post);
    $logtxt .= "Attribute inserted with ID: $attID\n";
    //set IDs for product_variation posts:
    $variation_id = $new_post_id + 1;
    $variation_two = $variation_id + 1;
    $variation_three = $variation_two + 1;
    $variation_four = $variation_three + 1;
    $variation_five = $variation_four + 1;
    $variation_six = $variation_five + 1;

    //Create 8"x10" variation for ea product_variation:
    update_post_meta($variation_id, 'attribute_pa_size', '8x10');
    update_post_meta($variation_id, '_price', 29.49);
    update_post_meta($variation_id, '_regular_price', '29.49');
    //add size attributes to this variation:
    wp_set_object_terms($variation_id, $avail_attributes, 'pa_size');
    $thedata = Array('pa_size' => Array(
            'name' => '8x10',
            'value' => '',
            'is_visible' => '1',
            'is_variation' => '1',
            'is_taxonomy' => '1'
    ));
    update_post_meta($variation_id, '_product_attributes', $thedata);

    //Create 16"x20" variation for ea product_variation:
    update_post_meta($variation_two, 'attribute_pa_size', '16x20');
    update_post_meta($variation_two, '_price', 39.49);
    update_post_meta($variation_two, '_regular_price', '39.49');
    //add size attributes:
    wp_set_object_terms($variation_two, $avail_attributes, 'pa_size');
    $thedata = Array('pa_size' => Array(
            'name' => '16x20',
            'value' => '',
            'is_visible' => '1',
            'is_variation' => '1',
            'is_taxonomy' => '1'
    ));
    update_post_meta($variation_two, '_product_attributes', $thedata);

    //Create 20"x30" variation for ea product_variation:
    update_post_meta($variation_three, 'attribute_pa_size', '20x30');
    update_post_meta($variation_three, '_price', 59.49);
    update_post_meta($variation_three, '_regular_price', '59.49');
    wp_set_object_terms($variation_three, $avail_attributes, 'pa_size');
    $thedata = Array('pa_size' => Array(
            'name' => '20x30',
            'value' => '',
            'is_visible' => '1',
            'is_variation' => '1',
            'is_taxonomy' => '1'
    ));
    update_post_meta($variation_three, '_product_attributes', $thedata);

    //Create 20"x24" variation for ea product_variation:
    update_post_meta($variation_four, 'attribute_pa_size', '20x24');
    update_post_meta($variation_four, '_price', 49.49);
    update_post_meta($variation_four, '_regular_price', '49.49');
    wp_set_object_terms($variation_four, $avail_attributes, 'pa_size');
    $thedata = Array('pa_size' => Array(
            'name' => '20x24',
            'value' => '',
            'is_visible' => '1',
            'is_variation' => '1',
            'is_taxonomy' => '1'
    ));
    update_post_meta($variation_four, '_product_attributes', $thedata);

    //Create 24"x24" variation for ea product_variation:
    update_post_meta($variation_five, 'attribute_pa_size', '24x24');
    update_post_meta($variation_five, '_price', 69.49);
    update_post_meta($variation_five, '_regular_price', '69.49');
    wp_set_object_terms($variation_five, $avail_attributes, 'pa_size');
    $thedata = Array('pa_size' => Array(
            'name' => '24x24',
            'value' => '',
            'is_visible' => '1',
            'is_variation' => '1',
            'is_taxonomy' => '1'
    ));
    update_post_meta($variation_five, '_product_attributes', $thedata);
    //Create 24"x24" variation for ea product_variation:
    update_post_meta($variation_six, 'attribute_pa_size', '36x48');
    update_post_meta($variation_six, '_price', 99.49);
    update_post_meta($variation_six, '_regular_price', '99.49');
    wp_set_object_terms($variation_six, $avail_attributes, 'pa_size');
    $thedata = Array('pa_size' => Array(
            'name' => '36x48',
            'value' => '',
            'is_visible' => '1',
            'is_variation' => '1',
            'is_taxonomy' => '1'
    ));
    update_post_meta($variation_six, '_product_attributes', $thedata);

    $i++;
}//end while i is less than or equal to 5(for 5 size variations)
 //############################ Done adding variation posts ############################
//add product image:
Generate_Featured_Image($prod_img, $new_post_id);
$prod_info = array();
$prod_info['prod_id'] = $new_post_id;
$prod_info['prod_url'] = get_permalink($new_post_id);
return $prod_info;
}

function Generate_Featured_Image($image_url, $post_id) {
    $upload_dir = wp_upload_dir();
    $image_data = file_get_contents($image_url);
    $filename = basename($image_url);
    if (wp_mkdir_p($upload_dir['path']))
      $file = $upload_dir['path'] . '/' . $filename;
    else
      $file = $upload_dir['basedir'] . '/' . $filename;
       file_put_contents($file, $image_data);

      $wp_filetype = wp_check_filetype($filename, null);
    $attachment = array(
      'post_mime_type' => $wp_filetype['type'],
      'post_title' => sanitize_file_name($filename),
      'post_content' => '',
      'post_status' => 'inherit'
    );
    $attach_id = wp_insert_attachment($attachment, $file, $post_id);
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attach_data = wp_generate_attachment_metadata($attach_id, $file);
    $res1 = wp_update_attachment_metadata($attach_id, $attach_data);
    $res2 = set_post_thumbnail($post_id, $attach_id);
}
函数addaprdct\u to\u woo($prod\u img,$caption,$url\u category){
全球$wpdb;
$insertLog=“insert\u product\u logs.txt”;//在wp admin文件夹中命名日志文件
$post=array();
$post=数组(
'post_title'=>“TB#”。.time(),
“post_内容”=>$caption,
“发布状态”=>“发布”,
//“post_摘录”=>“产品摘录内容…”,
'post_name'=>“TB#”。.time(),//name/slug
“发布类型”=>“产品”
);
//创建产品/帖子:
$new\u post\u id=wp\u insert\u post($post,$wp\u error);
$logtxt=“PrdctID:$new\u post\u id\n”;
//使产品类型可变:
wp_set_object_术语($new_post_id,'variable','product_type');
//将类别添加到产品:
wp_set_object_术语($new_post_id,$url_category,'product_cat');
//###################将尺寸属性添加到主产品:####################
//用于设置属性的数组
$avail\u attributes=数组(
“8”x10“,
“16”x20“,
“20”x24“,
“20”x30“,
“24”x24“,
'36英寸x48英寸'
);
wp_set_object_terms($new_post_id,$avail_attributes,'pa_size');
$thedata=数组('pa\u size'=>数组(
'name'=>'pa_size',
'值'=>'',
'是否可见'=>'1',
'is_variation'=>'1',
'is_分类法'=>'1'
));
更新发布meta($new发布id,$product属性,$thedata);
//##########################完成向产品添加属性#################
//设置产品值:
更新发布元数据($new发布id、'U stock'u status、'instock');
//更新帖子元($new帖子id,'uweight','0.06”);
//更新发布元数据($new发布id,'sku','skutest1');
更新发布元数据($new发布id,'U stock','100');
更新发布元数据($new发布id,'.'可见性','可见');
//######################添加尺寸的变化后类型#############################
//插入8“x10”、16“x20”、20“x30”、20“x24”、24“x24”的5种变型柱式:
$i=1;
而prdct的($i‘Variation#’)、$i.$6、$new#post#id、,
“发布名称”=>“产品-”.$new\u发布id.-variation-”.$i,
“发布状态”=>“发布”,
'post\u parent'=>$new\u post\u id,//post是产品post的子post
'post_type'=>'product_variation',//将post type设置为product_variation
'guid'=>home\u url()。/?product\u variation=product-'.$new\u post\u id'.-variation-'.$i
);
//将ea.post/变更插入数据库:
$attID=wp\u insert\u post($my\u post);
$logtxt.=“插入了ID为$attID的属性\n”;
//为产品变更帖子设置ID:
$variation\u id=$new\u post\u id+1;
$variation\u two=$variation\u id+1;
$variation_三=$variation_二+1;
$variation\u four=$variation\u four+1;
$variation\u five=$variation\u four+1;
$variation_六=$variation_五+1;
//为ea产品_变体创建8个“x10”变体:
更新后元($variation\u id,'attribute\u pa\u size','8x10');
更新后元($variation\u id,'u price',29.49);
更新后元($variation\u id,''u regular\u price','29.49');
//将大小属性添加到此变体:
wp_set_object_terms($variation_id,$avail_attributes,'pa_size');
$thedata=数组('pa\u size'=>数组(
“名称”=>“8x10”,
'值'=>'',
'是否可见'=>'1',
'is_variation'=>'1',
'is_分类法'=>'1'
));
更新发布元数据($variation\u id、$product\u attributes、$thedata);
//为ea产品变量创建16“x20”变量:
更新后元($variation_two,'attribute_pa_size','16x20');
更新后元($variation\u two,'u price',39.49);
更新后元($variation\u two,“'u常规价格,'39.49”);
//添加大小属性:
wp_set_object_terms($variation_two,$avail_attributes,'pa_size');
$thedata=数组('pa\u size'=>数组(
“名称”=>“16x20”,
'值'=>'',
'是否可见'=>'1',
'is_variation'=>'1',
'is_分类法'=>'1'
));
更新后元($variation\u two,$product\u attributes,$thedata);
//为ea产品创建20“x30”变体