OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxbasketitem.php
Go to the documentation of this file.
1 <?php
2 
7 class oxBasketItem extends oxSuperCfg
8 {
14  protected $_sProductId = null;
15 
21  protected $_sTitle = null;
22 
28  protected $_sVarSelect = null;
29 
35  protected $_sIcon = null;
36 
42  protected $_sLink = null;
43 
49  protected $_oPrice = null;
50 
56  protected $_oUnitPrice = null;
57 
63  protected $_dAmount = 0.0;
64 
70  protected $_dWeight = 0;
71 
77  protected $_aSelList = array();
78 
84  protected $_sShopId = null;
85 
91  protected $_sNativeShopId = null;
92 
98  protected $_blSkipDiscounts = false;
99 
105  protected $_aPersistentParameters = array();
106 
112  protected $_blBundle = false;
113 
119  protected $_blIsDiscountArticle = false;
120 
126  protected $_oArticle = null;
127 
133  protected $_sDimageDirNoSsl = null;
134 
140  protected $_sDimageDirSsl = null;
141 
147  protected $_aChosenSelectlist = array();
148 
154  protected $_sWrappingId = null;
155 
161  protected $_sWishId = null;
162 
168  protected $_sWishArticleId = null;
169 
175  protected $_blCheckArticleStock = true;
176 
177 
183  protected $_iLanguageId = null;
184 
190  protected $_blSsl = null;
191 
197  protected $_sIconUrl = null;
198 
199 
205  protected $_oRegularUnitPrice = null;
206 
207 
213  public function getRegularUnitPrice()
214  {
216  }
217 
225  public function setRegularUnitPrice( $oRegularUnitPrice )
226  {
227  $this->_oRegularUnitPrice = $oRegularUnitPrice;
228  }
229 
230 
249  public function init( $sProductID, $dAmount, $aSel = null, $aPersParam = null, $blBundle = null )
250  {
251  $this->_setArticle( $sProductID );
252  $this->setAmount( $dAmount );
253  $this->_setSelectList( $aSel );
254  $this->setPersParams( $aPersParam );
255  $this->setBundle( $blBundle );
256  $this->setLanguageId( oxRegistry::getLang()->getBaseLanguage() );
257  }
258 
272  public function initFromOrderArticle( $oOrderArticle )
273  {
274  $this->_setFromOrderArticle( $oOrderArticle );
275  $this->setAmount( $oOrderArticle->oxorderarticles__oxamount->value );
276  $this->_setSelectList( $oOrderArticle->getOrderArticleSelectList() );
277  $this->setPersParams( $oOrderArticle->getPersParams() );
278  $this->setBundle( $oOrderArticle->isBundle() );
279  }
280 
288  public function setAsDiscountArticle( $blIsDiscountArticle )
289  {
290  $this->_blIsDiscountArticle = $blIsDiscountArticle;
291  }
292 
300  public function setStockCheckStatus( $blStatus )
301  {
302  $this->_blCheckArticleStock = $blStatus;
303  }
304 
310  public function getStockCheckStatus()
311  {
313  }
314 
327  public function setAmount( $dAmount, $blOverride = true, $sItemKey = null )
328  {
329  try {
330  //validating amount
331  $dAmount = oxRegistry::get("oxInputValidator")->validateBasketAmount( $dAmount );
332  } catch( oxArticleInputException $oEx ) {
333  $oEx->setArticleNr( $this->getProductId() );
334  $oEx->setProductId( $this->getProductId() );
335  // setting additional information for excp and then rethrowing
336  throw $oEx;
337  }
338 
339  $oArticle = $this->getArticle( true );
340 
341 
342  // setting default
343  $iOnStock = true;
344 
345  if ( $blOverride ) {
346  $this->_dAmount = $dAmount;
347  } else {
348  $this->_dAmount += $dAmount;
349  }
350 
351  // checking for stock
352  if ( $this->getStockCheckStatus() == true ) {
353  $dArtStockAmount = $this->getSession()->getBasket()->getArtStockInBasket( $oArticle->getId(), $sItemKey );
354  $iOnStock = $oArticle->checkForStock( $this->_dAmount, $dArtStockAmount );
355  if ( $iOnStock !== true ) {
356  if ( $iOnStock === false ) {
357  // no stock !
358  $this->_dAmount = 0;
359  } else {
360  // limited stock
361  $this->_dAmount = $iOnStock;
362  $blOverride = true;
363  }
364  }
365  }
366 
367  // calculating general weight
368  $this->_dWeight = $oArticle->oxarticles__oxweight->value * $this->_dAmount;
369 
370  if ( $iOnStock !== true ) {
371  $oEx = oxNew( 'oxOutOfStockException' );
372  $oEx->setMessage( 'ERROR_MESSAGE_OUTOFSTOCK_OUTOFSTOCK' );
373  $oEx->setArticleNr( $oArticle->oxarticles__oxartnum->value );
374  $oEx->setProductId( $oArticle->getProductId() );
375  $oEx->setRemainingAmount( $this->_dAmount );
376  $oEx->setBasketIndex( $sItemKey );
377  throw $oEx;
378  }
379  }
380 
388  public function setPrice( $oPrice )
389  {
390  $this->_oUnitPrice = clone $oPrice;
391 
392  $this->_oPrice = clone $oPrice;
393  $this->_oPrice->multiply( $this->getAmount() );
394  }
395 
401  public function getIconUrl()
402  {
403  // icon url must be (re)loaded in case icon is not set or shop was switched between ssl/nonssl
404  if ( $this->_sIconUrl === null || $this->_blSsl != $this->getConfig()->isSsl() ) {
405  $this->_sIconUrl = $this->getArticle()->getIconUrl();
406  }
407  return $this->_sIconUrl;
408  }
409 
422  public function getArticle( $blCheckProduct = false, $sProductId = null, $blDisableLazyLoading = false )
423  {
424  if ( $this->_oArticle === null || ( !$this->_oArticle->isOrderArticle() && $blDisableLazyLoading ) ) {
425  $sProductId = $sProductId ? $sProductId : $this->_sProductId;
426  if ( !$sProductId ) {
427  //this excpetion may not be caught, anyhow this is a critical exception
428  $oEx = oxNew( 'oxArticleException' );
429  $oEx->setMessage( 'EXCEPTION_ARTICLE_NOPRODUCTID' );
430  throw $oEx;
431  }
432 
433  $this->_oArticle = oxNew( 'oxarticle' );
434  // #M773 Do not use article lazy loading on order save
435  if ( $blDisableLazyLoading ) {
436  $this->_oArticle->modifyCacheKey('_allviews');
437  $this->_oArticle->disableLazyLoading();
438  }
439 
440  // performance:
441  // - skipping variants loading
442  // - skipping 'ab' price info
443  // - load parent field
444  $this->_oArticle->setNoVariantLoading( true );
445  $this->_oArticle->setLoadParentData( true );
446  if ( !$this->_oArticle->load( $sProductId ) ) {
447  $oEx = oxNew( 'oxNoArticleException' );
448  $oLang = oxRegistry::getLang();
449  $oEx->setMessage( sprintf($oLang->translateString( 'ERROR_MESSAGE_ARTICLE_ARTICLE_DOES_NOT_EXIST', $oLang->getBaseLanguage() ), $sProductId) );
450  $oEx->setArticleNr( $sProductId );
451  $oEx->setProductId( $sProductId );
452  throw $oEx;
453  }
454 
455  // cant put not visible product to basket (M:1286)
456  if ( $blCheckProduct && !$this->_oArticle->isVisible() ) {
457  $oEx = oxNew( 'oxNoArticleException' );
458  $oLang = oxRegistry::getLang();
459  $oEx->setMessage( sprintf($oLang->translateString( 'ERROR_MESSAGE_ARTICLE_ARTICLE_DOES_NOT_EXIST', $oLang->getBaseLanguage() ), $this->_oArticle->oxarticles__oxartnum->value) );
460  $oEx->setArticleNr( $sProductId );
461  $oEx->setProductId( $sProductId );
462  throw $oEx;
463  }
464 
465  // cant put not buyable product to basket
466  if ( $blCheckProduct && !$this->_oArticle->isBuyable() ) {
467  $oEx = oxNew( 'oxArticleInputException' );
468  $oEx->setMessage( 'ERROR_MESSAGE_ARTICLE_ARTICLE_NOT_BUYABLE' );
469  $oEx->setArticleNr( $sProductId );
470  $oEx->setProductId( $sProductId );
471  throw $oEx;
472  }
473  }
474 
475  return $this->_oArticle;
476  }
477 
483  public function getdBundledAmount()
484  {
485  return $this->isBundle()?$this->_dAmount:0;
486  }
487 
493  public function getPrice()
494  {
495  return $this->_oPrice;
496  }
497 
503  public function getUnitPrice()
504  {
505  return $this->_oUnitPrice;
506  }
507 
513  public function getAmount()
514  {
515  return $this->_dAmount;
516  }
517 
523  public function getWeight()
524  {
525  return $this->_dWeight;
526  }
527 
533  public function getTitle()
534  {
535  if ( $this->_sTitle === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage() ) {
536 
537  $oArticle = $this->getArticle( );
538  $this->_sTitle = $oArticle->oxarticles__oxtitle->value;
539 
540  if ( $oArticle->oxarticles__oxvarselect->value ) {
541  $this->_sTitle = $this->_sTitle. ', ' . $this->getVarSelect();
542  }
543  }
544 
545  return $this->_sTitle;
546  }
547 
553  public function getLink()
554  {
555  if ( $this->_sLink === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage() ) {
556  $this->_sLink = oxRegistry::get("oxUtilsUrl")->cleanUrl( $this->getArticle()->getLink(), array( 'force_sid' ) );
557  }
558 
559  return $this->getSession()->processUrl( $this->_sLink );
560  }
561 
567  public function getShopId()
568  {
569  return $this->_sShopId;
570  }
571 
577  public function getSelList()
578  {
579  return $this->_aSelList;
580  }
581 
587  public function getChosenSelList()
588  {
590  }
591 
597  public function isBundle()
598  {
599  return $this->_blBundle;
600  }
601 
607  public function isDiscountArticle()
608  {
610  }
611 
617  public function isSkipDiscount()
618  {
620  }
621 
631  public function __get( $sName )
632  {
633  if ( $sName == 'oProduct' ) {
634  return $this->getArticle();
635  }
636  }
637 
643  public function __sleep()
644  {
645  $aRet = array();
646  foreach ( get_object_vars( $this ) as $sKey => $sVar ) {
647  if ( $sKey != '_oArticle' ) {
648  $aRet[] = $sKey;
649  }
650  }
651  return $aRet;
652  }
653 
671  protected function _setArticle( $sProductId )
672  {
673  $oConfig = $this->getConfig();
674  $oArticle = $this->getArticle( true, $sProductId );
675 
676  // product ID
677  $this->_sProductId = $sProductId;
678 
679  $this->_sTitle = null;
680  $this->_sVarSelect = null;
681  $this->getTitle();
682 
683  // icon and details URL's
684  $this->_sIcon = $oArticle->oxarticles__oxicon->value;
685  $this->_sIconUrl = $oArticle->getIconUrl();
686  $this->_blSsl = $oConfig->isSsl();
687 
688  // removing force_sid from the link (in case it'll change)
689  $this->_sLink = oxRegistry::get("oxUtilsUrl")->cleanUrl( $oArticle->getLink(), array( 'force_sid' ) );
690 
691  // shop Ids
692  $this->_sShopId = $oConfig->getShopId();
693  $this->_sNativeShopId = $oArticle->oxarticles__oxshopid->value;
694 
695  // SSL/NON SSL image paths
696  $this->_sDimageDirNoSsl = $oArticle->nossl_dimagedir;
697  $this->_sDimageDirSsl = $oArticle->ssl_dimagedir;
698  }
699 
711  protected function _setFromOrderArticle( $oOrderArticle )
712  {
713  // overriding whole article
714  $this->_oArticle = $oOrderArticle;
715 
716  // product ID
717  $this->_sProductId = $oOrderArticle->getProductId();
718 
719  // products title
720  $this->_sTitle = $oOrderArticle->oxarticles__oxtitle->value;
721 
722  // shop Ids
723  $this->_sShopId = $this->getConfig()->getShopId();
724  $this->_sNativeShopId = $oOrderArticle->oxarticles__oxshopid->value;
725  }
726 
734  protected function _setSelectList( $aSelList )
735  {
736  // checking for default select list
737  $aSelectLists = $this->getArticle()->getSelectLists();
738  if ( !$aSelList || is_array($aSelList) && count($aSelList) == 0 ) {
739  if ( $iSelCnt = count( $aSelectLists ) ) {
740  $aSelList = array_fill( 0, $iSelCnt, '0' );
741  }
742  }
743 
744  $this->_aSelList = $aSelList;
745 
746  //
747  if ( count( $this->_aSelList ) && is_array($this->_aSelList) ) {
748  foreach ( $this->_aSelList as $conkey => $iSel ) {
749  $this->_aChosenSelectlist[$conkey] = new stdClass();
750  $this->_aChosenSelectlist[$conkey]->name = $aSelectLists[$conkey]['name'];
751  $this->_aChosenSelectlist[$conkey]->value = $aSelectLists[$conkey][$iSel]->name;
752  }
753  }
754  }
755 
761  public function getPersParams()
762  {
764  }
765 
773  public function setPersParams( $aPersParam )
774  {
775  $this->_aPersistentParameters = $aPersParam;
776  }
777 
785  public function setBundle( $blBundle )
786  {
787  $this->_blBundle = $blBundle;
788  }
789 
797  public function setSkipDiscounts( $blSkip )
798  {
799  $this->_blSkipDiscounts = $blSkip;
800  }
801 
807  public function getProductId()
808  {
809  return $this->_sProductId;
810  }
811 
819  public function setWrapping( $sWrapId )
820  {
821  $this->_sWrappingId = $sWrapId;
822  }
823 
829  public function getWrappingId()
830  {
831  return $this->_sWrappingId;
832  }
833 
839  public function getWrapping()
840  {
841  $oWrap = null;
842  if ( $sWrapId = $this->getWrappingId() ) {
843  $oWrap = oxNew( 'oxwrapping' );
844  $oWrap->load( $sWrapId );
845  }
846  return $oWrap;
847  }
848 
854  public function getWishId()
855  {
856  return $this->_sWishId;
857  }
858 
866  public function setWishId( $sWishId )
867  {
868  $this->_sWishId = $sWishId;
869  }
870 
878  public function setWishArticleId( $sArticleId )
879  {
880  $this->_sWishArticleId = $sArticleId;
881  }
882 
888  public function getWishArticleId()
889  {
890  return $this->_sWishArticleId;
891  }
892 
900  public function getFRegularUnitPrice()
901  {
902  return oxRegistry::getLang()->formatCurrency( $this->getRegularUnitPrice()->getPrice() );
903  }
904 
912  public function getFUnitPrice()
913  {
914  return oxRegistry::getLang()->formatCurrency( $this->getUnitPrice()->getPrice() );
915  }
916 
924  public function getFTotalPrice()
925  {
926  return oxRegistry::getLang()->formatCurrency( $this->getPrice()->getPrice() );
927  }
928 
934  public function getVatPercent()
935  {
936  return oxRegistry::getLang()->formatVat( $this->getPrice()->getVat() );
937  }
938 
944  public function getVarSelect()
945  {
946  if ( $this->_sVarSelect === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage() ) {
947  $oArticle = $this->getArticle( );
948  $this->_sVarSelect = $oArticle->oxarticles__oxvarselect->value ? $oArticle->oxarticles__oxvarselect->value : '';
949  }
950 
951  return $this->_sVarSelect;
952  }
953 
959  public function getLanguageId()
960  {
961  return $this->_iLanguageId;
962  }
963 
971  public function setLanguageId( $iLanguageId )
972  {
973  $iOldLang = $this->_iLanguageId;
974  $this->_iLanguageId = $iLanguageId;
975 
976  // #0003777: reload content on language change
977  if ($iOldLang !== null && $iOldLang != $iLanguageId) {
978  try {
979  $this->_setArticle($this->getProductId());
980  } catch (oxNoArticleException $oEx) {
981  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
982  } catch (oxArticleInputException $oEx) {
983  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
984  }
985  }
986  }
987 }