Prestashop根据csv获取产品属性并在csv

时间:2016-09-30 03:30:15

标签: php prestashop fgetcsv

我有一个csv文件。该文件是这样的

Product_id, Product_quantity
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,

现在我已经开发了一个模块,它将从产品ID中获取所有这些产品质量 并将它们写在csv文件上而不更改任何名称

因此,我的代码就像这样

Class GetProducts extends Module {
    public function __construct() {
        $this->name = 'getproducts';
        $this->tab = 'pricing_promotion';
        $this->version = '1.0.0';
        $this->author = 'NewUser';
        $this->need_instance = 0;
        $this->bootstrap = 'true';
        parent::__construct();
        $this->displayName = $this->l('Get Products');
        $this->description = $this->l('lorem ipsum');
        $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
        $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
    }

    public function install() {
            $return = (parent::install() 
                && $this->registerHook('header'));
            return $return;         
    }

    public function uninstall() {
            if ( !parent::uninstall() )
                return false;
            return true;            
    }

    public function hookHeader() {
        $this->getProductQuantity();
    }

    public function getcsvUpdate() {
        $file = "http://localhost/projects/product.csv";
        $csv = array_map("str_getcsv", file($file,FILE_SKIP_EMPTY_LINES));
        $keys = array_shift($csv);
        print_r($keys); //getting csv headers

        $stock_array = array();
        if (($handle = fopen($file, "r")) !== FALSE) {
            fgetcsv($handle);

            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $stock_array[] = $this->get_qty($data[0]);
            }
            fclose($handle);
        }

        foreach( $stock_array as $stock ) {
            print_r($stock); //getting quantity and product id in array
        }

    }

    public function get_qty($id_product) {
        $product = new Product();
        $product_data[] = $id_product;
        $product_data[] = $product->getQuantity($id_product);
        return $product_data;
    }

}

你可以看到我有csv标头和我得到产品ID的数组 数量。那么有人可以告诉我如何将这些值写入csv 没有删除csv中的现有数据?任何帮助和建议都将是真的 明显的。

0 个答案:

没有答案