如何在prestashop中更新产品?

时间:2015-10-12 11:13:25

标签: php prestashop

我正在尝试为用户开发一个表单来更新他们的产品。 我已经开发了一个,但它只允许我更新名称和描述,我是POO开发的新手,如果我所做的是对的,我不会。

<?php
class AdminCreationEditProduct1ModuleFrontController extends ModuleFrontController {
    public $auth = true;
        public $ssl = true;
        private $done_traitement = true;

        public function init() {

            parent::init();

            $this->customer = $this->context->customer;

        }

public function postProcess() {
if (Tools::isSubmit('action'))
{

             global $cookie;
             if (!isset($cookie->id_customer)) 
             {
             $this->errors[] = Tools::displayError('Aucun client loggé.');
             }
             else
             {
             $action = Tools::getValue('action');
        $product_id = Tools::getValue('id');
        if ($action == 'update') 
        {


             $product = new Product((int) $product_id);
             $lang_id = $this->context->language->id;
               $product_name = Tools::getValue('product_name');
                $product_keywords = Tools::getValue('product_keywords');

                // recuperation de la catégorie
                $home_categories = Category::getHomeCategories($lang_id);
                $product_description = Tools::getValue('product_description');
                $categories = array();
                foreach ($home_categories as $home_category) {
                    $category_name = 'category-' . $home_category['id_category'];
                    if (isset($_REQUEST[$category_name]))
                        $categories[] = Tools::getValue('category-' . $home_category['id_category']);
                }

                $languages = Language::getLanguages();
        foreach ($languages as $lang) {
            $product->name[$lang['id_lang']] = $product_name;

            $product->description[$lang['id_lang']] = $product_description;
        }
        $product_color = Tools::getValue('product_color');
        if ($product_color != 'none' && trim($product_color) != '') {
            $attributes = array();
            $attributes[0] = $product_color;
            $id_product_attribute = $product->addAttribute(0, 0, 0, 0, array(), '', '', 0);
            $combination = new Combination((int) $id_product_attribute);
            $combination->setAttributes($attributes);
        }
         /*$idprod = $this->updateProduct($product_name, $product_color, $categories, 1, $product_description);
                if ($idprod) {
                    $this->done_traitement = true;
                    $this->context->smarty->assign('confirmation', 1);
                } else {
                    $this->errors[] = Tools::displayError('Erreur dans le creation du produit.');
                }*/
                //styles
                $lang_id = $this->context->language->id;
        $features = array();
        $features = Feature::getFeatures($lang_id);

        foreach ($features as $feature) {
            $feature_name = 'feature_' . $feature['id_feature'] . '_value';
            if (isset($_REQUEST[$feature_name])) {
                $feature_value = Tools::getValue('feature_' . $feature['id_feature'] . '_value');

                $feature_data = explode('_', $feature_value);
                if (isset($feature_data[0]) && isset($feature_data[1]))
                    $product->addFeaturesToDB($feature_data[1], $feature_data[0]);
            }
        }
        ///tags
        $tags = Tools::getValue('tasg');
        $tags = explode(',',$tags);
         $tags[]=$this->customer->firstname.' '.$this->customer->lastname;


        if (is_array($tags) && !empty($tags)) {
            //$product->tags = $this->createMultiLangField($tags);

            foreach ($product->tags as $key => $tags) {
                $is_tag_added = Tag::addTags($key, $product->id, $tags, ',');
                if (!$is_tag_added) {
                    $this->addProductWarning(Tools::safeOutput($name), $product->id, $this->l('Tags list is invalid'));
                    break;
                }
            }
        }


        $product->save();
         $product->updateCategories(array_map('intval', $product->id_category));



             }
             else {

             $this->errors[] = Tools::displayError('Pas d action update.');
             }
             }

}
else
{
$this->errors[] = Tools::displayError('Pas d action.');
}
}








}




?>

更新其他字段的任何想法! 感谢

0 个答案:

没有答案