Jump to content

Modify code to Access cookie in prestashop 1.4


djrooney

Recommended Posts

Hello. i have problem with my module. I use tecdoc module for prestashop but i have problem with cart. Integration code work perfect with prestashop 1.5 and 1.6 beacuse this work with if(!$context){$context = Context::getContext();}

 

the problem is that I can not modify the code to prestashop 1.4 where do not use this function

 

This is my code., please help to modify this for work cart with prestashop 1.4 (presta 1.5 and 1.6 work pefect)

In my presta when i try to add to card i receive withe screen and error log : PHP Fatal error:  Class 'Context' not found in /home/autoal/public_html/autoparts/tocms/PrestaShop.php on line 19

 

<?if(!defined("TDM_PROLOG_INCLUDED") || TDM_PROLOG_INCLUDED!==true)die();


require($_SERVER["DOCUMENT_ROOT"].'/config/config.inc.php');


//Add to cart
if(defined('TDM_ADD_TO_CART') AND TDM_ADD_TO_CART){
    global $arCartPrice;
    if(is_array($arCartPrice)){
        if($arCartPrice['OPTIONS']['MINIMUM']>1){$QUANTITY=$arCartPrice['OPTIONS']['MINIMUM'];}else{$QUANTITY=1;}
        $DefaultCategory = 2;
        $DefaultTaxGroup = 0;
        $Price = $arCartPrice['PRICE_CONVERTED'];
        $Reference = $arCartPrice['ARTICLE'].' / '.$arCartPrice['BRAND'];
        
        //Presta init
        global $context;
        if(!$context){$context = Context::getContext();}
        $logged = $context->cookie->__get('logged');
        $id_cart = $context->cookie->__get('id_cart');
        $id_lang = $context->cookie->__get('id_lang');
        $id_guest = $context->cookie->__get('id_guest');
        $id_currency = $context->cookie->__get('id_currency');
        
        // Add cart if no cart found
        if (!$id_cart){
            $context->cart = new Cart();
            $context->cart->id_customer = $context->customer->id;
            $context->cart->id_currency = $id_currency;
            $context->cart->add();
            if($context->cart->id){
                $context->cookie->id_cart = (int)$context->cart->id;
            }

        }
        $doAdd="Y";
        //if(!$logged>0){$doAdd="N"; $TCore->arErrorMessages[] = 'You must be <a href="/authentication.php?back=my-account.php">logged in</a> to buy products';}
        //if(trim($Reference)=='' OR !$Price>0 OR !$QUANTITY>0){$doAdd="N"; $TCore->arErrorMessages[] = 'Add to cart data is missing!';}
        if(!$id_cart>0){$doAdd="N"; ErAdd("Your cookie <b>id_cart</b> is wrong!",1);}
        if(!$id_lang>0){$doAdd="N"; ErAdd("Your cookie <b>id_lang</b> is wrong!",1);}
        if($doAdd!="N"){
            //Check avail. tecdoc item in Presta
            $sql = 'SELECT p.`id_product`, pl.`name` FROM `'._DB_PREFIX_.'product` p
                    LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product`)
                    WHERE pl.`id_lang` = '.$id_lang.' AND
                    p.`price` = '.$Price.' AND
                    p.`reference` = "'.$Reference.'"
                    ';
            $arRes = Db::getInstance()->executeS($sql);
            if(count($arRes)>0){
                $NewTID = $arRes[0]['id_product'];
            }else{
                    $obProduct = new Product(false,false,$id_lang);
                    $obProduct->id_category_default = $DefaultCategory;
                    $obProduct->unity = $arCartPrice['ADD_URL'];
                    $obProduct->name = $arCartPrice['NAME'];
                    $obProduct->description = 'This product is created automatically by TecDoc Module. http://tecdoc-module.com';
                    $obProduct->description_short = $arCartPrice['SUPPLIER_STOCK'].' ['.$arCartPrice['DAY'].' days]';
                    $obProduct->price = $Price;
                    $obProduct->reference = $Reference;
                    $obProduct->link_rewrite = $arCartPrice['CPID'];
                    $obProduct->available_for_order = 1; //true
                    $obProduct->visibility = 'none';
                    $obProduct->is_virtual = 0;
                    if($arCartPrice['OPTIONS']['WEIGHT']>0){$obProduct->weight = round($arCartPrice['OPTIONS']['WEIGHT']/1000,2);}
                    if($arCartPrice['OPTIONS']['MINIMUM']>0){$obProduct->minimal_quantity = $arCartPrice['OPTIONS']['MINIMUM']; $QUANTITY=$arCartPrice['OPTIONS']['MINIMUM'];}
                    if($arCartPrice['OPTIONS']['USED']>0){$obProduct->condition = 'used';}
                    if($arCartPrice['OPTIONS']['RESTORED']>0){$obProduct->condition = 'refurbished';}
                    
                    $obProduct->id_tax_rules_group = $DefaultTaxGroup;
                    //echo '<br><pre>';print_r($obProduct);echo '</pre>';die();
                    $obProduct->add();
                    if($obProduct->id>0){
                        $NewTID = $obProduct->id;
                        $obProduct->setWsCategories(Array(Array("id"=>$DefaultCategory)));
                        //Add image
                        if($arCartPrice['IMG_SRC']!='' AND $NewTID>0){
                            $shops = Shop::getShops(true, null, true);    
                            $image = new Image();
                            $image->id_product = $NewTID;
                            $image->position = Image::getHighestPosition($NewTID)+1;
                            $image->cover = true; // or false;
                            if(($image->validateFields(false, true)) === true && ($image->validateFieldsLang(false, true)) === true && $image->add()){
                                $image->associateTo($shops);
                                $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
                                if(Tools::copy($arCartPrice['IMG_SRC'], $tmpfile)){
                                    $path = $image->getPathForCreation();
                                    ImageManager::resize($tmpfile, $path.'.jpg');
                                }
                                unlink($tmpfile);
                            }
                        }
                    }else{
                        ErAdd("Prestashop new Item ID is false",1);
                    }
                    unset($obProduct);
            }
                
            if($NewTID>0){
                if($arCartPrice['AVAILABLE']>0){StockAvailable::setQuantity($NewTID, false, $arCartPrice['AVAILABLE']);}
                $obCart = new Cart($id_cart);
                $obCart->id_lang = $id_lang;
                $obCart->id_currency = $id_currency;
                if($obCart->updateQty($QUANTITY,$NewTID)){

                }else{
                    ErAdd("Prestashop Add to Cart is false",1);
                }
            }
        }
        
    }
}


require($_SERVER["DOCUMENT_ROOT"]."/header.php");


echo $TDMContent;

require($_SERVER["DOCUMENT_ROOT"]."/footer.php");
?>

Link to comment
Share on other sites

  • 2 years later...

hi,

i have the same tecdoc-module with prestashop 1.6 

i have the following error when i try to add a product to the cart with languages other than default one:

 

Notice: Undefined index: MINIMUM in /var/www/autoparts/tocms/PrestaShop.1.6.x.php on line 58

Fatal error: Uncaught exception 'PrestaShopException' with message 'Property Product->link_rewrite is empty' in /var/www/classes/ObjectModel.php:954 Stack trace: #0 /var/www/classes/ObjectModel.php(295): ObjectModelCore->validateFieldsLang() #1 /var/www/classes/ObjectModel.php(511): ObjectModelCore->getFieldsLang() #2 /var/www/classes/Product.php(530): ObjectModelCore->add(true, false) #3 /var/www/autoparts/tocms/PrestaShop.1.6.x.php(141): ProductCore->add() #4 /var/www/autoparts/index.php(0): unknown() #5 {main} thrown in /var/www/classes/ObjectModel.php on line 954

 

could you tell me if you have the same problem? 

 

thanks

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...