OXID eShop CE  4.9.11
 All Classes Files Functions Variables Pages
oxbasketitem.php
Go to the documentation of this file.
1 <?php
2 
7 class oxBasketItem extends oxSuperCfg
8 {
9 
15  protected $_sProductId = null;
16 
22  protected $_sTitle = null;
23 
29  protected $_sVarSelect = null;
30 
36  protected $_sIcon = null;
37 
43  protected $_sLink = null;
44 
50  protected $_oPrice = null;
51 
57  protected $_oUnitPrice = null;
58 
64  protected $_dAmount = 0.0;
65 
71  protected $_dWeight = 0;
72 
78  protected $_aSelList = array();
79 
85  protected $_sShopId = null;
86 
92  protected $_sNativeShopId = null;
93 
99  protected $_blSkipDiscounts = false;
100 
106  protected $_aPersistentParameters = array();
107 
113  protected $_blBundle = false;
114 
120  protected $_blIsDiscountArticle = false;
121 
127  protected $_oArticle = null;
128 
134  protected $_sDimageDirNoSsl = null;
135 
141  protected $_sDimageDirSsl = null;
142 
148  protected $_aChosenSelectlist = array();
149 
155  protected $_sWrappingId = null;
156 
162  protected $_sWishId = null;
163 
169  protected $_sWishArticleId = null;
170 
176  protected $_blCheckArticleStock = true;
177 
178 
184  protected $_iLanguageId = null;
185 
191  protected $_blSsl = null;
192 
198  protected $_sIconUrl = null;
199 
200 
206  protected $_oRegularUnitPrice = null;
207 
213  protected $basketItemKey = null;
214 
220  public function getBasketItemKey()
221  {
222  return $this->basketItemKey;
223  }
224 
230  public function setBasketItemKey($itemKey)
231  {
232  $this->basketItemKey = $itemKey;
233  }
234 
240  public function getRegularUnitPrice()
241  {
243  }
244 
250  public function setRegularUnitPrice($oRegularUnitPrice)
251  {
252  $this->_oRegularUnitPrice = $oRegularUnitPrice;
253  }
254 
255 
272  public function init($sProductID, $dAmount, $aSel = null, $aPersParam = null, $blBundle = null)
273  {
274  $this->_setArticle($sProductID);
275  $this->setAmount($dAmount);
276  $this->_setSelectList($aSel);
277  $this->setPersParams($aPersParam);
278  $this->setBundle($blBundle);
279  $this->setLanguageId(oxRegistry::getLang()->getBaseLanguage());
280  }
281 
293  public function initFromOrderArticle($oOrderArticle)
294  {
295  $this->_setFromOrderArticle($oOrderArticle);
296  $this->setAmount($oOrderArticle->oxorderarticles__oxamount->value);
297  $this->_setSelectList($oOrderArticle->getOrderArticleSelectList());
298  $this->setPersParams($oOrderArticle->getPersParams());
299  $this->setBundle($oOrderArticle->isBundle());
300  }
301 
307  public function setAsDiscountArticle($blIsDiscountArticle)
308  {
309  $this->_blIsDiscountArticle = $blIsDiscountArticle;
310  }
311 
317  public function setStockCheckStatus($blStatus)
318  {
319  $this->_blCheckArticleStock = $blStatus;
320  }
321 
327  public function getStockCheckStatus()
328  {
330  }
331 
343  public function setAmount($dAmount, $blOverride = true, $sItemKey = null)
344  {
345  try {
346  //validating amount
347  $dAmount = oxRegistry::get("oxInputValidator")->validateBasketAmount($dAmount);
348  } catch (oxArticleInputException $oEx) {
349  $oEx->setArticleNr($this->getProductId());
350  $oEx->setProductId($this->getProductId());
351  // setting additional information for exception and then rethrowing
352  throw $oEx;
353  }
354 
355  $oArticle = $this->getArticle(true);
356 
357 
358  // setting default
359  $iOnStock = true;
360 
361  if ($blOverride) {
362  $this->_dAmount = $dAmount;
363  } else {
364  $this->_dAmount += $dAmount;
365  }
366 
367  // checking for stock
368  if ($this->getStockCheckStatus() == true) {
369  $dArtStockAmount = $this->getSession()->getBasket()->getArtStockInBasket($oArticle->getId(), $sItemKey);
370  $iOnStock = $oArticle->checkForStock($this->_dAmount, $dArtStockAmount);
371  if ($iOnStock !== true) {
372  if ($iOnStock === false) {
373  // no stock !
374  $this->_dAmount = 0;
375  } else {
376  // limited stock
377  $this->_dAmount = $iOnStock;
378  $blOverride = true;
379  }
380  }
381  }
382 
383  // calculating general weight
384  $this->_dWeight = $oArticle->oxarticles__oxweight->value * $this->_dAmount;
385 
386  if ($iOnStock !== true) {
388  $oEx = oxNew('oxOutOfStockException');
389  $oEx->setMessage('ERROR_MESSAGE_OUTOFSTOCK_OUTOFSTOCK');
390  $oEx->setArticleNr($oArticle->oxarticles__oxartnum->value);
391  $oEx->setProductId($oArticle->getProductId());
392  $oEx->setRemainingAmount($this->_dAmount);
393  $oEx->setBasketIndex($sItemKey);
394  throw $oEx;
395  }
396  }
397 
403  public function setPrice($oPrice)
404  {
405  $this->_oUnitPrice = clone $oPrice;
406 
407  $this->_oPrice = clone $oPrice;
408  $this->_oPrice->multiply($this->getAmount());
409  }
410 
416  public function getIconUrl()
417  {
418  // icon url must be (re)loaded in case icon is not set or shop was switched between ssl/nonssl
419  if ($this->_sIconUrl === null || $this->_blSsl != $this->getConfig()->isSsl()) {
420  $this->_sIconUrl = $this->getArticle()->getIconUrl();
421  }
422 
423  return $this->_sIconUrl;
424  }
425 
440  public function getArticle($blCheckProduct = false, $sProductId = null, $blDisableLazyLoading = false)
441  {
442  if ($this->_oArticle === null || (!$this->_oArticle->isOrderArticle() && $blDisableLazyLoading)) {
443  $sProductId = $sProductId ? $sProductId : $this->_sProductId;
444  if (!$sProductId) {
445  //this exception may not be caught, anyhow this is a critical exception
447  $oEx = oxNew('oxArticleException');
448  $oEx->setMessage('EXCEPTION_ARTICLE_NOPRODUCTID');
449  throw $oEx;
450  }
451 
452  $this->_oArticle = oxNew('oxarticle');
453  // #M773 Do not use article lazy loading on order save
454  if ($blDisableLazyLoading) {
455  $this->_oArticle->modifyCacheKey('_allviews');
456  $this->_oArticle->disableLazyLoading();
457  }
458 
459  // performance:
460  // - skipping variants loading
461  // - skipping 'ab' price info
462  // - load parent field
463  $this->_oArticle->setNoVariantLoading(true);
464  $this->_oArticle->setLoadParentData(true);
465  if (!$this->_oArticle->load($sProductId)) {
467  $oEx = oxNew('oxNoArticleException');
468  $oLang = oxRegistry::getLang();
469  $oEx->setMessage(sprintf($oLang->translateString('ERROR_MESSAGE_ARTICLE_ARTICLE_DOES_NOT_EXIST', $oLang->getBaseLanguage()), $sProductId));
470  $oEx->setArticleNr($sProductId);
471  $oEx->setProductId($sProductId);
472  throw $oEx;
473  }
474 
475  // cant put not visible product to basket (M:1286)
476  if ($blCheckProduct && !$this->_oArticle->isVisible()) {
478  $oEx = oxNew('oxNoArticleException');
479  $oLang = oxRegistry::getLang();
480  $oEx->setMessage(sprintf($oLang->translateString('ERROR_MESSAGE_ARTICLE_ARTICLE_DOES_NOT_EXIST', $oLang->getBaseLanguage()), $this->_oArticle->oxarticles__oxartnum->value));
481  $oEx->setArticleNr($sProductId);
482  $oEx->setProductId($sProductId);
483  throw $oEx;
484  }
485 
486  // cant put not buyable product to basket
487  if ($blCheckProduct && !$this->_oArticle->isBuyable()) {
489  $oEx = oxNew('oxArticleInputException');
490  $oEx->setMessage('ERROR_MESSAGE_ARTICLE_ARTICLE_NOT_BUYABLE');
491  $oEx->setArticleNr($sProductId);
492  $oEx->setProductId($sProductId);
493  throw $oEx;
494  }
495  }
496 
497  return $this->_oArticle;
498  }
499 
505  public function getdBundledAmount()
506  {
507  return $this->isBundle() ? $this->_dAmount : 0;
508  }
509 
515  public function getPrice()
516  {
517  return $this->_oPrice;
518  }
519 
525  public function getUnitPrice()
526  {
527  return $this->_oUnitPrice;
528  }
529 
535  public function getAmount()
536  {
537  return $this->_dAmount;
538  }
539 
545  public function getWeight()
546  {
547  return $this->_dWeight;
548  }
549 
555  public function getTitle()
556  {
557  if ($this->_sTitle === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
558 
559  $oArticle = $this->getArticle();
560  $this->_sTitle = $oArticle->oxarticles__oxtitle->value;
561 
562  if ($oArticle->oxarticles__oxvarselect->value) {
563  $this->_sTitle = $this->_sTitle . ', ' . $this->getVarSelect();
564  }
565  }
566 
567  return $this->_sTitle;
568  }
569 
575  public function getLink()
576  {
577  if ($this->_sLink === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
578  $this->_sLink = oxRegistry::get("oxUtilsUrl")->cleanUrl($this->getArticle()->getLink(), array('force_sid'));
579  }
580 
581  return $this->getSession()->processUrl($this->_sLink);
582  }
583 
589  public function getShopId()
590  {
591  return $this->_sShopId;
592  }
593 
599  public function getSelList()
600  {
601  return $this->_aSelList;
602  }
603 
609  public function getChosenSelList()
610  {
612  }
613 
619  public function isBundle()
620  {
621  return $this->_blBundle;
622  }
623 
629  public function isDiscountArticle()
630  {
632  }
633 
639  public function isSkipDiscount()
640  {
642  }
643 
653  public function __get($sName)
654  {
655  if ($sName == 'oProduct') {
656  return $this->getArticle();
657  }
658  }
659 
665  public function __sleep()
666  {
667  $aRet = array();
668  foreach (get_object_vars($this) as $sKey => $sVar) {
669  if ($sKey != '_oArticle') {
670  $aRet[] = $sKey;
671  }
672  }
673 
674  return $aRet;
675  }
676 
692  protected function _setArticle($sProductId)
693  {
694  $oConfig = $this->getConfig();
695  $oArticle = $this->getArticle(true, $sProductId);
696 
697  // product ID
698  $this->_sProductId = $sProductId;
699 
700  $this->_sTitle = null;
701  $this->_sVarSelect = null;
702  $this->getTitle();
703 
704  // icon and details URL's
705  $this->_sIcon = $oArticle->oxarticles__oxicon->value;
706  $this->_sIconUrl = $oArticle->getIconUrl();
707  $this->_blSsl = $oConfig->isSsl();
708 
709  // removing force_sid from the link (in case it'll change)
710  $this->_sLink = oxRegistry::get("oxUtilsUrl")->cleanUrl($oArticle->getLink(), array('force_sid'));
711 
712  // shop Ids
713  $this->_sShopId = $oConfig->getShopId();
714  $this->_sNativeShopId = $oArticle->oxarticles__oxshopid->value;
715 
716  // SSL/NON SSL image paths
717  $this->_sDimageDirNoSsl = $oArticle->nossl_dimagedir;
718  $this->_sDimageDirSsl = $oArticle->ssl_dimagedir;
719  }
720 
730  protected function _setFromOrderArticle($oOrderArticle)
731  {
732  // overriding whole article
733  $this->_oArticle = $oOrderArticle;
734 
735  // product ID
736  $this->_sProductId = $oOrderArticle->getProductId();
737 
738  // products title
739  $this->_sTitle = $oOrderArticle->oxarticles__oxtitle->value;
740 
741  // shop Ids
742  $this->_sShopId = $this->getConfig()->getShopId();
743  $this->_sNativeShopId = $oOrderArticle->oxarticles__oxshopid->value;
744  }
745 
751  protected function _setSelectList($aSelList)
752  {
753  // checking for default select list
754  $aSelectLists = $this->getArticle()->getSelectLists();
755  if (!$aSelList || is_array($aSelList) && count($aSelList) == 0) {
756  if ($iSelCnt = count($aSelectLists)) {
757  $aSelList = array_fill(0, $iSelCnt, '0');
758  }
759  }
760 
761  $this->_aSelList = $aSelList;
762 
763  //
764  if (count($this->_aSelList) && is_array($this->_aSelList)) {
765  foreach ($this->_aSelList as $conkey => $iSel) {
766  $this->_aChosenSelectlist[$conkey] = new stdClass();
767  $this->_aChosenSelectlist[$conkey]->name = $aSelectLists[$conkey]['name'];
768  $this->_aChosenSelectlist[$conkey]->value = $aSelectLists[$conkey][$iSel]->name;
769  }
770  }
771  }
772 
778  public function getPersParams()
779  {
781  }
782 
788  public function setPersParams($aPersParam)
789  {
790  $this->_aPersistentParameters = $aPersParam;
791  }
792 
798  public function setBundle($blBundle)
799  {
800  $this->_blBundle = $blBundle;
801  }
802 
808  public function setSkipDiscounts($blSkip)
809  {
810  $this->_blSkipDiscounts = $blSkip;
811  }
812 
818  public function getProductId()
819  {
820  return $this->_sProductId;
821  }
822 
828  public function setWrapping($sWrapId)
829  {
830  $this->_sWrappingId = $sWrapId;
831  }
832 
838  public function getWrappingId()
839  {
840  return $this->_sWrappingId;
841  }
842 
848  public function getWrapping()
849  {
850  $oWrap = null;
851  if ($sWrapId = $this->getWrappingId()) {
852  $oWrap = oxNew('oxwrapping');
853  $oWrap->load($sWrapId);
854  }
855 
856  return $oWrap;
857  }
858 
864  public function getWishId()
865  {
866  return $this->_sWishId;
867  }
868 
874  public function setWishId($sWishId)
875  {
876  $this->_sWishId = $sWishId;
877  }
878 
884  public function setWishArticleId($sArticleId)
885  {
886  $this->_sWishArticleId = $sArticleId;
887  }
888 
894  public function getWishArticleId()
895  {
896  return $this->_sWishArticleId;
897  }
898 
906  public function getFRegularUnitPrice()
907  {
908  return oxRegistry::getLang()->formatCurrency($this->getRegularUnitPrice()->getPrice());
909  }
910 
918  public function getFUnitPrice()
919  {
920  return oxRegistry::getLang()->formatCurrency($this->getUnitPrice()->getPrice());
921  }
922 
930  public function getFTotalPrice()
931  {
932  return oxRegistry::getLang()->formatCurrency($this->getPrice()->getPrice());
933  }
934 
940  public function getVatPercent()
941  {
942  return oxRegistry::getLang()->formatVat($this->getPrice()->getVat());
943  }
944 
950  public function getVarSelect()
951  {
952  if ($this->_sVarSelect === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
953  $oArticle = $this->getArticle();
954  $this->_sVarSelect = $oArticle->oxarticles__oxvarselect->value ? $oArticle->oxarticles__oxvarselect->value : '';
955  }
956 
957  return $this->_sVarSelect;
958  }
959 
965  public function getLanguageId()
966  {
967  return $this->_iLanguageId;
968  }
969 
975  public function setLanguageId($iLanguageId)
976  {
977  $iOldLang = $this->_iLanguageId;
978  $this->_iLanguageId = $iLanguageId;
979 
980  // #0003777: reload content on language change
981  if ($iOldLang !== null && $iOldLang != $iLanguageId) {
982  try {
983  $this->_setArticle($this->getProductId());
984  } catch (oxNoArticleException $oEx) {
985  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
986  } catch (oxArticleInputException $oEx) {
987  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
988  }
989  }
990  }
991 }