Jump to content

Create Product in php script


CEDRICC17

Recommended Posts

Hi,

We have recently upgraded from 1.5 to 1.6, but 1 module is not working correctly. This is Sellsy Sync module To sync orders, customers and products to CRM Sellsy.

 

We have adapted to our needs , and the client could find his quote in its client area, and accept or reject it. The problem is that it is no longer possible to confirm or deny because the product does not exist . In fact, in version 1.5 , the product was automatically created where name product was quote number, so the customer could place an order for his quote. Everything works on the 1.5, the 1.6 also except the validation or rejection online .

 

The problem is that the product no longer created automatically. So we have the message " this product does not exist or is no longer in stock " impossible ordering.

 

Here are below the contents of php file. If somebody could help us solve this problem, it would be cool .

 

 

<?php

/*
* 2007-2012 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <[email protected]>
*  @copyright  2007-2012 PrestaShop SA
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/
 
/**
 * @since 1.5.0
 */
 require_once(_PS_MODULE_DIR_.'opssellsy/class/sellsyconnect_curl.php');
                    require_once(_PS_MODULE_DIR_.'opssellsy/class/sellsytools.php');
                    include(_PS_MODULE_DIR_.'opssellsy/opsutil.php');
class OpssellsyDevisModuleFrontController extends ModuleFrontController
{
public $ssl = true;
protected $id_product;
protected $id_product_attribute;
protected $id_address_delivery;
protected $customization_id;
protected $qty;
 
protected $ajax_refresh = false;
public function init()
{
parent::init();
// Get page main parameters
 
$this->id_product_attribute = 0;
$this->customization_id = 0;
$this->qty = 1;
$this->id_address_delivery = 0;
require_once($this->module->getLocalPath().'opssellsy.php');
}
public function postProcess()
{
global $cookie ;
if (Tools::isSubmit('refuse')){
$name= Tools::getvalue('accept');
$id= Tools::getvalue('id');
if (!$res = Db::getInstance()->ExecuteS("SELECT * FROM " . _DB_PREFIX_ . "ops_sellsy_devis WHERE id_devis='" . $name . "'")) {
$reqdevis = 'INSERT INTO '._DB_PREFIX_.'ops_sellsy_devis(id,id_devis, staty,devis) VALUES("","' . $name . '", 0,"'. $id .'")';
Db::getInstance()->Execute($reqdevis);
}
$request =  array( 
'method' => 'Document.updateStep', 
'params' => array (
'docid' => $id,
'document' => array(
'doctype' => 'estimate',
'step' => 'refused'
)
),
);
$response = sellsyConnect::load()->requestApi($request);
sellsyTools::showErrors();
}
if (Tools::isSubmit('accept')){
$name= Tools::getvalue('accept');
$price= Tools::getvalue('price');
$souscategorie="devis";
$id= Tools::getvalue('id');
$request =  array( 
'method' => 'Document.updateStep', 
'params' => array (
'docid' => $id,
'document' => array(
'doctype' => 'estimate',
'step' => 'accepted'
)
),
);
$response = sellsyConnect::load()->requestApi($request);
sellsyTools::showErrors();
if (!$res = Db::getInstance()->ExecuteS("SELECT * FROM " . _DB_PREFIX_ . "category_lang WHERE name='" . $souscategorie . "'")) {
                            $category_to_create1 = new Category();
                            if (!Shop::isFeatureActive())
                                $category_to_create1->id_shop_default = 1;
                            else
                                $category_to_create1->id_shop_default = (int) Context::getContext()->shop->id;
 
                            $category_to_create1->name = AdminImportController::createMultiLangField(trim($souscategorie));
                            $category_to_create1->active = 0;
                            $category_to_create1->id_parent =  Configuration::get('PS_HOME_CATEGORY');
                            $category_link_rewrite = 'categorie';
                            
 
                            if ($category_to_create1->add())
                                $a = 'true<br/>';
                            else
                                $a = "error<br/>";
                            $id_category = $category_to_create1->id;
                        }else {
                            $res = Db::getInstance()->ExecuteS("SELECT * FROM " . _DB_PREFIX_ . "category_lang WHERE name='" . $souscategorie . "'");
                            foreach ($res as $row) {
                                $id_category = $row['id_category'];
                            }
                        }
 
if (!$res = Db::getInstance()->ExecuteS("SELECT * FROM " . _DB_PREFIX_ . "ops_sellsy_devis WHERE id_devis='" . $name . "'")) {
 
                            $product_to_create = new Product();
$is_shop1=Context::getContext()->shop->id;
                            $product_to_create->id_shop_default = $is_shop1;
                           $product_to_create->shop = 1;//(int)$is_shop1;
                            $product_to_create->name = AdminImportController::createMultiLangField(trim($name));
                            $product_to_create->quantity = 1;
                            $product_to_create->quantity_discount = 1;
$product_to_create->weight = 1000;
                            $product_to_create->id_category_default = '63';
$product_to_create->price = (float) str_replace(',', '.', $price);
                            $product_to_create->wholesale_price = (float) str_replace(',', '.', $price);
                            $product_to_create->reference = 'validation-devis';
                            $product_to_create->supplier_reference = $name;
                            $product_to_create->date_add = date('Y-m-d H:i:s');
                            $product_to_create->date_upd = date('Y-m-d H:i:s');
                            $product_to_create->description = $name;
                            $product_to_create->visibility = 'none';
$product_to_create->active = true;
                            $product_to_create->description_short = 'reference';
                            $product_link_rewrite = 'o';
                            $product_to_create->link_rewrite = AdminImportController::createMultiLangField($product_link_rewrite);
                            $product_to_create->meta_description = 'reference';
                            if ($product_to_create->add()){
// $this->id_product =$product_to_create->id;
                            $sql =  "UPDATE  " . _DB_PREFIX_ . "ops_sellsy_devis SET  `id_product` =  '".(int)$product_to_create->id."' WHERE  `devis` ='".$id."'";
// Db::getInstance()->Execute("UPDATE  " . _DB_PREFIX_ . "ops_sellsy_devis SET  `id_product` =  '".(int)$product_to_create->id."' WHERE  `id_devis` ='".$name."'");
Db::getInstance()->Execute($sql);
                
//Db::getInstance()->update('ops_sellsy_devis',array('id_product' => (int)$product_to_create->id) , '`id_devis` like "'.$name.'"');
}
 
                            else
                                $a = "error<br/>";
                            Db::getInstance()->Execute("insert into " . _DB_PREFIX_ . "category_product values(" . (int) $id_category . ",'" . (int) $product_to_create->id . "','0')");                             
                            StockAvailable::setQuantity((int) $product_to_create->id, 0, $product_to_create->quantity, $product_to_create->id_shop_default);
$reqdevis = 'INSERT INTO '._DB_PREFIX_.'ops_sellsy_devis(id,id_devis, staty, id_product, devis ) VALUES("","' . $name . '", 1,'.(int)$product_to_create->id.', '.$id.' )';
$this->processChangeProductInCart((int) $product_to_create->id);
Db::getInstance()->Execute($reqdevis);
// Tools::redirect('index.php?controller=order&'.(isset($this->id_product) ? 'ipa='.$this->id_product : ''));
   }
   
                        }
 
// Db::getInstance()->Execute('truncate table '._DB_PREFIX_.'ops_sellsy_devis');
}
public function initContent()
{
parent::initContent();
if (!Context::getContext()->customer->isLogged())
Tools::redirect('index.php?controller=authentication&redirect=module&module=favoriteproducts&action=account');
 
if (Context::getContext()->customer->id){
 
 
$client=array();
$client[] = Db::getInstance()->getValue('SELECT id_sellsy from '._DB_PREFIX_.'ops_sellsy_map_cli where id_customer='.(int)$this->context->cookie->id_customer);
                        $request =  array( 
'method' => 'Document.getList', 
'params' => array ( 
'doctype' => 'estimate',
'search' => array(
'thirds' => $client,
)
),
);
 
$tab=array();
$response = sellsyConnect::load()->requestApi($request);
$i=0;
          //  print_r($response->response->result);
foreach ($response->response->result as $dlIndex => $dlDatas) {
$tab[$i]['ident']=$dlDatas->ident;
$tab[$i]['id']=$dlDatas->id;
$tab[$i]['totalAmountTaxesFree']=$dlDatas->totalAmountTaxesFree;
    $tab[$i]['status'] = $dlDatas->step;
 
$request2 = array(
'method' => 'Document.getPublicLink',
'params' => array(
'doctype' => 'estimate',
'docid' => $dlDatas->id,
)
);
 
$docPdfLink = sellsyConnect::load()->requestApi($request2);
sellsyTools::showErrors();
$tab[$i]['docPdfLink']= $docPdfLink->response;
$i++;
}
 
 
$this->context->smarty->assign('result',$tab);
    $this->context->smarty->assign('cou',count($tab));
 
$this->setTemplate('devis.tpl');
}
 
}
/**
* This process add  a product in the cart
*/
public function processChangeProductInCart($id_product)
{
$mode = 'add';
$this->id_product_attribute = 0;
$this->customization_id = 0;
$this->qty = 1;
$this->id_address_delivery = 0;
$this->id_product=$id_product;
 
$product = new Product($this->id_product, true, $this->context->language->id);
 
if ($mode == 'add')
{
 
// Add cart if no cart found
if (!$this->context->cart->id)
{
if (Context::getContext()->cookie->id_guest)
{
$guest = new Guest(Context::getContext()->cookie->id_guest);
$this->context->cart->mobile_theme = $guest->mobile_theme;
}
$this->context->cart->add();
if ($this->context->cart->id)
$this->context->cookie->id_cart = (int)$this->context->cart->id;
}
 
$cart_rules = $this->context->cart->getCartRules();
$update_quantity = $this->context->cart->updateQty($this->qty, $this->id_product, $this->id_product_attribute, $this->customization_id, Tools::getValue('op', 'up'), $this->id_address_delivery);
if ($update_quantity < 0)
{
// If product has attribute, minimal quantity is set with minimal quantity of attribute
$minimal_quantity = ($this->id_product_attribute) ? Attribute::getAttributeMinimalQty($this->id_product_attribute) : $product->minimal_quantity;
$this->errors[] = sprintf(Tools::displayError('You must add %d minimum quantity', false), $minimal_quantity);
}
elseif (!$update_quantity)
$this->errors[] = Tools::displayError('You already have the maximum quantity available for this product.', false);
elseif ((int)Tools::getValue('allow_refresh'))
{
// If the cart rules has changed, we need to refresh the whole cart
$cart_rules2 = $this->context->cart->getCartRules();
if (count($cart_rules2) != count($cart_rules))
$this->ajax_refresh = true;
else
{
$rule_list = array();
foreach ($cart_rules2 as $rule)
$rule_list[] = $rule['id_cart_rule'];
foreach ($cart_rules as $rule)
if (!in_array($rule['id_cart_rule'], $rule_list))
{
$this->ajax_refresh = true;
break;
}
}
}
 
}
 
$removed = CartRule::autoRemoveFromCart();
CartRule::autoAddToCart();
if (count($removed) && (int)Tools::getValue('allow_refresh'))
$this->ajax_refresh = true;
            Tools::redirect(__PS_BASE_URI__.'index.php?controller=order');
exit();
}
 
}

 

Link to comment
Share on other sites

do you get any error message? Maybe turn on debug mode and see if any error occurs.

http://www.prestashop.com/forums/topic/224525-how-to-turn-on-error-reporting-for-debug-information/

 

 

 

 

(and next time before you upgrade, maybe check your main modules if they are compatible already...

http://faq.sellsy.com/help/category/124/installing-sellsy-prestashop-module   )

 

 

pascal

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...