BothInteract_ConfigurableProductVariantsImageAssignment - Version 1.1.0

Version Notes

Added configuration options in admin backend.

Download this release

Release Info

Developer Matthias Kerstner
Extension BothInteract_ConfigurableProductVariantsImageAssignment
Version 1.1.0
Comparing to
See all releases


Code changes from version 1.0.0 to 1.1.0

app/code/community/BothInteract/ConfigurableProductVariantsImageAssignment/Model/Observer.php CHANGED
@@ -1,9 +1,9 @@
1
  <?php
2
 
3
  /**
4
- * Observer class that handles automatic image assignment for child products of
5
- * configurable products (i.e. associated products) once a configurable product
6
- * is being save()'d.
7
  *
8
  * It is possible to set the list of required image types for child products:
9
  * - @see self::$IMAGE_TYPE_BASE_IMAGE
@@ -25,27 +25,42 @@
25
  */
26
  class BothInteract_ConfigurableProductVariantsImageAssignment_Model_Observer {
27
 
28
- /** @var string name of logfile */
29
- private static $LOG_FILE = 'bothinteract_configurableproductvariantsimageassignment.log';
30
-
31
  /** @var string placeholder text if no image is set */
32
  private static $IMAGE_NO_SELECTION = 'no_selection';
33
 
34
  /** @var string base image type used by Magento */
35
- private static $IMAGE_TYPE_BASE_IMAGE = 'image';
36
 
37
  /** @var string small_image type used by Magento */
38
- private static $IMAGE_TYPE_SMALL_IMAGE = 'small_image';
39
 
40
  /** @var string thumbnail image type used by Magento */
41
- private static $IMAGE_TYPE_THUMBNAIL = 'thumbnail';
42
 
43
  /**
44
- * Logs $msg to logfile.
45
  * @param string $msg
46
  */
47
  private function logToFile($msg) {
48
- Mage::log($msg, null, self::$LOG_FILE);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  }
50
 
51
  /**
@@ -159,7 +174,10 @@ class BothInteract_ConfigurableProductVariantsImageAssignment_Model_Observer {
159
  * folder.
160
  */
161
  $childProduct->addImageToMediaGallery($parentProductBaseImagePath, $requiredChildProductImageTypes, false, false);
162
- $childProduct->save();
 
 
 
163
 
164
  $this->logToFile('Successfully set required image type(s) ['
165
  . implode(',', $requiredChildProductImageTypes)
@@ -284,6 +302,12 @@ class BothInteract_ConfigurableProductVariantsImageAssignment_Model_Observer {
284
  */
285
  public function catalog_product_save_after(Varien_Event_Observer $observer) {
286
  try {
 
 
 
 
 
 
287
  /**
288
  * Can be of any product type, e.g. configurable, grouped, simple,
289
  * @var $order Mage_Catalog_Model_Product
@@ -307,9 +331,18 @@ class BothInteract_ConfigurableProductVariantsImageAssignment_Model_Observer {
307
  * IMAGE_TYPE_BASE_IMAGE for e.g. Amazon Listing to work since it
308
  * requires a base image.
309
  *
310
- * TODO: load these settings from admin backend
311
  */
312
- $requiredChildProductImageTypes = array(self::$IMAGE_TYPE_BASE_IMAGE);
 
 
 
 
 
 
 
 
 
313
 
314
  if ($product->getTypeId() === Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) {
315
  $this->handleSimpleProduct($product, $requiredChildProductImageTypes);
1
  <?php
2
 
3
  /**
4
+ * Handles automatic image assignment for child products of configurable
5
+ * products (i.e. associated products) once a configurable product is being
6
+ * save()'d.
7
  *
8
  * It is possible to set the list of required image types for child products:
9
  * - @see self::$IMAGE_TYPE_BASE_IMAGE
25
  */
26
  class BothInteract_ConfigurableProductVariantsImageAssignment_Model_Observer {
27
 
 
 
 
28
  /** @var string placeholder text if no image is set */
29
  private static $IMAGE_NO_SELECTION = 'no_selection';
30
 
31
  /** @var string base image type used by Magento */
32
+ public static $IMAGE_TYPE_BASE_IMAGE = 'image';
33
 
34
  /** @var string small_image type used by Magento */
35
+ public static $IMAGE_TYPE_SMALL_IMAGE = 'small_image';
36
 
37
  /** @var string thumbnail image type used by Magento */
38
+ public static $IMAGE_TYPE_THUMBNAIL = 'thumbnail';
39
 
40
  /**
41
+ * Logs $msg to logfile specified in configuration.
42
  * @param string $msg
43
  */
44
  private function logToFile($msg) {
45
+ Mage::log($msg, null, Mage::getStoreConfig(
46
+ 'bothinteract_configurableproductvariantsimageassignment/general/log_file', Mage::app()->getStore()));
47
+ }
48
+
49
+ /**
50
+ *
51
+ * @return array
52
+ */
53
+ private function valueToImageType($value) {
54
+ $arr = array(
55
+ BothInteract_ConfigurableProductVariantsImageAssignment_Model_System_Config_Source_View::$VALUE_IMAGE_TYPE_BASE_IMAGE =>
56
+ self::$IMAGE_TYPE_BASE_IMAGE,
57
+ BothInteract_ConfigurableProductVariantsImageAssignment_Model_System_Config_Source_View::$VALUE_IMAGE_TYPE_SMALL_IMAGE =>
58
+ self::$IMAGE_TYPE_SMALL_IMAGE,
59
+ BothInteract_ConfigurableProductVariantsImageAssignment_Model_System_Config_Source_View::$VALUE_IMAGE_TYPE_THUMBNAIL =>
60
+ self::$IMAGE_TYPE_THUMBNAIL
61
+ );
62
+
63
+ return isset($arr[$value]) ? $arr[$value] : null;
64
  }
65
 
66
  /**
174
  * folder.
175
  */
176
  $childProduct->addImageToMediaGallery($parentProductBaseImagePath, $requiredChildProductImageTypes, false, false);
177
+
178
+ if (!Mage::getStoreConfig('bothinteract_configurableproductvariantsimageassignment/general/is_simulation')) {
179
+ $childProduct->save();
180
+ }
181
 
182
  $this->logToFile('Successfully set required image type(s) ['
183
  . implode(',', $requiredChildProductImageTypes)
302
  */
303
  public function catalog_product_save_after(Varien_Event_Observer $observer) {
304
  try {
305
+
306
+ if (!Mage::getStoreConfig('bothinteract_configurableproductvariantsimageassignment/general/is_active', Mage::app()->getStore())) {
307
+ $this->logToFile('Extension INACTIVE - Quitting...');
308
+ return;
309
+ }
310
+
311
  /**
312
  * Can be of any product type, e.g. configurable, grouped, simple,
313
  * @var $order Mage_Catalog_Model_Product
331
  * IMAGE_TYPE_BASE_IMAGE for e.g. Amazon Listing to work since it
332
  * requires a base image.
333
  *
334
+ * Options are taken from system config source view.
335
  */
336
+ $requiredChildProductImageTypes = array();
337
+ $requiredChildProductImageTypeValues = explode(',', Mage::getStoreConfig('bothinteract_configurableproductvariantsimageassignment/general/required_image_types', Mage::app()->getStore()));
338
+
339
+ foreach ($requiredChildProductImageTypeValues as $requiredChildProductImageTypeValue) {
340
+ $val = $this->valueToImageType($requiredChildProductImageTypeValue);
341
+
342
+ if ($val) {
343
+ $requiredChildProductImageTypes[] = $val;
344
+ }
345
+ }
346
 
347
  if ($product->getTypeId() === Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) {
348
  $this->handleSimpleProduct($product, $requiredChildProductImageTypes);
app/code/community/BothInteract/ConfigurableProductVariantsImageAssignment/Model/System/Config/Source/View.php ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @author Matthias Kerstner <matthias@both-interact.com>
5
+ * @version 1.0.0
6
+ * @copyright (c) 2014, Both Interact GmbH
7
+ */
8
+ class BothInteract_ConfigurableProductVariantsImageAssignment_Model_System_Config_Source_View {
9
+
10
+ public static $VALUE_IMAGE_TYPE_BASE_IMAGE = 0;
11
+ public static $VALUE_IMAGE_TYPE_SMALL_IMAGE = 1;
12
+ public static $VALUE_IMAGE_TYPE_THUMBNAIL = 2;
13
+
14
+ /**
15
+ * Options getter
16
+ *
17
+ * @return array
18
+ */
19
+ public function toOptionArray() {
20
+ return array(
21
+ array('value' => self::$VALUE_IMAGE_TYPE_BASE_IMAGE, 'label' => Mage::helper('adminhtml')->__('Base image')),
22
+ array('value' => self::$VALUE_IMAGE_TYPE_SMALL_IMAGE, 'label' => Mage::helper('adminhtml')->__('Small image')),
23
+ array('value' => self::$VALUE_IMAGE_TYPE_THUMBNAIL, 'label' => Mage::helper('adminhtml')->__('Thumbnail')),
24
+ );
25
+ }
26
+
27
+ /**
28
+ * Get options in "key-value" format
29
+ *
30
+ * @return array
31
+ */
32
+ public function toArray() {
33
+ return array(
34
+ self::$VALUE_IMAGE_TYPE_BASE_IMAGE => Mage::helper('adminhtml')->__('Base image'),
35
+ self::$VALUE_IMAGE_TYPE_SMALL_IMAGE => Mage::helper('adminhtml')->__('Small image'),
36
+ self::$VALUE_IMAGE_TYPE_THUMBNAIL => Mage::helper('adminhtml')->__('Thumbnail')
37
+ );
38
+ }
39
+
40
+ }
app/code/community/BothInteract/ConfigurableProductVariantsImageAssignment/etc/adminhtml.xml ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" ?>
2
+ <config>
3
+ <acl>
4
+ <resources>
5
+ <all>
6
+ <title>Allow Everything</title>
7
+ </all>
8
+ <admin>
9
+ <children>
10
+ <system>
11
+ <children>
12
+ <config>
13
+ <children>
14
+ <bothinteract_configurableproductvariantsimageassignment translate="title" module="bothinteract_configurableproductvariantsimageassignment">
15
+ <title>General</title>
16
+ <sort_order>100</sort_order>
17
+ </bothinteract_configurableproductvariantsimageassignment>
18
+ </children>
19
+ </config>
20
+ </children>
21
+ </system>
22
+ </children>
23
+ </admin>
24
+ </resources>
25
+ </acl>
26
+ </config>
app/code/community/BothInteract/ConfigurableProductVariantsImageAssignment/etc/config.xml CHANGED
@@ -1,11 +1,13 @@
1
  <?xml version="1.0"?>
2
 
3
  <config>
 
4
  <modules>
5
  <BothInteract_ConfigurableProductVariantsImageAssignment>
6
  <version>1.0.0</version>
7
  </BothInteract_ConfigurableProductVariantsImageAssignment>
8
  </modules>
 
9
  <global>
10
  <!-- inform Magento of our new model class-->
11
  <models>
@@ -26,4 +28,15 @@
26
  </catalog_product_save_after>
27
  </events>
28
  </global>
 
 
 
 
 
 
 
 
 
 
 
29
  </config>
1
  <?xml version="1.0"?>
2
 
3
  <config>
4
+
5
  <modules>
6
  <BothInteract_ConfigurableProductVariantsImageAssignment>
7
  <version>1.0.0</version>
8
  </BothInteract_ConfigurableProductVariantsImageAssignment>
9
  </modules>
10
+
11
  <global>
12
  <!-- inform Magento of our new model class-->
13
  <models>
28
  </catalog_product_save_after>
29
  </events>
30
  </global>
31
+
32
+ <default>
33
+ <bothinteract_configurableproductvariantsimageassignment>
34
+ <general>
35
+ <is_active>0</is_active>
36
+ <is_simulation>1</is_simulation>
37
+ <required_image_types></required_image_types>
38
+ <log_file>configurableproductvariantsimageassignment.log</log_file>
39
+ </general>
40
+ </bothinteract_configurableproductvariantsimageassignment>
41
+ </default>
42
  </config>
app/code/community/BothInteract/ConfigurableProductVariantsImageAssignment/etc/system.xml ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+
3
+ <config>
4
+
5
+ <tabs>
6
+ <bothinteract translate="label">
7
+ <label>Both Interact GmbH</label>
8
+ <sort_order>100</sort_order>
9
+ </bothinteract>
10
+ </tabs>
11
+
12
+ <sections>
13
+ <bothinteract_configurableproductvariantsimageassignment translate="label" module="adminhtml">
14
+ <label>Product Variants Image Assignment</label>
15
+ <tab>bothinteract</tab>
16
+ <frontend_type>text</frontend_type>
17
+ <sort_order>1000</sort_order>
18
+ <show_in_default>1</show_in_default>
19
+ <show_in_website>1</show_in_website>
20
+ <show_in_store>1</show_in_store>
21
+
22
+ <groups>
23
+ <general translate="label">
24
+ <label>General</label>
25
+ <frontend_type>text</frontend_type>
26
+ <sort_order>50</sort_order>
27
+ <show_in_default>1</show_in_default>
28
+ <show_in_website>1</show_in_website>
29
+ <show_in_store>1</show_in_store>
30
+
31
+ <fields>
32
+ <is_active translate="label">
33
+ <label>Active</label>
34
+ <comment>
35
+ <![CDATA[Check this box to <b>activate</b> automatic image assignment for product variants.]]>
36
+ </comment>
37
+ <frontend_type>select</frontend_type>
38
+ <source_model>adminhtml/system_config_source_enabledisable</source_model>
39
+ <sort_order>1</sort_order>
40
+ <show_in_default>1</show_in_default>
41
+ <show_in_website>1</show_in_website>
42
+ <show_in_store>1</show_in_store>
43
+ </is_active>
44
+ <is_simulation translate="label">
45
+ <label>Simulate</label>
46
+ <comment>
47
+ <![CDATA[Check this box to <b>simulate</b> automatic image assignment
48
+ for product variants. Child products will not be changed.
49
+ Please refer to log for more information.]]>
50
+ </comment>
51
+ <frontend_type>select</frontend_type>
52
+ <source_model>adminhtml/system_config_source_enabledisable</source_model>
53
+ <sort_order>2</sort_order>
54
+ <show_in_default>1</show_in_default>
55
+ <show_in_website>1</show_in_website>
56
+ <show_in_store>1</show_in_store>
57
+ </is_simulation>
58
+ <required_image_types translate="label">
59
+ <label>Required Image Types</label>
60
+ <comment>
61
+ <![CDATA[Please select required image types for child products.]]>
62
+ </comment>
63
+ <frontend_type>multiselect</frontend_type>
64
+ <source_model>BothInteract_ConfigurableProductVariantsImageAssignment_Model_System_Config_Source_View</source_model>
65
+ <sort_order>3</sort_order>
66
+ <show_in_default>1</show_in_default>
67
+ <show_in_website>1</show_in_website>
68
+ <show_in_store>1</show_in_store>
69
+ </required_image_types>
70
+ <log_file translate="label">
71
+ <label>Log file</label>
72
+ <comment>
73
+ <![CDATA[Please specify a name for your custom <b>log file</b>.]]>
74
+ </comment>
75
+ <frontend_type>text</frontend_type>
76
+ <sort_order>4</sort_order>
77
+ <show_in_default>1</show_in_default>
78
+ <show_in_website>1</show_in_website>
79
+ <show_in_store>1</show_in_store>
80
+ </log_file>
81
+ </fields>
82
+ </general>
83
+ </groups>
84
+ </bothinteract_configurableproductvariantsimageassignment>
85
+ </sections>
86
+ </config>
package.xml CHANGED
@@ -1,7 +1,7 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>BothInteract_ConfigurableProductVariantsImageAssignment</name>
4
- <version>1.0.0</version>
5
  <stability>stable</stability>
6
  <license uri="http://www.opensource.org/licenses/mit-license.php">MIT License (MITL)</license>
7
  <channel>community</channel>
@@ -9,12 +9,14 @@
9
  <summary>Automatically copies custom list of required image types from configurable products to variants on save.</summary>
10
  <description>This extension automatically copies a custom list of required image types (i.e. base image, small image, thumbail) from parent configurable products to variants (i.e. associated child simple products) once parent is saved.&#xD;
11
  &#xD;
12
- Logs messages to custom log file configurableproductvariantsimageassignment.log in your log directory. This way you can easily track events handled by this extension and check for possible problems.</description>
13
- <notes>Initial public release.</notes>
 
 
14
  <authors><author><name>Matthias Kerstner</name><user>mkbothinteract</user><email>matthias@both-interact.com</email></author></authors>
15
  <date>2015-01-21</date>
16
- <time>11:32:49</time>
17
- <contents><target name="magecommunity"><dir name="BothInteract"><dir name="ConfigurableProductVariantsImageAssignment"><dir name="Model"><file name="Observer.php" hash="738936e385908a5b730e7f366f388393"/></dir><dir name="etc"><file name="config.xml" hash="401d1569864aedd91fdd70a7d4be6148"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><dir name="BothInteract_BothInteract"><dir name="ConfigurableProductVariantsImageAssignment"><file name="BothInteract_ConfigurableProductVariantsImageAssignment.xml" hash=""/></dir></dir></dir></target></contents>
18
  <compatible/>
19
  <dependencies><required><php><min>5.3.0</min><max>5.6.3</max></php></required></dependencies>
20
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>BothInteract_ConfigurableProductVariantsImageAssignment</name>
4
+ <version>1.1.0</version>
5
  <stability>stable</stability>
6
  <license uri="http://www.opensource.org/licenses/mit-license.php">MIT License (MITL)</license>
7
  <channel>community</channel>
9
  <summary>Automatically copies custom list of required image types from configurable products to variants on save.</summary>
10
  <description>This extension automatically copies a custom list of required image types (i.e. base image, small image, thumbail) from parent configurable products to variants (i.e. associated child simple products) once parent is saved.&#xD;
11
  &#xD;
12
+ Logs messages to custom log file that can be specified in the configuration options in your log directory. This way you can easily track events handled by this extension and check for possible problems.&#xD;
13
+ &#xD;
14
+ For your convenience you can also &lt;b&gt;simulate&lt;/b&gt; the process of automatically assigning your list of required image type to child product by activating the option in the configuration menu. </description>
15
+ <notes>Added configuration options in admin backend.</notes>
16
  <authors><author><name>Matthias Kerstner</name><user>mkbothinteract</user><email>matthias@both-interact.com</email></author></authors>
17
  <date>2015-01-21</date>
18
+ <time>16:25:56</time>
19
+ <contents><target name="magecommunity"><dir name="BothInteract"><dir name="ConfigurableProductVariantsImageAssignment"><dir name="Model"><file name="Observer.php" hash="0595508f7f31692191b5d5d272888553"/><dir name="System"><dir name="Config"><dir name="Source"><file name="View.php" hash="611957ad032244303e103d682f7e1b94"/></dir></dir></dir></dir><dir name="etc"><file name="config.xml" hash="262c9016cf02f07b8cf96580b0d59dde"/><file name="system.xml" hash="07c7117572dc03d239e66161f5ce79dd"/><file name="adminhtml.xml" hash="17ff98ec54bdda6275f8024ee5d5d39f"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><dir name="BothInteract_BothInteract"><dir name="ConfigurableProductVariantsImageAssignment"><file name="BothInteract_ConfigurableProductVariantsImageAssignment.xml" hash=""/></dir></dir></dir></target></contents>
20
  <compatible/>
21
  <dependencies><required><php><min>5.3.0</min><max>5.6.3</max></php></required></dependencies>
22
  </package>