ContactData.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Apiconnector;
  3. /**
  4. * Manages data synced as contact.
  5. * @package Dotdigitalgroup\Email\Model\Apiconnector
  6. */
  7. class ContactData
  8. {
  9. /**
  10. * @var array
  11. */
  12. public $contactData;
  13. /**
  14. * @var Object
  15. */
  16. public $model;
  17. /**
  18. * @var array
  19. */
  20. private $mappingHash;
  21. /**
  22. * @var \Magento\Sales\Model\ResourceModel\Order
  23. */
  24. private $orderResource;
  25. /**
  26. * @var \Magento\Sales\Model\OrderFactory
  27. */
  28. private $orderFactory;
  29. /**
  30. * @var \Dotdigitalgroup\Email\Helper\Config
  31. */
  32. private $configHelper;
  33. /**
  34. * @var \Magento\Catalog\Model\ProductFactory
  35. */
  36. private $productFactory;
  37. /**
  38. * @var \Magento\Catalog\Model\ResourceModel\Product
  39. */
  40. private $productResource;
  41. /**
  42. * @var \Magento\Catalog\Api\Data\CategoryInterfaceFactory
  43. */
  44. private $categoryFactory;
  45. /**
  46. * @var \Magento\Catalog\Model\ResourceModel\Category
  47. */
  48. private $categoryResource;
  49. /**
  50. * @var \Magento\Eav\Model\ConfigFactory
  51. */
  52. private $eavConfigFactory;
  53. /**
  54. * @var \Magento\Store\Model\StoreManagerInterface
  55. */
  56. private $storeManager;
  57. /**
  58. * @var \Magento\Store\Model\Store
  59. */
  60. private $store;
  61. /**
  62. * @var array
  63. */
  64. private $brandValue = [];
  65. /**
  66. * @var array
  67. */
  68. private $categoryNames = [];
  69. /**
  70. * @var array
  71. */
  72. private $products = [];
  73. /**
  74. * ContactData constructor.
  75. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  76. * @param \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory
  77. * @param \Magento\Catalog\Model\ResourceModel\Product $productResource
  78. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  79. * @param \Magento\Sales\Model\ResourceModel\Order $orderResource
  80. * @param \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory
  81. * @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource
  82. * @param \Magento\Eav\Model\ConfigFactory $eavConfigFactory
  83. * @param \Dotdigitalgroup\Email\Helper\Config $configHelper
  84. */
  85. public function __construct(
  86. \Magento\Store\Model\StoreManagerInterface $storeManager,
  87. \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,
  88. \Magento\Catalog\Model\ResourceModel\Product $productResource,
  89. \Magento\Sales\Model\OrderFactory $orderFactory,
  90. \Magento\Sales\Model\ResourceModel\Order $orderResource,
  91. \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory,
  92. \Magento\Catalog\Model\ResourceModel\Category $categoryResource,
  93. \Magento\Eav\Model\ConfigFactory $eavConfigFactory,
  94. \Dotdigitalgroup\Email\Helper\Config $configHelper
  95. ) {
  96. $this->storeManager = $storeManager;
  97. $this->orderFactory = $orderFactory;
  98. $this->configHelper = $configHelper;
  99. $this->orderResource = $orderResource;
  100. $this->productFactory = $productFactory;
  101. $this->categoryFactory = $categoryFactory;
  102. $this->productResource = $productResource;
  103. $this->categoryResource = $categoryResource;
  104. $this->eavConfigFactory = $eavConfigFactory;
  105. }
  106. /**
  107. * @param $storeId
  108. *
  109. * @return \Magento\Store\Api\Data\StoreInterface|\Magento\Store\Model\Store
  110. */
  111. private function getStore($storeId)
  112. {
  113. if (! isset($this->store)) {
  114. $this->store = $this->storeManager->getStore($storeId);
  115. }
  116. return $this->store;
  117. }
  118. /**
  119. * @param $data
  120. */
  121. public function setData($data)
  122. {
  123. $this->contactData[] = $data;
  124. }
  125. /**
  126. * @param $model
  127. */
  128. public function setContactData($model)
  129. {
  130. $this->model = $model;
  131. $mappingHash = array_keys($this->getMappingHash());
  132. foreach ($mappingHash as $key) {
  133. //Call user function based on the attribute mapped.
  134. $function = 'get';
  135. $exploded = explode('_', $key);
  136. foreach ($exploded as $one) {
  137. $function .= ucfirst($one);
  138. }
  139. $value = call_user_func(
  140. ['self', $function]
  141. );
  142. $this->contactData[$key] = $value;
  143. }
  144. }
  145. /**
  146. * @return array
  147. */
  148. public function getMappingHash()
  149. {
  150. return $this->mappingHash;
  151. }
  152. /**
  153. * @param mixed $mappingHash
  154. *
  155. * @return $this
  156. */
  157. public function setMappingHash($mappingHash)
  158. {
  159. $this->mappingHash = $mappingHash;
  160. return $this;
  161. }
  162. /**
  163. * Contact data array.
  164. *
  165. * @return array
  166. */
  167. public function toCSVArray()
  168. {
  169. return array_values($this->contactData);
  170. }
  171. /**
  172. * @return string
  173. * @throws \Magento\Framework\Exception\LocalizedException
  174. */
  175. public function getWebsiteName()
  176. {
  177. $store = $this->getStore($this->model->getStoreId());
  178. $website = $store->getWebsite(
  179. $store->getWebsiteId()
  180. );
  181. if ($website) {
  182. return $website->getName();
  183. }
  184. return '';
  185. }
  186. /**
  187. * @return string
  188. */
  189. public function getStoreName()
  190. {
  191. $store = $this->getStore($this->model->getStoreId());
  192. if ($store) {
  193. return $store->getName();
  194. }
  195. return '';
  196. }
  197. /**
  198. * @param mixed $id
  199. * @return string
  200. */
  201. public function getBrandValue($id)
  202. {
  203. if (! isset($this->brandValue[$id])) {
  204. //attribute mapped from the config
  205. $attributeCode = $this->configHelper->getWebsiteConfig(
  206. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_DATA_FIELDS_BRAND_ATTRIBUTE,
  207. $this->model->getWebsiteId()
  208. );
  209. $storeId = $this->model->getStoreId();
  210. $this->brandValue[$id] = $this->getAttributeValue($id, $attributeCode, $storeId);
  211. }
  212. return $this->brandValue[$id];
  213. }
  214. /**
  215. * Get first purchased category.
  216. *
  217. * @return string
  218. */
  219. public function getFirstCategoryPur()
  220. {
  221. $firstOrderId = $this->model->getFirstOrderId();
  222. $order = $this->orderFactory->create();
  223. $this->orderResource->load($order, $firstOrderId);
  224. $categoryIds = $this->getCategoriesFromOrderItems($order->getAllItems());
  225. return $this->getCategoryNames($categoryIds);
  226. }
  227. /**
  228. * @param $orderItems
  229. * @return array
  230. */
  231. public function getCategoriesFromOrderItems($orderItems)
  232. {
  233. $catIds = [];
  234. //categories from all products
  235. foreach ($orderItems as $item) {
  236. $product = $item->getProduct();
  237. //sales item product may return null if the product not exists anymore rathe then empty object
  238. if ($product) {
  239. $categoryIds = $product->getCategoryIds();
  240. if (count($categoryIds)) {
  241. $catIds = array_unique(array_merge($catIds, $categoryIds));
  242. }
  243. }
  244. }
  245. return $catIds;
  246. }
  247. /**
  248. * Get last purchased category.
  249. *
  250. * @return string
  251. */
  252. public function getLastCategoryPur()
  253. {
  254. $lastOrderId = $this->model->getLastOrderId();
  255. $order = $this->orderFactory->create();
  256. $this->orderResource->load($order, $lastOrderId);
  257. $categoryIds = $this->getCategoriesFromOrderItems($order->getAllItems());
  258. return $this->getCategoryNames($categoryIds);
  259. }
  260. /**
  261. * @param $categoryId
  262. * @return string
  263. */
  264. private function getCategoryValue($categoryId)
  265. {
  266. if ($categoryId) {
  267. $category = $this->categoryFactory->create()
  268. ->setStoreId($this->model->getStoreId());
  269. $this->categoryResource->load($category, $categoryId);
  270. return $category->getName();
  271. }
  272. return '';
  273. }
  274. /**
  275. * @param $categoryIds
  276. * @return string
  277. */
  278. public function getCategoryNames($categoryIds)
  279. {
  280. $names = [];
  281. foreach ($categoryIds as $id) {
  282. if (! isset($this->categoryNames[$id])) {
  283. $this->categoryNames[$id] = $this->getCategoryValue($id);
  284. }
  285. $names[$this->categoryNames[$id]] = $this->categoryNames[$id];
  286. }
  287. //comma separated category names
  288. if (count($names)) {
  289. return implode(',', $names);
  290. }
  291. return '';
  292. }
  293. /**
  294. * Get first purchased brand.
  295. *
  296. * @return string
  297. */
  298. public function getFirstBrandPur()
  299. {
  300. $id = $this->model->getProductIdForFirstBrand();
  301. return $this->getBrandValue($id);
  302. }
  303. /**
  304. * Get last purchased brand.
  305. *
  306. * @return string
  307. */
  308. public function getLastBrandPur()
  309. {
  310. $id = $this->model->getProductIdForLastBrand();
  311. return $this->getBrandValue($id);
  312. }
  313. /**
  314. * @return string
  315. * @throws \Magento\Framework\Exception\LocalizedException
  316. */
  317. public function getMostPurBrand()
  318. {
  319. $productId = $this->model->getProductIdForMostSoldProduct();
  320. $store = $this->getStore($this->model->getStoreId());
  321. $attributeCode = $this->configHelper->getWebsiteConfig(
  322. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_DATA_FIELDS_BRAND_ATTRIBUTE,
  323. $store->getWebsiteId()
  324. );
  325. //if the id and attribute found
  326. if ($productId && $attributeCode) {
  327. $product = $this->getProduct($productId);
  328. if ($product->getId()) {
  329. $value = $this->productResource->getAttribute($attributeCode)->getFrontend()->getValue($product);
  330. if ($value) {
  331. return $value;
  332. }
  333. }
  334. }
  335. return '';
  336. }
  337. /**
  338. * get most frequent day of purchase
  339. *
  340. * @return string
  341. */
  342. public function getMostFreqPurDay()
  343. {
  344. $day = $this->model->getWeekDay();
  345. if ($day) {
  346. return $day;
  347. }
  348. return "";
  349. }
  350. /**
  351. * get most frequent month of purchase
  352. *
  353. * @return string
  354. */
  355. public function getMostFreqPurMon()
  356. {
  357. $month = $this->model->getMonthDay();
  358. if ($month) {
  359. return $month;
  360. }
  361. return "";
  362. }
  363. /**
  364. * Get most purchased category.
  365. *
  366. * @return string
  367. */
  368. public function getMostPurCategory()
  369. {
  370. $categories = '';
  371. $productId = $this->model->getProductIdForMostSoldProduct();
  372. //sales data found for customer with product id
  373. if ($productId) {
  374. $product = $this->getProduct($productId);
  375. //product found
  376. if ($product->getId()) {
  377. $categoryIds = $product->getCategoryIds();
  378. if (count($categoryIds)) {
  379. $categories = $this->getCategoryNames($categoryIds);
  380. }
  381. }
  382. }
  383. return $categories;
  384. }
  385. /**
  386. * Get last increment id.
  387. *
  388. * @return int
  389. */
  390. public function getLastIncrementId()
  391. {
  392. return $this->model->getLastIncrementId();
  393. }
  394. /**
  395. * @return int
  396. */
  397. public function getLastOrderId()
  398. {
  399. return $this->model->getLastOrderId();
  400. }
  401. /**
  402. * @return string
  403. */
  404. public function getLastOrderDate()
  405. {
  406. return $this->model->getLastOrderDate();
  407. }
  408. /**
  409. * Get total spend.
  410. *
  411. * @return string
  412. */
  413. public function getTotalSpend()
  414. {
  415. return $this->model->getTotalSpend();
  416. }
  417. /**
  418. * Get average order value.
  419. *
  420. * @return string
  421. */
  422. public function getAverageOrderValue()
  423. {
  424. return $this->model->getAverageOrderValue();
  425. }
  426. /**
  427. * @return int
  428. */
  429. public function getNumberOfOrders()
  430. {
  431. return $this->model->getNumberOfOrders();
  432. }
  433. /**
  434. * @return int
  435. */
  436. public function getId()
  437. {
  438. return $this->model->getId();
  439. }
  440. /**
  441. * @param int $id
  442. * @param string $attributeCode
  443. * @param int $storeId
  444. *
  445. * @return string
  446. */
  447. private function getAttributeValue($id, $attributeCode, $storeId)
  448. {
  449. //if the id and attribute found
  450. if ($id && $attributeCode) {
  451. $product = $this->productFactory->create();
  452. $product = $product->setStoreId($storeId);
  453. $this->productResource->load($product, $id);
  454. $attribute = $this->productResource->getAttribute($attributeCode);
  455. if ($attribute && $product->getId()) {
  456. $value = $attribute->setStoreId($storeId)
  457. ->getSource()
  458. ->getOptionText($product->getData($attributeCode));
  459. //check for brand text
  460. if ($value) {
  461. return $value;
  462. }
  463. }
  464. }
  465. return '';
  466. }
  467. /**
  468. * @param int $productId
  469. *
  470. * @return \Magento\Catalog\Model\Product
  471. */
  472. private function getProduct($productId)
  473. {
  474. if (! isset($this->products[$productId])) {
  475. $product = $this->productFactory->create()
  476. ->setStoreId($this->model->getStoreId());
  477. $this->productResource->load($product, $productId);
  478. $this->products[$productId] = $product;
  479. }
  480. return $this->products[$productId];
  481. }
  482. }