<?php
namespace I6media\Facebookpixel\Helpers;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Registry;
use Magento\Framework\Locale\CurrencyInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Session;
class Data extends AbstractHelper
{
protected $_context;
protected $_storeManager;
protected $_registry;
protected $_currency;
protected $_category;
protected $_catalogSession;
const ID = 'i6media_facebookpixel/general/';
const OPTIONS = 'i6media_facebookpixel/options/';
const CART_SESSION_KEY = 'i6media_facebookpixel_last_added_cart';
const PURCHASE_SESSION_KEY = 'i6media_facebookpixel_last_added_order';
public function __construct
(
CategoryRepositoryInterface $category,
CurrencyInterface $currency,
Registry $registry,
Context $context,
StoreManagerInterface $storeManager,
Session $catalogSession
)
{
$this->_catalogSession = $catalogSession;
$this->_registry = $registry;
$this->_currency = $currency;
$this->_storeManager = $storeManager;
$this->_context = $context;
$this->_category = $category;
}
private function _getConfigValue($value)
{
return $this->_context->getScopeConfig()->getValue($value, ScopeInterface::SCOPE_STORE);
}
private function _setSessionData($key, $value)
{
return $this->_catalogSession->setData($key, $value);
}
private function _getSessionData($key, $remove = false)
{
return $this->_catalogSession->getData($key, $remove);
}
public function getLastOrderId($remove = false)
{
return $this->_getSessionData(self::PURCHASE_SESSION_KEY, $remove);
}
public function setLastOrderId($order_id)
{
return $this->_setSessionData(self::PURCHASE_SESSION_KEY, $order_id);
}
public function getLastAddedProductId($remove = false)
{
return $this->_getSessionData(self::CART_SESSION_KEY, $remove);
}
public function setLastAddedProductId($product_id)
{
return $this->_setSessionData(self::CART_SESSION_KEY, $product_id);
}
public function getCurrencyCode()
{
return $this->_currency->getCurrency(NULL)->getShortName();
}
public function getFacebookPixelId()
{
return $this->_getConfigValue(self::ID . 'facebook_pixel_id');
}
public function getIsPurchaseEnabled()
{
return (boolean) $this->_getConfigValue(self::OPTIONS . 'purchase_enabled');
}
public function getIsViewContentEnabled()
{
return (boolean) $this->_getConfigValue(self::OPTIONS . 'viewcontent_enabled');
}
public function getIsAddToCartEnabled()
{
return (boolean) $this->_getConfigValue(self::OPTIONS . 'addtocart_enabled');
}
public function getIsCompleteRegistrationEnabled()
{
return (boolean) $this->_getConfigValue(self::OPTIONS . 'completeregistration_enabled');
}
public function getProductTrackID($product)
{
if( $this->getProductTrackType() == "id" ){
$return = $product->getId();
} else {
$return = $product->getSKU();
}
return "'" . $return . "'";
}
public function getIsDynamicProductsEnabled()
{
return (boolean) $this->_getConfigValue(self::OPTIONS . 'dynamic_products');
}
public function getIsWishlistEnabled()
{
return (boolean) $this->_getConfigValue(self::OPTIONS . 'addtowishlist_enabled');
}
public function getIsEnabled()
{
return (boolean) $this->_getConfigValue(self::ID . 'enabled');
}
public function getProductTrackType()
{
return $this->_getConfigValue(self::ID . 'track_product_type');
}
public function getProductPrice($product)
{
return number_format($product->getFinalPrice(),2,'.','');
}
public function getCurrentProduct()
{
return $this->_registry->registry('current_product');
}
public function getCurrentCategory()
{
return $this->_registry->registry('current_category');
}
public function getIsInitiateCheckoutEnabled()
{
return (boolean) $this->_getConfigValue(self::OPTIONS . 'initiatecheckout_enabled');
}
public function getIsAddPaymentInfoEnabled()
{
return (boolean) $this->_getConfigValue(self::OPTIONS . 'addpaymentinfo_enabled');
}
public function getIsLeadEnabled()
{
return (boolean) $this->_getConfigValue(self::OPTIONS . 'lead_enabled');
}
public function getIsSearchEnabled()
{
return (boolean) $this->_getConfigValue(self::OPTIONS . 'search_enabled');
}
public function getCategoryChain()
{
$category = $this->getCurrentCategory();
$names = array();
if( is_null($category) ) return '';
$path = $category->getPath();
$path = explode("/",$path);
unset($path[0],$path[1]);
foreach($path as $id){
$category = $this->_category->get($id, $this->_storeManager->getStore()->getId());
$names[] = $category->getName();
}
$return = implode(" > ", $names);
return $return;
}
} |