Setting custom user-defined fields on a subscription


Magento 1 product user defined fields As of December 12, 2015, Subscribe Pro now allows you to set custom "user-defined" fields on a subscription. These are key-value pairs that can contain arbitrary data set by Magento when a new subscription order is placed. The data is stored in Subscribe Pro with that subscription record, and is sent back to Magento on each recurring order. This can be useful for a lot of things, such as delivery instructions, product color preferences, etc.

Setting these fields will require some custom code on the Magento side, utilizing the sfc_autoship_before_create_subscription_from_quote_item event. They can then be accessed from the subscription upon reorder and applied to Magento's fields using the sfc_autoship_before_subscription_reorder_place event.

As an example, the attached archive contains a module that can be uploaded directly to your Magento installation and will not overwrite anything. The relevant code is in the Observer.php file. The first method is where the fields are set. In this extension, these fields are static values, but they can be changed to use user-inputted information or something dynamically pulled from the database. The second method shows how to retrieve the fields for use when the order is being placed from Subscribe Pro.

Note: In extension versions up through 1.3.2.8, two small edits must be made to the app/code/local/SFC/Autoship/Helper/Platform.php file, as shown in the following diff:

diff --git a/app/code/local/SFC/Autoship/Helper/Platform.php b/app/code/local/SFC/Autoship/Helper/Platform.php
index b66f1cd..185e1fa 100755
--- a/app/code/local/SFC/Autoship/Helper/Platform.php
+++ b/app/code/local/SFC/Autoship/Helper/Platform.php
@@ -721,6 +721,11 @@ class SFC_Autoship_Helper_Platform extends Mage_Core_Helper_Abstract
             $platformSubscription['subscription_products'][0]['magento_additional_options'] = $additionalOptions;
         }
+        // If user defined fields are set, set on platform
+        if ($subscription->hasData('user_defined_fields')) {
+            $platformSubscription['user_defined_fields'] = $subscription->getData('user_defined_fields');
+        }
+
         // Add payment details
         // New, multi-gateway aware method
         $platformSubscription['payment_profile'] = array(
@@ -939,6 +944,10 @@ class SFC_Autoship_Helper_Platform extends Mage_Core_Helper_Abstract
         $subscription->setData('creditcard_last_digits', $platformSubscription['payment_profile']['creditcard_last_digits']);
         $subscription->setData('customer_cardnumber', $platformSubscription['payment_profile']['creditcard_last_digits']);
+        // Set user-defined fields
+        $user_defined_fields = isset($platformSubscription['user_defined_fields']) ? $platformSubscription['user_defined_fields'] : array();
+        $subscription->setData('user_defined_fields', $user_defined_fields);
+
         return $subscription;
     }