oxbasketitem.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxBasketItem extends oxSuperCfg
00007 {
00013     protected $_sProductId = null;
00014 
00020     protected $_sTitle = null;
00021 
00027     protected $_sVarSelect = null;
00028 
00034     protected $_sIcon = null;
00035 
00041     protected $_sLink = null;
00042 
00048     protected $_oPrice = null;
00049 
00055     protected $_oUnitPrice = null;
00056 
00062     protected $_dAmount = 0.0;
00063 
00069     protected $_dWeight = 0;
00070 
00076     protected $_aSelList = array();
00077 
00083     protected $_sShopId = null;
00084 
00090     protected $_sNativeShopId = null;
00091 
00097     protected $_blSkipDiscounts = false;
00098 
00104     protected $_aPersistentParameters = array();
00105 
00111     protected $_blBundle = false;
00112 
00118     protected $_blIsDiscountArticle = false;
00119 
00125     protected $_oArticle = null;
00126 
00132     protected $_sDimageDirNoSsl = null;
00133 
00139     protected $_sDimageDirSsl = null;
00140 
00146     protected $_aChosenSelectlist = array();
00147 
00153     protected $_sWrappingId = null;
00154 
00160      protected $_sWishId = null;
00161 
00167     protected $_sWishArticleId = null;
00168 
00174     protected $_blCheckArticleStock = true;
00175 
00176 
00182     protected $_iLanguageId = null;
00183 
00189     protected $_blSsl = null;
00190 
00196     protected $_sIconUrl = null;
00197 
00216     public function init( $sProductID, $dAmount, $aSel = null, $aPersParam = null, $blBundle = null )
00217     {
00218         $this->_setArticle( $sProductID );
00219         $this->setAmount( $dAmount );
00220         $this->_setSelectList( $aSel );
00221         $this->setPersParams( $aPersParam );
00222         $this->setBundle( $blBundle );
00223         $this->setLanguageId( oxLang::getInstance()->getBaseLanguage() );
00224     }
00225 
00239     public function initFromOrderArticle( $oOrderArticle )
00240     {
00241         $this->_setFromOrderArticle( $oOrderArticle );
00242         $this->setAmount( $oOrderArticle->oxorderarticles__oxamount->value );
00243         $this->_setSelectList( $oOrderArticle->getOrderArticleSelectList() );
00244         $this->setPersParams( $oOrderArticle->getPersParams() );
00245         $this->setBundle( $oOrderArticle->isBundle() );
00246     }
00247 
00255     public function setAsDiscountArticle( $blIsDiscountArticle )
00256     {
00257         $this->_blIsDiscountArticle = $blIsDiscountArticle;
00258     }
00259 
00267     public function setStockCheckStatus( $blStatus )
00268     {
00269         $this->_blCheckArticleStock = $blStatus;
00270     }
00271 
00277     public function getStockCheckStatus()
00278     {
00279         return $this->_blCheckArticleStock;
00280     }
00281 
00294     public function setAmount( $dAmount, $blOverride = true, $sItemKey = null )
00295     {
00296         try {
00297             //validating amount
00298             $dAmount = oxInputValidator::getInstance()->validateBasketAmount( $dAmount );
00299         } catch( oxArticleInputException $oEx ) {
00300             $oEx->setArticleNr( $this->getProductId() );
00301             $oEx->setProductId( $this->getProductId() );
00302             // setting additional information for excp and then rethrowing
00303             throw $oEx;
00304         }
00305 
00306         $oArticle = $this->getArticle();
00307 
00308 
00309         // setting default
00310         $iOnStock = true;
00311 
00312         if ( $blOverride ) {
00313             $this->_dAmount  = $dAmount;
00314         } else {
00315             $this->_dAmount += $dAmount;
00316         }
00317 
00318         // checking for stock
00319         if ( $this->getStockCheckStatus() == true ) {
00320             $dArtStockAmount = $this->getSession()->getBasket()->getArtStockInBasket( $oArticle->getId(), $sItemKey );
00321             $iOnStock = $oArticle->checkForStock( $this->_dAmount, $dArtStockAmount );
00322             if ( $iOnStock !== true ) {
00323                 if ( $iOnStock === false ) {
00324                     // no stock !
00325                     $this->_dAmount = 0;
00326                 } else {
00327                     // limited stock
00328                     $this->_dAmount = $iOnStock;
00329                     $blOverride = true;
00330                 }
00331             }
00332         }
00333 
00334         // calculating general weight
00335         $this->_dWeight = $oArticle->oxarticles__oxweight->value * $this->_dAmount;
00336 
00337         if ( $iOnStock !== true ) {
00338             $oEx = oxNew( 'oxOutOfStockException' );
00339             $oEx->setMessage( 'EXCEPTION_OUTOFSTOCK_OUTOFSTOCK' );
00340             $oEx->setArticleNr( $oArticle->oxarticles__oxartnum->value );
00341             $oEx->setProductId( $oArticle->getProductId() );
00342             $oEx->setRemainingAmount( $this->_dAmount );
00343             throw $oEx;
00344         }
00345     }
00346 
00354     public function setPrice( $oPrice )
00355     {
00356         $this->_oPrice = oxNew( 'oxprice' );
00357         $this->_oPrice->setBruttoPriceMode();
00358         $this->_oPrice->setVat( $oPrice->getVAT() );
00359         $this->_oPrice->addPrice( $oPrice );
00360         $this->_oPrice->multiply( $this->getAmount() );
00361 
00362         $this->_oUnitPrice = oxNew( 'oxprice' );
00363         $this->_oUnitPrice->setBruttoPriceMode();
00364         $this->_oUnitPrice->setVat( $oPrice->getVAT() );
00365         $this->_oUnitPrice->addPrice( $oPrice );
00366     }
00367 
00373     public function getImageUrl()
00374     {
00375         $blIsSSl = $this->getConfig()->isSsl();
00376         if ( $blIsSSl ) {
00377             return $this->_sDimageDirSsl;
00378         } else {
00379             return $this->_sDimageDirNoSsl;
00380         }
00381     }
00382 
00388     public function getIconUrl()
00389     {
00390         // icon url must be (re)loaded in case icon is not set or shop was switched between ssl/nonssl
00391         if ( $this->_sIconUrl === null || $this->_blSsl != $this->getConfig()->isSsl() ) {
00392             $this->setLanguageId( oxLang::getInstance()->getBaseLanguage() );
00393             $this->_sIconUrl = $this->getArticle()->getIconUrl();
00394         }
00395         return $this->_sIconUrl;
00396     }
00397 
00410     public function getArticle( $blCheckProduct = true, $sProductId = null, $blDisableLazyLoading = false )
00411     {
00412         if ( $this->_oArticle === null || ( !$this->_oArticle->isOrderArticle() && $blDisableLazyLoading ) ) {
00413             $sProductId = $sProductId ? $sProductId : $this->_sProductId;
00414             if ( !$sProductId ) {
00415                 //this excpetion may not be caught, anyhow this is a critical exception
00416                 $oEx = oxNew( 'oxArticleException' );
00417                 $oEx->setMessage( 'EXCEPTION_ARTICLE_NOPRODUCTID' );
00418                 throw $oEx;
00419             }
00420 
00421             $this->_oArticle = oxNew( 'oxarticle' );
00422             // #M773 Do not use article lazy loading on order save
00423             if ( $blDisableLazyLoading ) {
00424                 $this->_oArticle->modifyCacheKey('_allviews');
00425                 $this->_oArticle->disableLazyLoading();
00426             }
00427 
00428             // performance:
00429             // - skipping variants loading
00430             // - skipping 'ab' price info
00431             // - load parent field
00432             $this->_oArticle->setNoVariantLoading( true );
00433             $this->_oArticle->setSkipAbPrice( true );
00434             $this->_oArticle->setLoadParentData( true );
00435             if ( !$this->_oArticle->load( $sProductId ) ) {
00436                 $oEx = oxNew( 'oxNoArticleException' );
00437                 $oLang = oxLang::getInstance();
00438                 $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_ARTICLE_ARTICELDOESNOTEXIST', $oLang->getBaseLanguage() ), $sProductId) );
00439                 $oEx->setArticleNr( $sProductId );
00440                 $oEx->setProductId( $sProductId );
00441                 throw $oEx;
00442             }
00443 
00444             // cant put not visible product to basket (M:1286)
00445             if ( $blCheckProduct && !$this->_oArticle->isVisible() ) {
00446                 $oEx = oxNew( 'oxNoArticleException' );
00447                 $oLang = oxLang::getInstance();
00448                 $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_ARTICLE_ARTICELDOESNOTEXIST', $oLang->getBaseLanguage() ), $this->_oArticle->oxarticles__oxartnum->value) );
00449                 $oEx->setArticleNr( $sProductId );
00450                 $oEx->setProductId( $sProductId );
00451                 throw $oEx;
00452             }
00453 
00454             // cant put not buyable product to basket
00455             if ( $blCheckProduct && !$this->_oArticle->isBuyable() ) {
00456                 $oEx = oxNew( 'oxArticleInputException' );
00457                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
00458                 $oEx->setArticleNr( $sProductId );
00459                 $oEx->setProductId( $sProductId );
00460                 throw $oEx;
00461             }
00462         }
00463 
00464         return $this->_oArticle;
00465     }
00466 
00472     public function getdBundledAmount()
00473     {
00474         return $this->isBundle()?$this->_dAmount:0;
00475     }
00476 
00482     public function getPrice()
00483     {
00484         return $this->_oPrice;
00485     }
00486 
00492     public function getUnitPrice()
00493     {
00494         return $this->_oUnitPrice;
00495     }
00496 
00502     public function getAmount()
00503     {
00504         return $this->_dAmount;
00505     }
00506 
00512     public function getWeight()
00513     {
00514         return $this->_dWeight;
00515     }
00516 
00522     public function getTitle()
00523     {
00524         if ( $this->_sTitle === null || $this->getLanguageId() != oxLang::getInstance()->getBaseLanguage() ) {
00525 
00526             $this->setLanguageId( oxLang::getInstance()->getBaseLanguage() );
00527 
00528             $oArticle = $this->getArticle( );
00529             $this->_sTitle = $oArticle->oxarticles__oxtitle->value;
00530 
00531             if ( $oArticle->oxarticles__oxvarselect->value ) {
00532                 $this->_sTitle = $this->_sTitle. ', ' . $this->getVarSelect();
00533             }
00534         }
00535 
00536         return $this->_sTitle;
00537     }
00538 
00544     public function getIcon()
00545     {
00546         return $this->_sIcon;
00547     }
00548 
00554     public function getLink()
00555     {
00556         return $this->getSession()->processUrl( $this->_sLink );
00557     }
00558 
00564     public function getShopId()
00565     {
00566         return $this->_sShopId;
00567     }
00568 
00574     public function getSelList()
00575     {
00576         return $this->_aSelList;
00577     }
00578 
00584     public function getChosenSelList()
00585     {
00586         return $this->_aChosenSelectlist;
00587     }
00588 
00594     public function isBundle()
00595     {
00596         return $this->_blBundle;
00597     }
00598 
00604     public function isDiscountArticle()
00605     {
00606         return $this->_blIsDiscountArticle;
00607     }
00608 
00614     public function isSkipDiscount()
00615     {
00616         return $this->_blSkipDiscounts;
00617     }
00618 
00628     public function __get( $sName )
00629     {
00630         if ( $sName == 'oProduct' ) {
00631             return $this->getArticle();
00632         }
00633     }
00634 
00640     public function __sleep()
00641     {
00642         $aRet = array();
00643         foreach ( get_object_vars( $this ) as $sKey => $sVar ) {
00644             if ( $sKey != '_oArticle' ) {
00645                 $aRet[] = $sKey;
00646             }
00647         }
00648         return $aRet;
00649     }
00650 
00668     protected function _setArticle( $sProductId )
00669     {
00670         $oConfig = $this->getConfig();
00671         $oArticle = $this->getArticle( true, $sProductId );
00672 
00673         // product ID
00674         $this->_sProductId = $sProductId;
00675 
00676         $this->getTitle();
00677 
00678         // icon and details URL's
00679         $this->_sIcon    = $oArticle->oxarticles__oxicon->value;
00680         $this->_sIconUrl = $oArticle->getIconUrl();
00681         $this->_sLink    = $oArticle->getLink();
00682         $this->_blSsl    = $oConfig->isSsl();
00683 
00684         // removing force_sid from the link (incase it'll change)
00685         $oUrlUtils = oxUtilsUrl::getInstance();
00686         $this->_sLink    = $oUrlUtils->cleanUrl( $oArticle->getLink(), array( 'force_sid' ) );
00687 
00688         // shop Ids
00689         $this->_sShopId       = $oConfig->getShopId();
00690         $this->_sNativeShopId = $oArticle->oxarticles__oxshopid->value;
00691 
00692         // SSL/NON SSL image paths
00693         $this->_sDimageDirNoSsl = $oArticle->nossl_dimagedir;
00694         $this->_sDimageDirSsl   = $oArticle->ssl_dimagedir;
00695     }
00696 
00708     protected function _setFromOrderArticle( $oOrderArticle )
00709     {
00710         // overriding whole article
00711         $this->_oArticle = $oOrderArticle;
00712 
00713         // product ID
00714         $this->_sProductId = $oOrderArticle->getProductId();
00715 
00716         // products title
00717         $this->_sTitle = $oOrderArticle->oxarticles__oxtitle->value;
00718 
00719         // shop Ids
00720         $this->_sShopId       = $this->getConfig()->getShopId();
00721         $this->_sNativeShopId = $oOrderArticle->oxarticles__oxshopid->value;
00722     }
00723 
00731     protected function _setSelectList( $aSelList )
00732     {
00733         // checking for default select list
00734         $aSelectLists = $this->getArticle()->getSelectLists();
00735         if ( !$aSelList || is_array($aSelList) && count($aSelList) == 0 ) {
00736             if ( $iSelCnt = count( $aSelectLists ) ) {
00737                 $aSelList = array_fill( 0, $iSelCnt, '0' );
00738             }
00739         }
00740 
00741         $this->_aSelList = $aSelList;
00742 
00743         //
00744         if ( count( $this->_aSelList ) && is_array($this->_aSelList) ) {
00745             foreach ( $this->_aSelList as $conkey => $iSel ) {
00746                 $this->_aChosenSelectlist[$conkey] = new Oxstdclass();
00747                 $this->_aChosenSelectlist[$conkey]->name  = $aSelectLists[$conkey]['name'];
00748                 $this->_aChosenSelectlist[$conkey]->value = $aSelectLists[$conkey][$iSel]->name;
00749             }
00750         }
00751     }
00752 
00758     public function getPersParams()
00759     {
00760         return $this->_aPersistentParameters;
00761     }
00762 
00770     public function setPersParams( $aPersParam )
00771     {
00772         $this->_aPersistentParameters = $aPersParam;
00773     }
00774 
00782     public function setBundle( $blBundle )
00783     {
00784         $this->_blBundle = $blBundle;
00785     }
00786 
00794     public function setSkipDiscounts( $blSkip )
00795     {
00796         $this->_blSkipDiscounts = $blSkip;
00797     }
00798 
00804     public function getProductId()
00805     {
00806         return $this->_sProductId;
00807     }
00808 
00816     public function setWrapping( $sWrapId )
00817     {
00818         $this->_sWrappingId = $sWrapId;
00819     }
00820 
00826     public function getWrappingId()
00827     {
00828         return $this->_sWrappingId;
00829     }
00830 
00836     public function getWrapping()
00837     {
00838         $oWrap = null;
00839         if ( $sWrapId = $this->getWrappingId() ) {
00840             $oWrap = oxNew( 'oxwrapping' );
00841             $oWrap->load( $sWrapId );
00842         }
00843         return $oWrap;
00844     }
00845 
00851     public function getWishId()
00852     {
00853         return $this->_sWishId;
00854     }
00855 
00863     public function setWishId( $sWishId )
00864     {
00865         $this->_sWishId = $sWishId;
00866     }
00867 
00875     public function setWishArticleId( $sArticleId )
00876     {
00877         $this->_sWishArticleId = $sArticleId;
00878     }
00879 
00885     public function getWishArticleId()
00886     {
00887         return $this->_sWishArticleId;
00888     }
00889 
00895     public function getFUnitPrice()
00896     {
00897         return oxLang::getInstance()->formatCurrency( $this->getUnitPrice()->getBruttoPrice() );
00898     }
00899 
00905     public function getFTotalPrice()
00906     {
00907         return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice() );
00908     }
00909 
00915     public function getVatPercent()
00916     {
00917         return oxLang::getInstance()->formatVat( $this->getPrice()->getVat() );
00918     }
00919 
00925     public function getVarSelect()
00926     {
00927         if ( $this->_sVarSelect === null || $this->getLanguageId() != oxLang::getInstance()->getBaseLanguage() ) {
00928             $oArticle = $this->getArticle( );
00929             $this->_sVarSelect = $oArticle->oxarticles__oxvarselect->value ? $oArticle->oxarticles__oxvarselect->value : '';
00930         }
00931 
00932         return $this->_sVarSelect;
00933     }
00934 
00940     public function getLanguageId()
00941     {
00942         return $this->_iLanguageId;
00943     }
00944 
00952     public function setLanguageId( $iLanguageId )
00953     {
00954         $this->_iLanguageId = $iLanguageId;
00955     }
00956 }