oxbasketitem.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxBasketItem extends oxSuperCfg
00008 {
00009 
00015     protected $_sProductId = null;
00016 
00022     protected $_sTitle = null;
00023 
00029     protected $_sVarSelect = null;
00030 
00036     protected $_sIcon = null;
00037 
00043     protected $_sLink = null;
00044 
00050     protected $_oPrice = null;
00051 
00057     protected $_oUnitPrice = null;
00058 
00064     protected $_dAmount = 0.0;
00065 
00071     protected $_dWeight = 0;
00072 
00078     protected $_aSelList = array();
00079 
00085     protected $_sShopId = null;
00086 
00092     protected $_sNativeShopId = null;
00093 
00099     protected $_blSkipDiscounts = false;
00100 
00106     protected $_aPersistentParameters = array();
00107 
00113     protected $_blBundle = false;
00114 
00120     protected $_blIsDiscountArticle = false;
00121 
00127     protected $_oArticle = null;
00128 
00134     protected $_sDimageDirNoSsl = null;
00135 
00141     protected $_sDimageDirSsl = null;
00142 
00148     protected $_aChosenSelectlist = array();
00149 
00155     protected $_sWrappingId = null;
00156 
00162     protected $_sWishId = null;
00163 
00169     protected $_sWishArticleId = null;
00170 
00176     protected $_blCheckArticleStock = true;
00177 
00178 
00184     protected $_iLanguageId = null;
00185 
00191     protected $_blSsl = null;
00192 
00198     protected $_sIconUrl = null;
00199 
00200 
00206     protected $_oRegularUnitPrice = null;
00207 
00208 
00214     public function getRegularUnitPrice()
00215     {
00216         return $this->_oRegularUnitPrice;
00217     }
00218 
00224     public function setRegularUnitPrice($oRegularUnitPrice)
00225     {
00226         $this->_oRegularUnitPrice = $oRegularUnitPrice;
00227     }
00228 
00229 
00246     public function init($sProductID, $dAmount, $aSel = null, $aPersParam = null, $blBundle = null)
00247     {
00248         $this->_setArticle($sProductID);
00249         $this->setAmount($dAmount);
00250         $this->_setSelectList($aSel);
00251         $this->setPersParams($aPersParam);
00252         $this->setBundle($blBundle);
00253         $this->setLanguageId(oxRegistry::getLang()->getBaseLanguage());
00254     }
00255 
00267     public function initFromOrderArticle($oOrderArticle)
00268     {
00269         $this->_setFromOrderArticle($oOrderArticle);
00270         $this->setAmount($oOrderArticle->oxorderarticles__oxamount->value);
00271         $this->_setSelectList($oOrderArticle->getOrderArticleSelectList());
00272         $this->setPersParams($oOrderArticle->getPersParams());
00273         $this->setBundle($oOrderArticle->isBundle());
00274     }
00275 
00281     public function setAsDiscountArticle($blIsDiscountArticle)
00282     {
00283         $this->_blIsDiscountArticle = $blIsDiscountArticle;
00284     }
00285 
00291     public function setStockCheckStatus($blStatus)
00292     {
00293         $this->_blCheckArticleStock = $blStatus;
00294     }
00295 
00301     public function getStockCheckStatus()
00302     {
00303         return $this->_blCheckArticleStock;
00304     }
00305 
00317     public function setAmount($dAmount, $blOverride = true, $sItemKey = null)
00318     {
00319         try {
00320             //validating amount
00321             $dAmount = oxRegistry::get("oxInputValidator")->validateBasketAmount($dAmount);
00322         } catch (oxArticleInputException $oEx) {
00323             $oEx->setArticleNr($this->getProductId());
00324             $oEx->setProductId($this->getProductId());
00325             // setting additional information for exception and then rethrowing
00326             throw $oEx;
00327         }
00328 
00329         $oArticle = $this->getArticle(true);
00330 
00331 
00332         // setting default
00333         $iOnStock = true;
00334 
00335         if ($blOverride) {
00336             $this->_dAmount = $dAmount;
00337         } else {
00338             $this->_dAmount += $dAmount;
00339         }
00340 
00341         // checking for stock
00342         if ($this->getStockCheckStatus() == true) {
00343             $dArtStockAmount = $this->getSession()->getBasket()->getArtStockInBasket($oArticle->getId(), $sItemKey);
00344             $iOnStock = $oArticle->checkForStock($this->_dAmount, $dArtStockAmount);
00345             if ($iOnStock !== true) {
00346                 if ($iOnStock === false) {
00347                     // no stock !
00348                     $this->_dAmount = 0;
00349                 } else {
00350                     // limited stock
00351                     $this->_dAmount = $iOnStock;
00352                     $blOverride = true;
00353                 }
00354             }
00355         }
00356 
00357         // calculating general weight
00358         $this->_dWeight = $oArticle->oxarticles__oxweight->value * $this->_dAmount;
00359 
00360         if ($iOnStock !== true) {
00362             $oEx = oxNew('oxOutOfStockException');
00363             $oEx->setMessage('ERROR_MESSAGE_OUTOFSTOCK_OUTOFSTOCK');
00364             $oEx->setArticleNr($oArticle->oxarticles__oxartnum->value);
00365             $oEx->setProductId($oArticle->getProductId());
00366             $oEx->setRemainingAmount($this->_dAmount);
00367             $oEx->setBasketIndex($sItemKey);
00368             throw $oEx;
00369         }
00370     }
00371 
00377     public function setPrice($oPrice)
00378     {
00379         $this->_oUnitPrice = clone $oPrice;
00380 
00381         $this->_oPrice = clone $oPrice;
00382         $this->_oPrice->multiply($this->getAmount());
00383     }
00384 
00390     public function getIconUrl()
00391     {
00392         // icon url must be (re)loaded in case icon is not set or shop was switched between ssl/nonssl
00393         if ($this->_sIconUrl === null || $this->_blSsl != $this->getConfig()->isSsl()) {
00394             $this->_sIconUrl = $this->getArticle()->getIconUrl();
00395         }
00396 
00397         return $this->_sIconUrl;
00398     }
00399 
00412     public function getArticle($blCheckProduct = false, $sProductId = null, $blDisableLazyLoading = false)
00413     {
00414         if ($this->_oArticle === null || (!$this->_oArticle->isOrderArticle() && $blDisableLazyLoading)) {
00415             $sProductId = $sProductId ? $sProductId : $this->_sProductId;
00416             if (!$sProductId) {
00417                 //this exception may not be caught, anyhow this is a critical exception
00419                 $oEx = oxNew('oxArticleException');
00420                 $oEx->setMessage('EXCEPTION_ARTICLE_NOPRODUCTID');
00421                 throw $oEx;
00422             }
00423 
00424             $this->_oArticle = oxNew('oxarticle');
00425             // #M773 Do not use article lazy loading on order save
00426             if ($blDisableLazyLoading) {
00427                 $this->_oArticle->modifyCacheKey('_allviews');
00428                 $this->_oArticle->disableLazyLoading();
00429             }
00430 
00431             // performance:
00432             // - skipping variants loading
00433             // - skipping 'ab' price info
00434             // - load parent field
00435             $this->_oArticle->setNoVariantLoading(true);
00436             $this->_oArticle->setLoadParentData(true);
00437             if (!$this->_oArticle->load($sProductId)) {
00439                 $oEx = oxNew('oxNoArticleException');
00440                 $oLang = oxRegistry::getLang();
00441                 $oEx->setMessage(sprintf($oLang->translateString('ERROR_MESSAGE_ARTICLE_ARTICLE_DOES_NOT_EXIST', $oLang->getBaseLanguage()), $sProductId));
00442                 $oEx->setArticleNr($sProductId);
00443                 $oEx->setProductId($sProductId);
00444                 throw $oEx;
00445             }
00446 
00447             // cant put not visible product to basket (M:1286)
00448             if ($blCheckProduct && !$this->_oArticle->isVisible()) {
00450                 $oEx = oxNew('oxNoArticleException');
00451                 $oLang = oxRegistry::getLang();
00452                 $oEx->setMessage(sprintf($oLang->translateString('ERROR_MESSAGE_ARTICLE_ARTICLE_DOES_NOT_EXIST', $oLang->getBaseLanguage()), $this->_oArticle->oxarticles__oxartnum->value));
00453                 $oEx->setArticleNr($sProductId);
00454                 $oEx->setProductId($sProductId);
00455                 throw $oEx;
00456             }
00457 
00458             // cant put not buyable product to basket
00459             if ($blCheckProduct && !$this->_oArticle->isBuyable()) {
00461                 $oEx = oxNew('oxArticleInputException');
00462                 $oEx->setMessage('ERROR_MESSAGE_ARTICLE_ARTICLE_NOT_BUYABLE');
00463                 $oEx->setArticleNr($sProductId);
00464                 $oEx->setProductId($sProductId);
00465                 throw $oEx;
00466             }
00467         }
00468 
00469         return $this->_oArticle;
00470     }
00471 
00477     public function getdBundledAmount()
00478     {
00479         return $this->isBundle() ? $this->_dAmount : 0;
00480     }
00481 
00487     public function getPrice()
00488     {
00489         return $this->_oPrice;
00490     }
00491 
00497     public function getUnitPrice()
00498     {
00499         return $this->_oUnitPrice;
00500     }
00501 
00507     public function getAmount()
00508     {
00509         return $this->_dAmount;
00510     }
00511 
00517     public function getWeight()
00518     {
00519         return $this->_dWeight;
00520     }
00521 
00527     public function getTitle()
00528     {
00529         if ($this->_sTitle === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
00530 
00531             $oArticle = $this->getArticle();
00532             $this->_sTitle = $oArticle->oxarticles__oxtitle->value;
00533 
00534             if ($oArticle->oxarticles__oxvarselect->value) {
00535                 $this->_sTitle = $this->_sTitle . ', ' . $this->getVarSelect();
00536             }
00537         }
00538 
00539         return $this->_sTitle;
00540     }
00541 
00547     public function getLink()
00548     {
00549         if ($this->_sLink === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
00550             $this->_sLink = oxRegistry::get("oxUtilsUrl")->cleanUrl($this->getArticle()->getLink(), array('force_sid'));
00551         }
00552 
00553         return $this->getSession()->processUrl($this->_sLink);
00554     }
00555 
00561     public function getShopId()
00562     {
00563         return $this->_sShopId;
00564     }
00565 
00571     public function getSelList()
00572     {
00573         return $this->_aSelList;
00574     }
00575 
00581     public function getChosenSelList()
00582     {
00583         return $this->_aChosenSelectlist;
00584     }
00585 
00591     public function isBundle()
00592     {
00593         return $this->_blBundle;
00594     }
00595 
00601     public function isDiscountArticle()
00602     {
00603         return $this->_blIsDiscountArticle;
00604     }
00605 
00611     public function isSkipDiscount()
00612     {
00613         return $this->_blSkipDiscounts;
00614     }
00615 
00625     public function __get($sName)
00626     {
00627         if ($sName == 'oProduct') {
00628             return $this->getArticle();
00629         }
00630     }
00631 
00637     public function __sleep()
00638     {
00639         $aRet = array();
00640         foreach (get_object_vars($this) as $sKey => $sVar) {
00641             if ($sKey != '_oArticle') {
00642                 $aRet[] = $sKey;
00643             }
00644         }
00645 
00646         return $aRet;
00647     }
00648 
00664     protected function _setArticle($sProductId)
00665     {
00666         $oConfig = $this->getConfig();
00667         $oArticle = $this->getArticle(true, $sProductId);
00668 
00669         // product ID
00670         $this->_sProductId = $sProductId;
00671 
00672         $this->_sTitle = null;
00673         $this->_sVarSelect = null;
00674         $this->getTitle();
00675 
00676         // icon and details URL's
00677         $this->_sIcon = $oArticle->oxarticles__oxicon->value;
00678         $this->_sIconUrl = $oArticle->getIconUrl();
00679         $this->_blSsl = $oConfig->isSsl();
00680 
00681         // removing force_sid from the link (in case it'll change)
00682         $this->_sLink = oxRegistry::get("oxUtilsUrl")->cleanUrl($oArticle->getLink(), array('force_sid'));
00683 
00684         // shop Ids
00685         $this->_sShopId = $oConfig->getShopId();
00686         $this->_sNativeShopId = $oArticle->oxarticles__oxshopid->value;
00687 
00688         // SSL/NON SSL image paths
00689         $this->_sDimageDirNoSsl = $oArticle->nossl_dimagedir;
00690         $this->_sDimageDirSsl = $oArticle->ssl_dimagedir;
00691     }
00692 
00702     protected function _setFromOrderArticle($oOrderArticle)
00703     {
00704         // overriding whole article
00705         $this->_oArticle = $oOrderArticle;
00706 
00707         // product ID
00708         $this->_sProductId = $oOrderArticle->getProductId();
00709 
00710         // products title
00711         $this->_sTitle = $oOrderArticle->oxarticles__oxtitle->value;
00712 
00713         // shop Ids
00714         $this->_sShopId = $this->getConfig()->getShopId();
00715         $this->_sNativeShopId = $oOrderArticle->oxarticles__oxshopid->value;
00716     }
00717 
00723     protected function _setSelectList($aSelList)
00724     {
00725         // checking for default select list
00726         $aSelectLists = $this->getArticle()->getSelectLists();
00727         if (!$aSelList || is_array($aSelList) && count($aSelList) == 0) {
00728             if ($iSelCnt = count($aSelectLists)) {
00729                 $aSelList = array_fill(0, $iSelCnt, '0');
00730             }
00731         }
00732 
00733         $this->_aSelList = $aSelList;
00734 
00735         //
00736         if (count($this->_aSelList) && is_array($this->_aSelList)) {
00737             foreach ($this->_aSelList as $conkey => $iSel) {
00738                 $this->_aChosenSelectlist[$conkey] = new stdClass();
00739                 $this->_aChosenSelectlist[$conkey]->name = $aSelectLists[$conkey]['name'];
00740                 $this->_aChosenSelectlist[$conkey]->value = $aSelectLists[$conkey][$iSel]->name;
00741             }
00742         }
00743     }
00744 
00750     public function getPersParams()
00751     {
00752         return $this->_aPersistentParameters;
00753     }
00754 
00760     public function setPersParams($aPersParam)
00761     {
00762         $this->_aPersistentParameters = $aPersParam;
00763     }
00764 
00770     public function setBundle($blBundle)
00771     {
00772         $this->_blBundle = $blBundle;
00773     }
00774 
00780     public function setSkipDiscounts($blSkip)
00781     {
00782         $this->_blSkipDiscounts = $blSkip;
00783     }
00784 
00790     public function getProductId()
00791     {
00792         return $this->_sProductId;
00793     }
00794 
00800     public function setWrapping($sWrapId)
00801     {
00802         $this->_sWrappingId = $sWrapId;
00803     }
00804 
00810     public function getWrappingId()
00811     {
00812         return $this->_sWrappingId;
00813     }
00814 
00820     public function getWrapping()
00821     {
00822         $oWrap = null;
00823         if ($sWrapId = $this->getWrappingId()) {
00824             $oWrap = oxNew('oxwrapping');
00825             $oWrap->load($sWrapId);
00826         }
00827 
00828         return $oWrap;
00829     }
00830 
00836     public function getWishId()
00837     {
00838         return $this->_sWishId;
00839     }
00840 
00846     public function setWishId($sWishId)
00847     {
00848         $this->_sWishId = $sWishId;
00849     }
00850 
00856     public function setWishArticleId($sArticleId)
00857     {
00858         $this->_sWishArticleId = $sArticleId;
00859     }
00860 
00866     public function getWishArticleId()
00867     {
00868         return $this->_sWishArticleId;
00869     }
00870 
00878     public function getFRegularUnitPrice()
00879     {
00880         return oxRegistry::getLang()->formatCurrency($this->getRegularUnitPrice()->getPrice());
00881     }
00882 
00890     public function getFUnitPrice()
00891     {
00892         return oxRegistry::getLang()->formatCurrency($this->getUnitPrice()->getPrice());
00893     }
00894 
00902     public function getFTotalPrice()
00903     {
00904         return oxRegistry::getLang()->formatCurrency($this->getPrice()->getPrice());
00905     }
00906 
00912     public function getVatPercent()
00913     {
00914         return oxRegistry::getLang()->formatVat($this->getPrice()->getVat());
00915     }
00916 
00922     public function getVarSelect()
00923     {
00924         if ($this->_sVarSelect === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
00925             $oArticle = $this->getArticle();
00926             $this->_sVarSelect = $oArticle->oxarticles__oxvarselect->value ? $oArticle->oxarticles__oxvarselect->value : '';
00927         }
00928 
00929         return $this->_sVarSelect;
00930     }
00931 
00937     public function getLanguageId()
00938     {
00939         return $this->_iLanguageId;
00940     }
00941 
00947     public function setLanguageId($iLanguageId)
00948     {
00949         $iOldLang = $this->_iLanguageId;
00950         $this->_iLanguageId = $iLanguageId;
00951 
00952         // #0003777: reload content on language change
00953         if ($iOldLang !== null && $iOldLang != $iLanguageId) {
00954             $this->_setArticle($this->getProductId());
00955         }
00956     }
00957 }