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->_sIconUrl = $this->getArticle()->getIconUrl();
00393         }
00394         return $this->_sIconUrl;
00395     }
00396 
00409     public function getArticle( $blCheckProduct = true, $sProductId = null, $blDisableLazyLoading = false )
00410     {
00411         if ( $this->_oArticle === null || ( !$this->_oArticle->isOrderArticle() && $blDisableLazyLoading ) ) {
00412             $sProductId = $sProductId ? $sProductId : $this->_sProductId;
00413             if ( !$sProductId ) {
00414                 //this excpetion may not be caught, anyhow this is a critical exception
00415                 $oEx = oxNew( 'oxArticleException' );
00416                 $oEx->setMessage( 'EXCEPTION_ARTICLE_NOPRODUCTID' );
00417                 throw $oEx;
00418             }
00419 
00420             $this->_oArticle = oxNew( 'oxarticle' );
00421             // #M773 Do not use article lazy loading on order save
00422             if ( $blDisableLazyLoading ) {
00423                 $this->_oArticle->modifyCacheKey('_allviews');
00424                 $this->_oArticle->disableLazyLoading();
00425             }
00426 
00427             // performance:
00428             // - skipping variants loading
00429             // - skipping 'ab' price info
00430             // - load parent field
00431             $this->_oArticle->setNoVariantLoading( true );
00432             $this->_oArticle->setSkipAbPrice( true );
00433             $this->_oArticle->setLoadParentData( true );
00434             if ( !$this->_oArticle->load( $sProductId ) ) {
00435                 $oEx = oxNew( 'oxNoArticleException' );
00436                 $oLang = oxLang::getInstance();
00437                 $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_ARTICLE_ARTICELDOESNOTEXIST', $oLang->getBaseLanguage() ), $sProductId) );
00438                 $oEx->setArticleNr( $sProductId );
00439                 $oEx->setProductId( $sProductId );
00440                 throw $oEx;
00441             }
00442 
00443             // cant put not visible product to basket (M:1286)
00444             if ( $blCheckProduct && !$this->_oArticle->isVisible() ) {
00445                 $oEx = oxNew( 'oxNoArticleException' );
00446                 $oLang = oxLang::getInstance();
00447                 $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_ARTICLE_ARTICELDOESNOTEXIST', $oLang->getBaseLanguage() ), $this->_oArticle->oxarticles__oxartnum->value) );
00448                 $oEx->setArticleNr( $sProductId );
00449                 $oEx->setProductId( $sProductId );
00450                 throw $oEx;
00451             }
00452 
00453             // cant put not buyable product to basket
00454             if ( $blCheckProduct && !$this->_oArticle->isBuyable() ) {
00455                 $oEx = oxNew( 'oxArticleInputException' );
00456                 $oEx->setMessage( 'EXCEPTION_ARTICLE_ARTICELNOTBUYABLE' );
00457                 $oEx->setArticleNr( $sProductId );
00458                 $oEx->setProductId( $sProductId );
00459                 throw $oEx;
00460             }
00461         }
00462 
00463         return $this->_oArticle;
00464     }
00465 
00471     public function getdBundledAmount()
00472     {
00473         return $this->isBundle()?$this->_dAmount:0;
00474     }
00475 
00481     public function getPrice()
00482     {
00483         return $this->_oPrice;
00484     }
00485 
00491     public function getUnitPrice()
00492     {
00493         return $this->_oUnitPrice;
00494     }
00495 
00501     public function getAmount()
00502     {
00503         return $this->_dAmount;
00504     }
00505 
00511     public function getWeight()
00512     {
00513         return $this->_dWeight;
00514     }
00515 
00521     public function getTitle()
00522     {
00523         if ( $this->_sTitle === null || $this->getLanguageId() != oxLang::getInstance()->getBaseLanguage() ) {
00524 
00525             $oArticle = $this->getArticle( );
00526             $this->_sTitle = $oArticle->oxarticles__oxtitle->value;
00527 
00528             if ( $oArticle->oxarticles__oxvarselect->value ) {
00529                 $this->_sTitle = $this->_sTitle. ', ' . $this->getVarSelect();
00530             }
00531         }
00532 
00533         return $this->_sTitle;
00534     }
00535 
00541     public function getIcon()
00542     {
00543         return $this->_sIcon;
00544     }
00545 
00551     public function getLink()
00552     {
00553         if ( $this->_sLink === null || $this->getLanguageId() != oxLang::getInstance()->getBaseLanguage() ) {
00554             $this->_sLink = oxUtilsUrl::getInstance()->cleanUrl( $this->getArticle()->getLink(), array( 'force_sid' ) );
00555         }
00556 
00557         return $this->getSession()->processUrl( $this->_sLink );
00558     }
00559 
00565     public function getShopId()
00566     {
00567         return $this->_sShopId;
00568     }
00569 
00575     public function getSelList()
00576     {
00577         return $this->_aSelList;
00578     }
00579 
00585     public function getChosenSelList()
00586     {
00587         return $this->_aChosenSelectlist;
00588     }
00589 
00595     public function isBundle()
00596     {
00597         return $this->_blBundle;
00598     }
00599 
00605     public function isDiscountArticle()
00606     {
00607         return $this->_blIsDiscountArticle;
00608     }
00609 
00615     public function isSkipDiscount()
00616     {
00617         return $this->_blSkipDiscounts;
00618     }
00619 
00629     public function __get( $sName )
00630     {
00631         if ( $sName == 'oProduct' ) {
00632             return $this->getArticle();
00633         }
00634     }
00635 
00641     public function __sleep()
00642     {
00643         $aRet = array();
00644         foreach ( get_object_vars( $this ) as $sKey => $sVar ) {
00645             if ( $sKey != '_oArticle' ) {
00646                 $aRet[] = $sKey;
00647             }
00648         }
00649         return $aRet;
00650     }
00651 
00669     protected function _setArticle( $sProductId )
00670     {
00671         $oConfig = $this->getConfig();
00672         $oArticle = $this->getArticle( true, $sProductId );
00673 
00674         // product ID
00675         $this->_sProductId = $sProductId;
00676 
00677         $this->_sTitle = null;
00678         $this->_sVarSelect = null;
00679         $this->getTitle();
00680 
00681         // icon and details URL's
00682         $this->_sIcon    = $oArticle->oxarticles__oxicon->value;
00683         $this->_sIconUrl = $oArticle->getIconUrl();
00684         $this->_blSsl    = $oConfig->isSsl();
00685 
00686         // removing force_sid from the link (incase it'll change)
00687         $this->_sLink    = oxUtilsUrl::getInstance()->cleanUrl( $oArticle->getLink(), array( 'force_sid' ) );
00688 
00689         // shop Ids
00690         $this->_sShopId       = $oConfig->getShopId();
00691         $this->_sNativeShopId = $oArticle->oxarticles__oxshopid->value;
00692 
00693         // SSL/NON SSL image paths
00694         $this->_sDimageDirNoSsl = $oArticle->nossl_dimagedir;
00695         $this->_sDimageDirSsl   = $oArticle->ssl_dimagedir;
00696     }
00697 
00709     protected function _setFromOrderArticle( $oOrderArticle )
00710     {
00711         // overriding whole article
00712         $this->_oArticle = $oOrderArticle;
00713 
00714         // product ID
00715         $this->_sProductId = $oOrderArticle->getProductId();
00716 
00717         // products title
00718         $this->_sTitle = $oOrderArticle->oxarticles__oxtitle->value;
00719 
00720         // shop Ids
00721         $this->_sShopId       = $this->getConfig()->getShopId();
00722         $this->_sNativeShopId = $oOrderArticle->oxarticles__oxshopid->value;
00723     }
00724 
00732     protected function _setSelectList( $aSelList )
00733     {
00734         // checking for default select list
00735         $aSelectLists = $this->getArticle()->getSelectLists();
00736         if ( !$aSelList || is_array($aSelList) && count($aSelList) == 0 ) {
00737             if ( $iSelCnt = count( $aSelectLists ) ) {
00738                 $aSelList = array_fill( 0, $iSelCnt, '0' );
00739             }
00740         }
00741 
00742         $this->_aSelList = $aSelList;
00743 
00744         //
00745         if ( count( $this->_aSelList ) && is_array($this->_aSelList) ) {
00746             foreach ( $this->_aSelList as $conkey => $iSel ) {
00747                 $this->_aChosenSelectlist[$conkey] = new Oxstdclass();
00748                 $this->_aChosenSelectlist[$conkey]->name  = $aSelectLists[$conkey]['name'];
00749                 $this->_aChosenSelectlist[$conkey]->value = $aSelectLists[$conkey][$iSel]->name;
00750             }
00751         }
00752     }
00753 
00759     public function getPersParams()
00760     {
00761         return $this->_aPersistentParameters;
00762     }
00763 
00771     public function setPersParams( $aPersParam )
00772     {
00773         $this->_aPersistentParameters = $aPersParam;
00774     }
00775 
00783     public function setBundle( $blBundle )
00784     {
00785         $this->_blBundle = $blBundle;
00786     }
00787 
00795     public function setSkipDiscounts( $blSkip )
00796     {
00797         $this->_blSkipDiscounts = $blSkip;
00798     }
00799 
00805     public function getProductId()
00806     {
00807         return $this->_sProductId;
00808     }
00809 
00817     public function setWrapping( $sWrapId )
00818     {
00819         $this->_sWrappingId = $sWrapId;
00820     }
00821 
00827     public function getWrappingId()
00828     {
00829         return $this->_sWrappingId;
00830     }
00831 
00837     public function getWrapping()
00838     {
00839         $oWrap = null;
00840         if ( $sWrapId = $this->getWrappingId() ) {
00841             $oWrap = oxNew( 'oxwrapping' );
00842             $oWrap->load( $sWrapId );
00843         }
00844         return $oWrap;
00845     }
00846 
00852     public function getWishId()
00853     {
00854         return $this->_sWishId;
00855     }
00856 
00864     public function setWishId( $sWishId )
00865     {
00866         $this->_sWishId = $sWishId;
00867     }
00868 
00876     public function setWishArticleId( $sArticleId )
00877     {
00878         $this->_sWishArticleId = $sArticleId;
00879     }
00880 
00886     public function getWishArticleId()
00887     {
00888         return $this->_sWishArticleId;
00889     }
00890 
00896     public function getFUnitPrice()
00897     {
00898         return oxLang::getInstance()->formatCurrency( $this->getUnitPrice()->getBruttoPrice() );
00899     }
00900 
00906     public function getFTotalPrice()
00907     {
00908         return oxLang::getInstance()->formatCurrency( $this->getPrice()->getBruttoPrice() );
00909     }
00910 
00916     public function getVatPercent()
00917     {
00918         return oxLang::getInstance()->formatVat( $this->getPrice()->getVat() );
00919     }
00920 
00926     public function getVarSelect()
00927     {
00928         if ( $this->_sVarSelect === null || $this->getLanguageId() != oxLang::getInstance()->getBaseLanguage() ) {
00929             $oArticle = $this->getArticle( );
00930             $this->_sVarSelect = $oArticle->oxarticles__oxvarselect->value ? $oArticle->oxarticles__oxvarselect->value : '';
00931         }
00932 
00933         return $this->_sVarSelect;
00934     }
00935 
00941     public function getLanguageId()
00942     {
00943         return $this->_iLanguageId;
00944     }
00945 
00953     public function setLanguageId( $iLanguageId )
00954     {
00955         $iOldLang = $this->_iLanguageId;
00956         $this->_iLanguageId = $iLanguageId;
00957 
00958         // #0003777: reload content on language change
00959         if ($iOldLang !== null && $iOldLang != $iLanguageId) {
00960             $this->_setArticle($this->getProductId());
00961         }
00962     }
00963 }