如何使数据库条目id\u stock\u可用=id\u product PrestaShop 1.7

如何使数据库条目id\u stock\u可用=id\u product PrestaShop 1.7,prestashop,prestashop-1.7,Prestashop,Prestashop 1.7,我正在尝试使用第三方php代码将项目带到购物车。该项被输入到数据库中。在表ps\u stock\u available中,我注意到差异id\u stock\u available>id\u product中,我还注意到ps\u product表和id\u stock\u available表中的差异。它们有不同的编号和数据量 这是我的文件的代码: //Presta init global $context; if(!$context){$cont

我正在尝试使用第三方php代码将项目带到购物车。该项被输入到数据库中。在表
ps\u stock\u available
中,我注意到差异
id\u stock\u available>id\u product
中,我还注意到
ps\u product
表和
id\u stock\u available
表中的差异。它们有不同的编号和数据量

这是我的文件的代码:


        //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;
            }
            $id_cart = (int)$context->cart->id;
        }

        $doAdd="Y";

        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{
                    //Supplier data (for 1C integration)
                    $supplierName = $arCartPrice['SUPPLIER_STOCK'];
                    $supplierId = $suppliers = (Supplier::getIdByName($supplierName));
                    if (!$supplierId) {
                        $supplier = new Supplier();
                        $supplier->name = $supplierName;
                        $supplier->active = 1;
                        $supplier->add();
                        $supplierId = $supplier->id;
                    }

                    $obProduct = new Product(false,false,$id_lang);
                    $obProduct->id_category_default = $DefaultCategory;
                    $obProduct->unity = $arCartPrice['ADD_URL'];
                    $obProduct->name = $arCartPrice['ARTICLE'] .' / '.substr($arCartPrice['NAME'],0,400).' / '.$arCartPrice['BRAND'];
                    $obProduct->description = 'This product is created automatically by ';
                    $obProduct->description_short = $arCartPrice['SUPPLIER_STOCK'].' ['.$arCartPrice['DAY'].' days]';
                    $obProduct->price = $Price;
                    $obProduct->wholesale_price = $arCartPrice['PRICE'];
                    $obProduct->reference = $Reference;
                    $obProduct->link_rewrite = $arCartPrice['CPID'];
                    $obProduct->available_for_order = 1; //true
                    $obProduct->visibility = 'none';
                    $obProduct->is_virtual = 0;
                    //$obProduct->weight = round($arCartPrice['OPTIONS']['WEIGHT']/1000,2);
                    $obProduct->minimal_quantity = 1;
                    $obProduct->condition = 'used';
                    $obProduct->condition = 'refurbished';
                    $obProduct->available_now = $arCartPrice['AVAILABLE_NUM'];
                    $obProduct->quantity = $arCartPrice['AVAILABLE_NUM'];



                    //supplier
                    $obProduct->supplier_name = $supplierName;
                    $obProduct->id_supplier = $supplierId;

                    $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']);}
                StockAvailable::setQuantity($NewTID, 0, (int) $arCartPrice['AVAILABLE']);
                $obCart = new Cart($id_cart);
                $obCart->id_lang = $id_lang;
                $obCart->id_currency = $id_currency;
                if($obCart->updateQty((int)$QUANTITY=1, (int)$NewTID)){
                    Header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); die();
                }else{
                    ErAdd(" Add to Cart is false. NewTID=".$NewTID,1);
                }

//普雷斯塔初始
全球环境;
如果(!$context){$context=context::getContext();}
$logged=$context->cookie->uu get('logged');
$id\u cart=$context->cookie->\u get('id\u cart');
$id\u lang=$context->cookie->\u get('id\u lang');
$id\u guest=$context->cookie->\u get('id\u guest');
$id\u currency=$context->cookie->\u get('id\u currency');
//如果未找到购物车,则添加购物车
如果(!$id\u购物车){
$context->cart=new cart();
$context->cart->id\u customer=$context->customer->id;
$context->cart->id\u currency=$id\u currency;
$context->cart->add();
如果($context->cart->id){
$context->cookie->id\u cart=(int)$context->cart->id;
}
$id_cart=(int)$context->cart->id;
}
$doAdd=“Y”;
如果(!$id\u cart>0){$doAdd=“N”ErAdd(“您的cookie id\u cart错误了!”,1);}
如果(!$id_-lang>0){$doAdd=“N”ErAdd(“您的cookie id_-lang错了!”,1)}
如果($doAdd!=“N”){
//检查Presta中的有效tecdoc项目
$sql='选择p.`id\u product`,pl.`name`来自`.`DB\u前缀`.`product`p
左连接''.'DB'.'product''pl ON(p.'id'u product`=pl.'id'u product`)
其中pl.`id_lang`='.$id_lang.'和
p、 “价格”=“$price.”和
p、 `reference`='.$reference'
';
$arRes=Db::getInstance()->执行($sql);
如果(计数($arRes)>0){
$NewTID=$arRes[0]['id_product'];
}否则{
//供应商数据(用于集成电路)
$supplierName=$arCartPrice['SUPPLIER_STOCK'];
$supplierId=$suppliers=(Supplier::getIdByName($supplierName));
如果(!$supplierId){
$supplier=新供应商();
$supplier->name=$supplierName;
$supplier->active=1;
$supplier->add();
$supplierId=$supplier->id;
}
$obProduct=新产品(假,假,$id_lang);
$obProduct->id\u category\u default=$DefaultCategory;
$obProduct->unity=$arCartPrice['ADD_URL'];
$obProduct->name=$arCartPrice['ARTICLE']./'.substr($arCartPrice['name'],0400)。'/'.$arCartPrice['BRAND'];
$obProduct->description='此产品由''自动创建;
$obProduct->description_short=$arCartPrice['SUPPLIER_STOCK'].['.$arCartPrice['DAY'.'days].'days];
$obProduct->price=$price;
$obProduct->批发价格=$arCartPrice[‘价格’];
$obProduct->reference=$reference;
$obProduct->link_rewrite=$arCartPrice['CPID'];
$obProduct->available\u for\u order=1;//true
$obProduct->visibility='none';
$obProduct->is_virtual=0;
//$obProduct->WERGET=round($arCartPrice['OPTIONS']['WERGET']]/1000,2);
$obProduct->最小数量=1;
$obProduct->condition='used';
$obProduct->条件=‘翻新’;
$obProduct->available_now=$arCartPrice['available_NUM'];
$obProduct->quantity=$arCartPrice['AVAILABLE_NUM'];
//供应商
$obProduct->supplier\u name=$supplierName;
$obProduct->id\u供应商=$supplierId;
$obProduct->id\u tax\u rules\u group=$DefaultTaxGroup;
//回显“
”;打印($obProduct);回显“;模具(); $obProduct->add(); 如果($obProduct->id>0){ $NewTID=$obProduct->id; $obProduct->setWsCategories(数组(数组(“id”=>$DefaultCategory)); //添加图像 如果($arCartPrice['IMG_SRC']!=''和$NewTID>0){ $shops=Shop::getShops(true、null、true); $image=新图像(); $image->id\u product=$NewTID; $image->position=image::getHighestPosition($NewTID)+1; $image->cover=true;//或false; 如果($image->validateFields(false,true))==true&($image->validateFieldsLang(false,true))==true&&&$image->add()){ $image->associateTo($shops); $tmpfile=tempnam(_PS_TMP_IMG_DIR,'PS_import'); if(工具::复制($arCartPrice['IMG_SRC'],$tmpfile)){ $path=$image->getPathForCreation(); ImageManager::resize($tmpfile,$path.'.jpg'); } 取消链接($tmpfile); } } }否则{ ErAdd(“Prestashop新项目ID为false”,1); } 未结算($产品); } 如果($NewTID>0){ //如果($arCartPrice['AVAILABLE']>0){StockAvailable::setQuantity($NewTID,false,$arCartPrice['AVAILABLE']);} 可用库存::设置数量($NewTID,0,