通过模块预先安装具有功能和图像的新产品

时间:2015-04-24 07:04:12

标签: php prestashop

除功能和图像外,还成功添加了产品信息。这是我的代码:

 $product = new Product();
    $image = new Image();
    $langId = (int) (Configuration::get('PS_LANG_DEFAULT'));
    $name = Tools::getValue('pr_name');
    $product->id_supplier =  $arr['pr_supp'][0];
    $product->id_manufacturer = Tools::getValue('pr_manuf');
    $product->description_short = Tools::getValue('pr_short_desc');
    $product->description = Tools::getValue('pr_desc');
    $product->quantity = Tools::getValue('pr_cant');

    $product->name = array($langId => $name);
    $product->price = Tools::getValue('pr_pret_ach');
    $product->wholesale_price = Tools::getValue('pr_pret_angro');
    $product->active = false;
    //$product->is_virtual = true;
   // $product->cache_has_attachments = true;
    $product->id_category_default= $arr['categorii'][0];
    $product->reference = $this->context->cookie->id_customer;
    $product->link_rewrite = array($langId => Tools::link_rewrite($name));

    //some test values
    $product->product_features = array(
        'id' => '123',
        'id_feature_value' => '111'
    );

    $product->add();



    //trying to save images (got just images src from another server)
    $image->id_product = (int)$product->id;

     $i=0;

while($i <= $num){
    $imgs[$i] = Tools::getValue('image-'.$i);
     AdminImportController::copyImg2($product->id, $imgs[$i], 'products');
     $i++;
}

以下是copyImg2的代码(这只是默认函数copyImg的克隆):

public static function copyImg2($id_entity, $id_image = null, $url, $entity = 'products', $regenerate = true)
{
    $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
    $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));

    switch ($entity)
    {
        default:
        case 'products':
            $image_obj = new Image($id_image);
            $path = $image_obj->getPathForCreation();
        break;
        case 'categories':
            $path = _PS_CAT_IMG_DIR_.(int)$id_entity;
        break;
        case 'manufacturers':
            $path = _PS_MANU_IMG_DIR_.(int)$id_entity;
        break;
        case 'suppliers':
            $path = _PS_SUPP_IMG_DIR_.(int)$id_entity;
        break;
    }
    $url = str_replace(' ', '%20', trim($url));

    // Evaluate the memory required to resize the image: if it's too much, you can't resize it.
    if (!ImageManager::checkImageMemoryLimit($url))
        return false;

    // 'file_exists' doesn't work on distant file, and getimagesize makes the import slower.
    // Just hide the warning, the processing will be the same.
    if (Tools::copy($url, $tmpfile))//this always returning false
    {
        ImageManager::resize($tmpfile, $path.'.jpg');
        $images_types = ImageType::getImagesTypes($entity);
                //domo
                $res = array();   
                $nr = 0;

        if ($regenerate)
            foreach ($images_types as $image_type)
            {
                $res[0] = ImageManager::resize($tmpfile, $path.'-'.stripslashes($image_type['name']).'.jpg', $image_type['width'], $image_type['height']);
                if (in_array($image_type['id_image_type'], $watermark_types))
                    Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));

                                    $nr++;
                            }
    }
    else
    {
        unlink($tmpfile);
        return 'false';
    }
    unlink($tmpfile);
    return $res;
}

public static function copy($source, $destination, $stream_context = null)
    {
        if (is_null($stream_context) && !preg_match('/^https?:\/\//', $source))
            return @copy($source, $destination);
        return @file_put_contents($destination, Tools::file_get_contents($source, false, $stream_context));
    }

另一个问题是如何保存功能。以下是print_r()对象的$product

 ....
[category] => 
[webserviceParameters:protected] => Array
    (
        [objectMethods] => Array
            (
                [add] => addWs
                [update] => updateWs
            )

        [objectNodeNames] => products
        [fields] => Array
            (
                [id_manufacturer] => Array
                    (
                        [xlink_resource] => manufacturers
                    )




                [id_default_image] => Array
                    (
                        [getter] => getCoverWs
                        [setter] => setCoverWs
                        [xlink_resource] => Array
                            (
                                [resourceName] => images
                                [subResourceName] => products
                            )

                    )

                [id_default_combination] => Array
                    (
                        [getter] => getWsDefaultCombination
                        [setter] => setWsDefaultCombination
                        [xlink_resource] => Array
                            (
                                [resourceName] => combinations
                            )

                    )

                [id_tax_rules_group] => Array
                    (
                        [xlink_resource] => Array
                            (
                                [resourceName] => tax_rule_groups
                            )

                    )


            )

        [associations] => Array
            (
                [categories] => Array
                    (
                        [resource] => category
                        [fields] => Array
                            (
                                [id] => Array
                                    (
                                        [required] => 1
                                    )

                            )

                    )

                [images] => Array
                    (
                        [resource] => image
                        [fields] => Array
                            (
                                [id] => Array
                                    (
                                    )

                            )

                    )

               //here may be the features, but i don't know how to put it there
                [product_features] => Array
                    (
                        [resource] => product_feature
                        [fields] => Array
                            (
                                [id] => Array
                                    (
                                        [required] => 1
                                    )

                                [custom] => Array
                                    (
                                        [required] => 
                                    )

                                [id_feature_value] => Array
                                    (
                                        [required] => 1
                                        [xlink_resource] => product_feature_values
                                    )

                            )

                    )

1 个答案:

答案 0 :(得分:1)

谈论图片:

首先制作一个循环来重复这个:

$img = new Image();
$img->id_product = $product->id;
$img->add();

你可以在这里做更多。语言等。在此之后更新:

$this->copyImg($product->id, $img->id, $yourImagePath)