OXID eShop CE  4.10.7
 All Classes Namespaces 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  $selectForUpdate = false;
371  if ($this->getConfig()->getConfigParam('blPsBasketReservationEnabled')) {
372  $selectForUpdate = true;
373  }
374  $iOnStock = $oArticle->checkForStock($this->_dAmount, $dArtStockAmount, $selectForUpdate);
375  if ($iOnStock !== true) {
376  if ($iOnStock === false) {
377  // no stock !
378  $this->_dAmount = 0;
379  } else {
380  // limited stock
381  $this->_dAmount = $iOnStock;
382  $blOverride = true;
383  }
384  }
385  }
386 
387  // calculating general weight
388  $this->_dWeight = $oArticle->oxarticles__oxweight->value * $this->_dAmount;
389 
390  if ($iOnStock !== true) {
392  $oEx = oxNew('oxOutOfStockException');
393  $oEx->setMessage('ERROR_MESSAGE_OUTOFSTOCK_OUTOFSTOCK');
394  $oEx->setArticleNr($oArticle->oxarticles__oxartnum->value);
395  $oEx->setProductId($oArticle->getProductId());
396  $oEx->setRemainingAmount($this->_dAmount);
397  $oEx->setBasketIndex($sItemKey);
398  throw $oEx;
399  }
400  }
401 
407  public function setPrice($oPrice)
408  {
409  $this->_oUnitPrice = clone $oPrice;
410 
411  $this->_oPrice = clone $oPrice;
412  $this->_oPrice->multiply($this->getAmount());
413  }
414 
420  public function getIconUrl()
421  {
422  // icon url must be (re)loaded in case icon is not set or shop was switched between ssl/nonssl
423  if ($this->_sIconUrl === null || $this->_blSsl != $this->getConfig()->isSsl()) {
424  $this->_sIconUrl = $this->getArticle()->getIconUrl();
425  }
426 
427  return $this->_sIconUrl;
428  }
429 
444  public function getArticle($blCheckProduct = false, $sProductId = null, $blDisableLazyLoading = false)
445  {
446  if ($this->_oArticle === null || (!$this->_oArticle->isOrderArticle() && $blDisableLazyLoading)) {
447  $sProductId = $sProductId ? $sProductId : $this->_sProductId;
448  if (!$sProductId) {
449  //this exception may not be caught, anyhow this is a critical exception
451  $oEx = oxNew('oxArticleException');
452  $oEx->setMessage('EXCEPTION_ARTICLE_NOPRODUCTID');
453  throw $oEx;
454  }
455 
456  $this->_oArticle = oxNew('oxarticle');
457  // #M773 Do not use article lazy loading on order save
458  if ($blDisableLazyLoading) {
459  $this->_oArticle->modifyCacheKey('_allviews');
460  $this->_oArticle->disableLazyLoading();
461  }
462 
463  // performance:
464  // - skipping variants loading
465  // - skipping 'ab' price info
466  // - load parent field
467  $this->_oArticle->setNoVariantLoading(true);
468  $this->_oArticle->setLoadParentData(true);
469  if (!$this->_oArticle->load($sProductId)) {
471  $oEx = oxNew('oxNoArticleException');
472  $oLang = oxRegistry::getLang();
473  $oEx->setMessage(sprintf($oLang->translateString('ERROR_MESSAGE_ARTICLE_ARTICLE_DOES_NOT_EXIST', $oLang->getBaseLanguage()), $sProductId));
474  $oEx->setArticleNr($sProductId);
475  $oEx->setProductId($sProductId);
476  throw $oEx;
477  }
478 
479  // cant put not visible product to basket (M:1286)
480  if ($blCheckProduct && !$this->_oArticle->isVisible()) {
482  $oEx = oxNew('oxNoArticleException');
483  $oLang = oxRegistry::getLang();
484  $oEx->setMessage(sprintf($oLang->translateString('ERROR_MESSAGE_ARTICLE_ARTICLE_DOES_NOT_EXIST', $oLang->getBaseLanguage()), $this->_oArticle->oxarticles__oxartnum->value));
485  $oEx->setArticleNr($sProductId);
486  $oEx->setProductId($sProductId);
487  throw $oEx;
488  }
489 
490  // cant put not buyable product to basket
491  if ($blCheckProduct && !$this->_oArticle->isBuyable()) {
493  $oEx = oxNew('oxArticleInputException');
494  $oEx->setMessage('ERROR_MESSAGE_ARTICLE_ARTICLE_NOT_BUYABLE');
495  $oEx->setArticleNr($sProductId);
496  $oEx->setProductId($sProductId);
497  throw $oEx;
498  }
499  }
500 
501  return $this->_oArticle;
502  }
503 
509  public function getdBundledAmount()
510  {
511  return $this->isBundle() ? $this->_dAmount : 0;
512  }
513 
519  public function getPrice()
520  {
521  return $this->_oPrice;
522  }
523 
529  public function getUnitPrice()
530  {
531  return $this->_oUnitPrice;
532  }
533 
539  public function getAmount()
540  {
541  return $this->_dAmount;
542  }
543 
549  public function getWeight()
550  {
551  return $this->_dWeight;
552  }
553 
559  public function getTitle()
560  {
561  if ($this->_sTitle === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
562 
563  $oArticle = $this->getArticle();
564  $this->_sTitle = $oArticle->oxarticles__oxtitle->value;
565 
566  if ($oArticle->oxarticles__oxvarselect->value) {
567  $this->_sTitle = $this->_sTitle . ', ' . $this->getVarSelect();
568  }
569  }
570 
571  return $this->_sTitle;
572  }
573 
579  public function getLink()
580  {
581  if ($this->_sLink === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
582  $this->_sLink = oxRegistry::get("oxUtilsUrl")->cleanUrl($this->getArticle()->getLink(), array('force_sid'));
583  }
584 
585  return $this->getSession()->processUrl($this->_sLink);
586  }
587 
593  public function getShopId()
594  {
595  return $this->_sShopId;
596  }
597 
603  public function getSelList()
604  {
605  return $this->_aSelList;
606  }
607 
613  public function getChosenSelList()
614  {
616  }
617 
623  public function isBundle()
624  {
625  return $this->_blBundle;
626  }
627 
633  public function isDiscountArticle()
634  {
636  }
637 
643  public function isSkipDiscount()
644  {
646  }
647 
657  public function __get($sName)
658  {
659  if ($sName == 'oProduct') {
660  return $this->getArticle();
661  }
662  }
663 
669  public function __sleep()
670  {
671  $aRet = array();
672  foreach (get_object_vars($this) as $sKey => $sVar) {
673  if ($sKey != '_oArticle') {
674  $aRet[] = $sKey;
675  }
676  }
677 
678  return $aRet;
679  }
680 
696  protected function _setArticle($sProductId)
697  {
698  $oConfig = $this->getConfig();
699  $oArticle = $this->getArticle(true, $sProductId);
700 
701  // product ID
702  $this->_sProductId = $sProductId;
703 
704  $this->_sTitle = null;
705  $this->_sVarSelect = null;
706  $this->getTitle();
707 
708  // icon and details URL's
709  $this->_sIcon = $oArticle->oxarticles__oxicon->value;
710  $this->_sIconUrl = $oArticle->getIconUrl();
711  $this->_blSsl = $oConfig->isSsl();
712 
713  // removing force_sid from the link (in case it'll change)
714  $this->_sLink = oxRegistry::get("oxUtilsUrl")->cleanUrl($oArticle->getLink(), array('force_sid'));
715 
716  // shop Ids
717  $this->_sShopId = $oConfig->getShopId();
718  $this->_sNativeShopId = $oArticle->oxarticles__oxshopid->value;
719 
720  // SSL/NON SSL image paths
721  $this->_sDimageDirNoSsl = $oArticle->nossl_dimagedir;
722  $this->_sDimageDirSsl = $oArticle->ssl_dimagedir;
723  }
724 
734  protected function _setFromOrderArticle($oOrderArticle)
735  {
736  // overriding whole article
737  $this->_oArticle = $oOrderArticle;
738 
739  // product ID
740  $this->_sProductId = $oOrderArticle->getProductId();
741 
742  // products title
743  $this->_sTitle = $oOrderArticle->oxarticles__oxtitle->value;
744 
745  // shop Ids
746  $this->_sShopId = $this->getConfig()->getShopId();
747  $this->_sNativeShopId = $oOrderArticle->oxarticles__oxshopid->value;
748  }
749 
755  protected function _setSelectList($aSelList)
756  {
757  // checking for default select list
758  $aSelectLists = $this->getArticle()->getSelectLists();
759  if (!$aSelList || is_array($aSelList) && count($aSelList) == 0) {
760  if ($iSelCnt = count($aSelectLists)) {
761  $aSelList = array_fill(0, $iSelCnt, '0');
762  }
763  }
764 
765  $this->_aSelList = $aSelList;
766 
767  //
768  if (count($this->_aSelList) && is_array($this->_aSelList)) {
769  foreach ($this->_aSelList as $conkey => $iSel) {
770  $this->_aChosenSelectlist[$conkey] = new stdClass();
771  $this->_aChosenSelectlist[$conkey]->name = $aSelectLists[$conkey]['name'];
772  $this->_aChosenSelectlist[$conkey]->value = $aSelectLists[$conkey][$iSel]->name;
773  }
774  }
775  }
776 
782  public function getPersParams()
783  {
785  }
786 
792  public function setPersParams($aPersParam)
793  {
794  $this->_aPersistentParameters = $aPersParam;
795  }
796 
802  public function setBundle($blBundle)
803  {
804  $this->_blBundle = $blBundle;
805  }
806 
812  public function setSkipDiscounts($blSkip)
813  {
814  $this->_blSkipDiscounts = $blSkip;
815  }
816 
822  public function getProductId()
823  {
824  return $this->_sProductId;
825  }
826 
832  public function setWrapping($sWrapId)
833  {
834  $this->_sWrappingId = $sWrapId;
835  }
836 
842  public function getWrappingId()
843  {
844  return $this->_sWrappingId;
845  }
846 
852  public function getWrapping()
853  {
854  $oWrap = null;
855  if ($sWrapId = $this->getWrappingId()) {
856  $oWrap = oxNew('oxwrapping');
857  $oWrap->load($sWrapId);
858  }
859 
860  return $oWrap;
861  }
862 
868  public function getWishId()
869  {
870  return $this->_sWishId;
871  }
872 
878  public function setWishId($sWishId)
879  {
880  $this->_sWishId = $sWishId;
881  }
882 
888  public function setWishArticleId($sArticleId)
889  {
890  $this->_sWishArticleId = $sArticleId;
891  }
892 
898  public function getWishArticleId()
899  {
900  return $this->_sWishArticleId;
901  }
902 
910  public function getFRegularUnitPrice()
911  {
912  return oxRegistry::getLang()->formatCurrency($this->getRegularUnitPrice()->getPrice());
913  }
914 
922  public function getFUnitPrice()
923  {
924  return oxRegistry::getLang()->formatCurrency($this->getUnitPrice()->getPrice());
925  }
926 
934  public function getFTotalPrice()
935  {
936  return oxRegistry::getLang()->formatCurrency($this->getPrice()->getPrice());
937  }
938 
944  public function getVatPercent()
945  {
946  return oxRegistry::getLang()->formatVat($this->getPrice()->getVat());
947  }
948 
954  public function getVarSelect()
955  {
956  if ($this->_sVarSelect === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
957  $oArticle = $this->getArticle();
958  $sVarSelectValue = $oArticle->oxarticles__oxvarselect->value;
959  $this->_sVarSelect = (!empty($sVarSelectValue) || $sVarSelectValue === '0') ? $sVarSelectValue : '';
960  }
961 
962  return $this->_sVarSelect;
963  }
964 
970  public function getLanguageId()
971  {
972  return $this->_iLanguageId;
973  }
974 
980  public function setLanguageId($iLanguageId)
981  {
982  $iOldLang = $this->_iLanguageId;
983  $this->_iLanguageId = $iLanguageId;
984 
985  // #0003777: reload content on language change
986  if ($iOldLang !== null && $iOldLang != $iLanguageId) {
987  try {
988  $this->_setArticle($this->getProductId());
989  } catch (oxNoArticleException $oEx) {
990  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
991  } catch (oxArticleInputException $oEx) {
992  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
993  }
994  }
995  }
996 }