OXID eShop CE  4.9.5
 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 
208 
214  public function getRegularUnitPrice()
215  {
217  }
218 
224  public function setRegularUnitPrice($oRegularUnitPrice)
225  {
226  $this->_oRegularUnitPrice = $oRegularUnitPrice;
227  }
228 
229 
246  public function init($sProductID, $dAmount, $aSel = null, $aPersParam = null, $blBundle = null)
247  {
248  $this->_setArticle($sProductID);
249  $this->setAmount($dAmount);
250  $this->_setSelectList($aSel);
251  $this->setPersParams($aPersParam);
252  $this->setBundle($blBundle);
253  $this->setLanguageId(oxRegistry::getLang()->getBaseLanguage());
254  }
255 
267  public function initFromOrderArticle($oOrderArticle)
268  {
269  $this->_setFromOrderArticle($oOrderArticle);
270  $this->setAmount($oOrderArticle->oxorderarticles__oxamount->value);
271  $this->_setSelectList($oOrderArticle->getOrderArticleSelectList());
272  $this->setPersParams($oOrderArticle->getPersParams());
273  $this->setBundle($oOrderArticle->isBundle());
274  }
275 
281  public function setAsDiscountArticle($blIsDiscountArticle)
282  {
283  $this->_blIsDiscountArticle = $blIsDiscountArticle;
284  }
285 
291  public function setStockCheckStatus($blStatus)
292  {
293  $this->_blCheckArticleStock = $blStatus;
294  }
295 
301  public function getStockCheckStatus()
302  {
304  }
305 
317  public function setAmount($dAmount, $blOverride = true, $sItemKey = null)
318  {
319  try {
320  //validating amount
321  $dAmount = oxRegistry::get("oxInputValidator")->validateBasketAmount($dAmount);
322  } catch (oxArticleInputException $oEx) {
323  $oEx->setArticleNr($this->getProductId());
324  $oEx->setProductId($this->getProductId());
325  // setting additional information for exception and then rethrowing
326  throw $oEx;
327  }
328 
329  $oArticle = $this->getArticle(true);
330 
331 
332  // setting default
333  $iOnStock = true;
334 
335  if ($blOverride) {
336  $this->_dAmount = $dAmount;
337  } else {
338  $this->_dAmount += $dAmount;
339  }
340 
341  // checking for stock
342  if ($this->getStockCheckStatus() == true) {
343  $dArtStockAmount = $this->getSession()->getBasket()->getArtStockInBasket($oArticle->getId(), $sItemKey);
344  $iOnStock = $oArticle->checkForStock($this->_dAmount, $dArtStockAmount);
345  if ($iOnStock !== true) {
346  if ($iOnStock === false) {
347  // no stock !
348  $this->_dAmount = 0;
349  } else {
350  // limited stock
351  $this->_dAmount = $iOnStock;
352  $blOverride = true;
353  }
354  }
355  }
356 
357  // calculating general weight
358  $this->_dWeight = $oArticle->oxarticles__oxweight->value * $this->_dAmount;
359 
360  if ($iOnStock !== true) {
362  $oEx = oxNew('oxOutOfStockException');
363  $oEx->setMessage('ERROR_MESSAGE_OUTOFSTOCK_OUTOFSTOCK');
364  $oEx->setArticleNr($oArticle->oxarticles__oxartnum->value);
365  $oEx->setProductId($oArticle->getProductId());
366  $oEx->setRemainingAmount($this->_dAmount);
367  $oEx->setBasketIndex($sItemKey);
368  throw $oEx;
369  }
370  }
371 
377  public function setPrice($oPrice)
378  {
379  $this->_oUnitPrice = clone $oPrice;
380 
381  $this->_oPrice = clone $oPrice;
382  $this->_oPrice->multiply($this->getAmount());
383  }
384 
390  public function getIconUrl()
391  {
392  // icon url must be (re)loaded in case icon is not set or shop was switched between ssl/nonssl
393  if ($this->_sIconUrl === null || $this->_blSsl != $this->getConfig()->isSsl()) {
394  $this->_sIconUrl = $this->getArticle()->getIconUrl();
395  }
396 
397  return $this->_sIconUrl;
398  }
399 
414  public function getArticle($blCheckProduct = false, $sProductId = null, $blDisableLazyLoading = false)
415  {
416  if ($this->_oArticle === null || (!$this->_oArticle->isOrderArticle() && $blDisableLazyLoading)) {
417  $sProductId = $sProductId ? $sProductId : $this->_sProductId;
418  if (!$sProductId) {
419  //this exception may not be caught, anyhow this is a critical exception
421  $oEx = oxNew('oxArticleException');
422  $oEx->setMessage('EXCEPTION_ARTICLE_NOPRODUCTID');
423  throw $oEx;
424  }
425 
426  $this->_oArticle = oxNew('oxarticle');
427  // #M773 Do not use article lazy loading on order save
428  if ($blDisableLazyLoading) {
429  $this->_oArticle->modifyCacheKey('_allviews');
430  $this->_oArticle->disableLazyLoading();
431  }
432 
433  // performance:
434  // - skipping variants loading
435  // - skipping 'ab' price info
436  // - load parent field
437  $this->_oArticle->setNoVariantLoading(true);
438  $this->_oArticle->setLoadParentData(true);
439  if (!$this->_oArticle->load($sProductId)) {
441  $oEx = oxNew('oxNoArticleException');
442  $oLang = oxRegistry::getLang();
443  $oEx->setMessage(sprintf($oLang->translateString('ERROR_MESSAGE_ARTICLE_ARTICLE_DOES_NOT_EXIST', $oLang->getBaseLanguage()), $sProductId));
444  $oEx->setArticleNr($sProductId);
445  $oEx->setProductId($sProductId);
446  throw $oEx;
447  }
448 
449  // cant put not visible product to basket (M:1286)
450  if ($blCheckProduct && !$this->_oArticle->isVisible()) {
452  $oEx = oxNew('oxNoArticleException');
453  $oLang = oxRegistry::getLang();
454  $oEx->setMessage(sprintf($oLang->translateString('ERROR_MESSAGE_ARTICLE_ARTICLE_DOES_NOT_EXIST', $oLang->getBaseLanguage()), $this->_oArticle->oxarticles__oxartnum->value));
455  $oEx->setArticleNr($sProductId);
456  $oEx->setProductId($sProductId);
457  throw $oEx;
458  }
459 
460  // cant put not buyable product to basket
461  if ($blCheckProduct && !$this->_oArticle->isBuyable()) {
463  $oEx = oxNew('oxArticleInputException');
464  $oEx->setMessage('ERROR_MESSAGE_ARTICLE_ARTICLE_NOT_BUYABLE');
465  $oEx->setArticleNr($sProductId);
466  $oEx->setProductId($sProductId);
467  throw $oEx;
468  }
469  }
470 
471  return $this->_oArticle;
472  }
473 
479  public function getdBundledAmount()
480  {
481  return $this->isBundle() ? $this->_dAmount : 0;
482  }
483 
489  public function getPrice()
490  {
491  return $this->_oPrice;
492  }
493 
499  public function getUnitPrice()
500  {
501  return $this->_oUnitPrice;
502  }
503 
509  public function getAmount()
510  {
511  return $this->_dAmount;
512  }
513 
519  public function getWeight()
520  {
521  return $this->_dWeight;
522  }
523 
529  public function getTitle()
530  {
531  if ($this->_sTitle === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
532 
533  $oArticle = $this->getArticle();
534  $this->_sTitle = $oArticle->oxarticles__oxtitle->value;
535 
536  if ($oArticle->oxarticles__oxvarselect->value) {
537  $this->_sTitle = $this->_sTitle . ', ' . $this->getVarSelect();
538  }
539  }
540 
541  return $this->_sTitle;
542  }
543 
549  public function getLink()
550  {
551  if ($this->_sLink === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
552  $this->_sLink = oxRegistry::get("oxUtilsUrl")->cleanUrl($this->getArticle()->getLink(), array('force_sid'));
553  }
554 
555  return $this->getSession()->processUrl($this->_sLink);
556  }
557 
563  public function getShopId()
564  {
565  return $this->_sShopId;
566  }
567 
573  public function getSelList()
574  {
575  return $this->_aSelList;
576  }
577 
583  public function getChosenSelList()
584  {
586  }
587 
593  public function isBundle()
594  {
595  return $this->_blBundle;
596  }
597 
603  public function isDiscountArticle()
604  {
606  }
607 
613  public function isSkipDiscount()
614  {
616  }
617 
627  public function __get($sName)
628  {
629  if ($sName == 'oProduct') {
630  return $this->getArticle();
631  }
632  }
633 
639  public function __sleep()
640  {
641  $aRet = array();
642  foreach (get_object_vars($this) as $sKey => $sVar) {
643  if ($sKey != '_oArticle') {
644  $aRet[] = $sKey;
645  }
646  }
647 
648  return $aRet;
649  }
650 
666  protected function _setArticle($sProductId)
667  {
668  $oConfig = $this->getConfig();
669  $oArticle = $this->getArticle(true, $sProductId);
670 
671  // product ID
672  $this->_sProductId = $sProductId;
673 
674  $this->_sTitle = null;
675  $this->_sVarSelect = null;
676  $this->getTitle();
677 
678  // icon and details URL's
679  $this->_sIcon = $oArticle->oxarticles__oxicon->value;
680  $this->_sIconUrl = $oArticle->getIconUrl();
681  $this->_blSsl = $oConfig->isSsl();
682 
683  // removing force_sid from the link (in case it'll change)
684  $this->_sLink = oxRegistry::get("oxUtilsUrl")->cleanUrl($oArticle->getLink(), array('force_sid'));
685 
686  // shop Ids
687  $this->_sShopId = $oConfig->getShopId();
688  $this->_sNativeShopId = $oArticle->oxarticles__oxshopid->value;
689 
690  // SSL/NON SSL image paths
691  $this->_sDimageDirNoSsl = $oArticle->nossl_dimagedir;
692  $this->_sDimageDirSsl = $oArticle->ssl_dimagedir;
693  }
694 
704  protected function _setFromOrderArticle($oOrderArticle)
705  {
706  // overriding whole article
707  $this->_oArticle = $oOrderArticle;
708 
709  // product ID
710  $this->_sProductId = $oOrderArticle->getProductId();
711 
712  // products title
713  $this->_sTitle = $oOrderArticle->oxarticles__oxtitle->value;
714 
715  // shop Ids
716  $this->_sShopId = $this->getConfig()->getShopId();
717  $this->_sNativeShopId = $oOrderArticle->oxarticles__oxshopid->value;
718  }
719 
725  protected function _setSelectList($aSelList)
726  {
727  // checking for default select list
728  $aSelectLists = $this->getArticle()->getSelectLists();
729  if (!$aSelList || is_array($aSelList) && count($aSelList) == 0) {
730  if ($iSelCnt = count($aSelectLists)) {
731  $aSelList = array_fill(0, $iSelCnt, '0');
732  }
733  }
734 
735  $this->_aSelList = $aSelList;
736 
737  //
738  if (count($this->_aSelList) && is_array($this->_aSelList)) {
739  foreach ($this->_aSelList as $conkey => $iSel) {
740  $this->_aChosenSelectlist[$conkey] = new stdClass();
741  $this->_aChosenSelectlist[$conkey]->name = $aSelectLists[$conkey]['name'];
742  $this->_aChosenSelectlist[$conkey]->value = $aSelectLists[$conkey][$iSel]->name;
743  }
744  }
745  }
746 
752  public function getPersParams()
753  {
755  }
756 
762  public function setPersParams($aPersParam)
763  {
764  $this->_aPersistentParameters = $aPersParam;
765  }
766 
772  public function setBundle($blBundle)
773  {
774  $this->_blBundle = $blBundle;
775  }
776 
782  public function setSkipDiscounts($blSkip)
783  {
784  $this->_blSkipDiscounts = $blSkip;
785  }
786 
792  public function getProductId()
793  {
794  return $this->_sProductId;
795  }
796 
802  public function setWrapping($sWrapId)
803  {
804  $this->_sWrappingId = $sWrapId;
805  }
806 
812  public function getWrappingId()
813  {
814  return $this->_sWrappingId;
815  }
816 
822  public function getWrapping()
823  {
824  $oWrap = null;
825  if ($sWrapId = $this->getWrappingId()) {
826  $oWrap = oxNew('oxwrapping');
827  $oWrap->load($sWrapId);
828  }
829 
830  return $oWrap;
831  }
832 
838  public function getWishId()
839  {
840  return $this->_sWishId;
841  }
842 
848  public function setWishId($sWishId)
849  {
850  $this->_sWishId = $sWishId;
851  }
852 
858  public function setWishArticleId($sArticleId)
859  {
860  $this->_sWishArticleId = $sArticleId;
861  }
862 
868  public function getWishArticleId()
869  {
870  return $this->_sWishArticleId;
871  }
872 
880  public function getFRegularUnitPrice()
881  {
882  return oxRegistry::getLang()->formatCurrency($this->getRegularUnitPrice()->getPrice());
883  }
884 
892  public function getFUnitPrice()
893  {
894  return oxRegistry::getLang()->formatCurrency($this->getUnitPrice()->getPrice());
895  }
896 
904  public function getFTotalPrice()
905  {
906  return oxRegistry::getLang()->formatCurrency($this->getPrice()->getPrice());
907  }
908 
914  public function getVatPercent()
915  {
916  return oxRegistry::getLang()->formatVat($this->getPrice()->getVat());
917  }
918 
924  public function getVarSelect()
925  {
926  if ($this->_sVarSelect === null || $this->getLanguageId() != oxRegistry::getLang()->getBaseLanguage()) {
927  $oArticle = $this->getArticle();
928  $this->_sVarSelect = $oArticle->oxarticles__oxvarselect->value ? $oArticle->oxarticles__oxvarselect->value : '';
929  }
930 
931  return $this->_sVarSelect;
932  }
933 
939  public function getLanguageId()
940  {
941  return $this->_iLanguageId;
942  }
943 
949  public function setLanguageId($iLanguageId)
950  {
951  $iOldLang = $this->_iLanguageId;
952  $this->_iLanguageId = $iLanguageId;
953 
954  // #0003777: reload content on language change
955  if ($iOldLang !== null && $iOldLang != $iLanguageId) {
956  try {
957  $this->_setArticle($this->getProductId());
958  } catch (oxNoArticleException $oEx) {
959  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
960  } catch (oxArticleInputException $oEx) {
961  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
962  }
963  }
964  }
965 }