如何制作数据库条目id_stock_available = id_product PrestaShop 1.7

问题描述 投票:0回答:1

我正在尝试使用第三方php代码将商品带到购物车。该项目已输入数据库。在表ps_stock_available中,注意到了id_stock_available >id_product的差异,我也注意到了ps_product表和id_stock_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);
                }

如何执行id_stock_available = id_product

prestashop prestashop-1.7
1个回答
0
投票

您能更具体一点吗?其实我不明白您到底是什么问题。尽可能分析地描述您的方案。

到目前为止,据我所了解,您拥有/使用第三方库。您尝试制作一个脚本,将客户添加到购物车中的物品“喂入”该库,对吗?

您需要他们的id_product,id_product_attribute吗?数量已添加到购物车?库存中存在的数量?

让我理解为您解决问题提供帮助。

© www.soinside.com 2019 - 2024. All rights reserved.