123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- <?php
- namespace Dotdigitalgroup\Email\Model\Apiconnector;
- /**
- * Manages data synced as contact.
- * @package Dotdigitalgroup\Email\Model\Apiconnector
- */
- class ContactData
- {
- /**
- * @var array
- */
- public $contactData;
- /**
- * @var Object
- */
- public $model;
- /**
- * @var array
- */
- private $mappingHash;
- /**
- * @var \Magento\Sales\Model\ResourceModel\Order
- */
- private $orderResource;
- /**
- * @var \Magento\Sales\Model\OrderFactory
- */
- private $orderFactory;
- /**
- * @var \Dotdigitalgroup\Email\Helper\Config
- */
- private $configHelper;
- /**
- * @var \Magento\Catalog\Model\ProductFactory
- */
- private $productFactory;
- /**
- * @var \Magento\Catalog\Model\ResourceModel\Product
- */
- private $productResource;
- /**
- * @var \Magento\Catalog\Api\Data\CategoryInterfaceFactory
- */
- private $categoryFactory;
- /**
- * @var \Magento\Catalog\Model\ResourceModel\Category
- */
- private $categoryResource;
- /**
- * @var \Magento\Eav\Model\ConfigFactory
- */
- private $eavConfigFactory;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- private $storeManager;
- /**
- * @var \Magento\Store\Model\Store
- */
- private $store;
- /**
- * @var array
- */
- private $brandValue = [];
- /**
- * @var array
- */
- private $categoryNames = [];
- /**
- * @var array
- */
- private $products = [];
- /**
- * ContactData constructor.
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory
- * @param \Magento\Catalog\Model\ResourceModel\Product $productResource
- * @param \Magento\Sales\Model\OrderFactory $orderFactory
- * @param \Magento\Sales\Model\ResourceModel\Order $orderResource
- * @param \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory
- * @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource
- * @param \Magento\Eav\Model\ConfigFactory $eavConfigFactory
- * @param \Dotdigitalgroup\Email\Helper\Config $configHelper
- */
- public function __construct(
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,
- \Magento\Catalog\Model\ResourceModel\Product $productResource,
- \Magento\Sales\Model\OrderFactory $orderFactory,
- \Magento\Sales\Model\ResourceModel\Order $orderResource,
- \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory,
- \Magento\Catalog\Model\ResourceModel\Category $categoryResource,
- \Magento\Eav\Model\ConfigFactory $eavConfigFactory,
- \Dotdigitalgroup\Email\Helper\Config $configHelper
- ) {
- $this->storeManager = $storeManager;
- $this->orderFactory = $orderFactory;
- $this->configHelper = $configHelper;
- $this->orderResource = $orderResource;
- $this->productFactory = $productFactory;
- $this->categoryFactory = $categoryFactory;
- $this->productResource = $productResource;
- $this->categoryResource = $categoryResource;
- $this->eavConfigFactory = $eavConfigFactory;
- }
- /**
- * @param $storeId
- *
- * @return \Magento\Store\Api\Data\StoreInterface|\Magento\Store\Model\Store
- */
- private function getStore($storeId)
- {
- if (! isset($this->store)) {
- $this->store = $this->storeManager->getStore($storeId);
- }
- return $this->store;
- }
- /**
- * @param $data
- */
- public function setData($data)
- {
- $this->contactData[] = $data;
- }
- /**
- * @param $model
- */
- public function setContactData($model)
- {
- $this->model = $model;
- $mappingHash = array_keys($this->getMappingHash());
- foreach ($mappingHash as $key) {
- //Call user function based on the attribute mapped.
- $function = 'get';
- $exploded = explode('_', $key);
- foreach ($exploded as $one) {
- $function .= ucfirst($one);
- }
- $value = call_user_func(
- ['self', $function]
- );
- $this->contactData[$key] = $value;
- }
- }
- /**
- * @return array
- */
- public function getMappingHash()
- {
- return $this->mappingHash;
- }
- /**
- * @param mixed $mappingHash
- *
- * @return $this
- */
- public function setMappingHash($mappingHash)
- {
- $this->mappingHash = $mappingHash;
- return $this;
- }
- /**
- * Contact data array.
- *
- * @return array
- */
- public function toCSVArray()
- {
- return array_values($this->contactData);
- }
- /**
- * @return string
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getWebsiteName()
- {
- $store = $this->getStore($this->model->getStoreId());
- $website = $store->getWebsite(
- $store->getWebsiteId()
- );
- if ($website) {
- return $website->getName();
- }
- return '';
- }
- /**
- * @return string
- */
- public function getStoreName()
- {
- $store = $this->getStore($this->model->getStoreId());
- if ($store) {
- return $store->getName();
- }
- return '';
- }
- /**
- * @param mixed $id
- * @return string
- */
- public function getBrandValue($id)
- {
- if (! isset($this->brandValue[$id])) {
- //attribute mapped from the config
- $attributeCode = $this->configHelper->getWebsiteConfig(
- \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_DATA_FIELDS_BRAND_ATTRIBUTE,
- $this->model->getWebsiteId()
- );
- $storeId = $this->model->getStoreId();
- $this->brandValue[$id] = $this->getAttributeValue($id, $attributeCode, $storeId);
- }
- return $this->brandValue[$id];
- }
- /**
- * Get first purchased category.
- *
- * @return string
- */
- public function getFirstCategoryPur()
- {
- $firstOrderId = $this->model->getFirstOrderId();
- $order = $this->orderFactory->create();
- $this->orderResource->load($order, $firstOrderId);
- $categoryIds = $this->getCategoriesFromOrderItems($order->getAllItems());
- return $this->getCategoryNames($categoryIds);
- }
- /**
- * @param $orderItems
- * @return array
- */
- public function getCategoriesFromOrderItems($orderItems)
- {
- $catIds = [];
- //categories from all products
- foreach ($orderItems as $item) {
- $product = $item->getProduct();
- //sales item product may return null if the product not exists anymore rathe then empty object
- if ($product) {
- $categoryIds = $product->getCategoryIds();
- if (count($categoryIds)) {
- $catIds = array_unique(array_merge($catIds, $categoryIds));
- }
- }
- }
- return $catIds;
- }
- /**
- * Get last purchased category.
- *
- * @return string
- */
- public function getLastCategoryPur()
- {
- $lastOrderId = $this->model->getLastOrderId();
- $order = $this->orderFactory->create();
- $this->orderResource->load($order, $lastOrderId);
- $categoryIds = $this->getCategoriesFromOrderItems($order->getAllItems());
- return $this->getCategoryNames($categoryIds);
- }
- /**
- * @param $categoryId
- * @return string
- */
- private function getCategoryValue($categoryId)
- {
- if ($categoryId) {
- $category = $this->categoryFactory->create()
- ->setStoreId($this->model->getStoreId());
- $this->categoryResource->load($category, $categoryId);
- return $category->getName();
- }
- return '';
- }
- /**
- * @param $categoryIds
- * @return string
- */
- public function getCategoryNames($categoryIds)
- {
- $names = [];
- foreach ($categoryIds as $id) {
- if (! isset($this->categoryNames[$id])) {
- $this->categoryNames[$id] = $this->getCategoryValue($id);
- }
- $names[$this->categoryNames[$id]] = $this->categoryNames[$id];
- }
- //comma separated category names
- if (count($names)) {
- return implode(',', $names);
- }
- return '';
- }
- /**
- * Get first purchased brand.
- *
- * @return string
- */
- public function getFirstBrandPur()
- {
- $id = $this->model->getProductIdForFirstBrand();
- return $this->getBrandValue($id);
- }
- /**
- * Get last purchased brand.
- *
- * @return string
- */
- public function getLastBrandPur()
- {
- $id = $this->model->getProductIdForLastBrand();
- return $this->getBrandValue($id);
- }
- /**
- * @return string
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getMostPurBrand()
- {
- $productId = $this->model->getProductIdForMostSoldProduct();
- $store = $this->getStore($this->model->getStoreId());
- $attributeCode = $this->configHelper->getWebsiteConfig(
- \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_DATA_FIELDS_BRAND_ATTRIBUTE,
- $store->getWebsiteId()
- );
- //if the id and attribute found
- if ($productId && $attributeCode) {
- $product = $this->getProduct($productId);
- if ($product->getId()) {
- $value = $this->productResource->getAttribute($attributeCode)->getFrontend()->getValue($product);
- if ($value) {
- return $value;
- }
- }
- }
- return '';
- }
- /**
- * get most frequent day of purchase
- *
- * @return string
- */
- public function getMostFreqPurDay()
- {
- $day = $this->model->getWeekDay();
- if ($day) {
- return $day;
- }
- return "";
- }
- /**
- * get most frequent month of purchase
- *
- * @return string
- */
- public function getMostFreqPurMon()
- {
- $month = $this->model->getMonthDay();
- if ($month) {
- return $month;
- }
- return "";
- }
- /**
- * Get most purchased category.
- *
- * @return string
- */
- public function getMostPurCategory()
- {
- $categories = '';
- $productId = $this->model->getProductIdForMostSoldProduct();
- //sales data found for customer with product id
- if ($productId) {
- $product = $this->getProduct($productId);
- //product found
- if ($product->getId()) {
- $categoryIds = $product->getCategoryIds();
- if (count($categoryIds)) {
- $categories = $this->getCategoryNames($categoryIds);
- }
- }
- }
- return $categories;
- }
- /**
- * Get last increment id.
- *
- * @return int
- */
- public function getLastIncrementId()
- {
- return $this->model->getLastIncrementId();
- }
- /**
- * @return int
- */
- public function getLastOrderId()
- {
- return $this->model->getLastOrderId();
- }
- /**
- * @return string
- */
- public function getLastOrderDate()
- {
- return $this->model->getLastOrderDate();
- }
- /**
- * Get total spend.
- *
- * @return string
- */
- public function getTotalSpend()
- {
- return $this->model->getTotalSpend();
- }
- /**
- * Get average order value.
- *
- * @return string
- */
- public function getAverageOrderValue()
- {
- return $this->model->getAverageOrderValue();
- }
- /**
- * @return int
- */
- public function getNumberOfOrders()
- {
- return $this->model->getNumberOfOrders();
- }
- /**
- * @return int
- */
- public function getId()
- {
- return $this->model->getId();
- }
- /**
- * @param int $id
- * @param string $attributeCode
- * @param int $storeId
- *
- * @return string
- */
- private function getAttributeValue($id, $attributeCode, $storeId)
- {
- //if the id and attribute found
- if ($id && $attributeCode) {
- $product = $this->productFactory->create();
- $product = $product->setStoreId($storeId);
- $this->productResource->load($product, $id);
- $attribute = $this->productResource->getAttribute($attributeCode);
- if ($attribute && $product->getId()) {
- $value = $attribute->setStoreId($storeId)
- ->getSource()
- ->getOptionText($product->getData($attributeCode));
- //check for brand text
- if ($value) {
- return $value;
- }
- }
- }
- return '';
- }
- /**
- * @param int $productId
- *
- * @return \Magento\Catalog\Model\Product
- */
- private function getProduct($productId)
- {
- if (! isset($this->products[$productId])) {
- $product = $this->productFactory->create()
- ->setStoreId($this->model->getStoreId());
- $this->productResource->load($product, $productId);
- $this->products[$productId] = $product;
- }
- return $this->products[$productId];
- }
- }
|