Tax.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee;
  7. /**
  8. * Catalog product WEEE tax backend attribute model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Tax extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  13. {
  14. /**
  15. * @var \Magento\Store\Model\StoreManagerInterface
  16. */
  17. protected $_storeManager;
  18. /**
  19. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  20. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  21. * @param string $connectionName
  22. */
  23. public function __construct(
  24. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  25. \Magento\Store\Model\StoreManagerInterface $storeManager,
  26. $connectionName = null
  27. ) {
  28. $this->_storeManager = $storeManager;
  29. parent::__construct($context, $connectionName);
  30. }
  31. /**
  32. * Defines main resource table and table identifier field
  33. *
  34. * @return void
  35. */
  36. protected function _construct()
  37. {
  38. $this->_init('weee_tax', 'value_id');
  39. }
  40. /**
  41. * Load product data
  42. *
  43. * @param \Magento\Catalog\Model\Product $product
  44. * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
  45. * @return array
  46. */
  47. public function loadProductData($product, $attribute)
  48. {
  49. $select = $this->getConnection()->select()->from(
  50. $this->getMainTable(),
  51. ['website_id', 'country', 'state', 'value']
  52. )->where(
  53. 'entity_id = ?',
  54. (int)$product->getId()
  55. )->where(
  56. 'attribute_id = ?',
  57. (int)$attribute->getId()
  58. );
  59. if ($attribute->isScopeGlobal()) {
  60. $select->where('website_id = ?', 0);
  61. } else {
  62. $storeId = $product->getStoreId();
  63. if ($storeId) {
  64. $select->where(
  65. 'website_id IN (?)',
  66. [0, $this->_storeManager->getStore($storeId)->getWebsiteId()]
  67. );
  68. }
  69. }
  70. return $this->getConnection()->fetchAll($select);
  71. }
  72. /**
  73. * Delete product data
  74. *
  75. * @param \Magento\Catalog\Model\Product $product
  76. * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
  77. * @return $this
  78. */
  79. public function deleteProductData($product, $attribute)
  80. {
  81. $where = ['entity_id = ?' => (int)$product->getId(), 'attribute_id = ?' => (int)$attribute->getId()];
  82. $connection = $this->getConnection();
  83. if (!$attribute->isScopeGlobal()) {
  84. $storeId = $product->getStoreId();
  85. if ($storeId) {
  86. $where['website_id IN(?)'] = [0, $this->_storeManager->getStore($storeId)->getWebsiteId()];
  87. }
  88. }
  89. $connection->delete($this->getMainTable(), $where);
  90. return $this;
  91. }
  92. /**
  93. * Insert product data
  94. *
  95. * @param \Magento\Catalog\Model\Product $product
  96. * @param array $data
  97. * @return $this
  98. */
  99. public function insertProductData($product, $data)
  100. {
  101. $data['entity_id'] = (int)$product->getId();
  102. $this->getConnection()->insert($this->getMainTable(), $data);
  103. return $this;
  104. }
  105. }