{"id":17187876,"date":"2023-11-09T12:20:44","date_gmt":"2023-11-09T12:20:44","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/code-error-please-correct\/"},"modified":"2023-11-09T12:20:44","modified_gmt":"2023-11-09T12:20:44","slug":"code-error-please-correct","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/code-error-please-correct\/","title":{"rendered":"Code error, please correct"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In your latest version 6.0.0 you&#8217;re doing the following:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>File: \/lib\/bambora-online-classic-helper.php<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static function get_bambora_online_classic_subscription_id( $subscription ) {\n\t\t$bambora_subscription_id = $subscription-&gt;get_meta(\n\t\t\tself::BAMBORA_ONLINE_CLASSIC_SUBSCRIPTION_ID,\n\t\t\ttrue\n\t\t);\n\n\t\t\/\/For Legacy\n\t\tif ( empty( $bambora_subscription_id ) ) {\n\t\t\t$parent_order_id         = $subscription-&gt;get_parent_id();\n\t\t\t$parent_order            = wc_get_order( $parent_order_id );\n\t\t\t$bambora_subscription_id = $parent_order-&gt;get_meta(\n\t\t\t\tself::BAMBORA_ONLINE_CLASSIC_SUBSCRIPTION_ID_LEGACY,\n\t\t\t\ttrue\n\t\t\t);\n\t\t\tif ( ! empty( $bambora_subscription_id ) ) {\n\t\t\t\t\/\/Transform Legacy to new standards\n\t\t\t\t$subscription-&gt;update_meta_data(\n\t\t\t\t\tself::BAMBORA_ONLINE_CLASSIC_SUBSCRIPTION_ID,\n\t\t\t\t\t$bambora_subscription_id\n\t\t\t\t);\n\t\t\t\t$subscription-&gt;save();\n\t\t\t\t$parent_order-&gt;delete_meta_data(\n\t\t\t\t\tself::BAMBORA_ONLINE_CLASSIC_SUBSCRIPTION_ID_LEGACY\n\t\t\t\t);\n\t\t\t\t$parent_order-&gt;save();\n\t\t\t}\n\t\t}\n\n\t\treturn $bambora_subscription_id;\n\t}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On line 187 you&#8217;re assuming there&#8217;s a parent_order available, but if for some reason that parent order has been deleted, the code will throw a PHP Fatal.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The correct code would be:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static function get_bambora_online_classic_subscription_id($subscription)\n    {\n        $bambora_subscription_id = $subscription-&gt;get_meta(\n            self::BAMBORA_ONLINE_CLASSIC_SUBSCRIPTION_ID,\n            true\n        );\n\n        \/\/For Legacy\n        if (empty($bambora_subscription_id)) {\n            $parent_order_id         = $subscription-&gt;get_parent_id();\n            $parent_order            = wc_get_order($parent_order_id);\n            if($parent_order) {\n                $bambora_subscription_id = $parent_order-&gt;get_meta(\n                    self::BAMBORA_ONLINE_CLASSIC_SUBSCRIPTION_ID_LEGACY,\n                    true\n                );\n                if (! empty($bambora_subscription_id)) {\n                    \/\/Transform Legacy to new standards\n                    $subscription-&gt;update_meta_data(\n                        self::BAMBORA_ONLINE_CLASSIC_SUBSCRIPTION_ID,\n                        $bambora_subscription_id\n                    );\n                    $subscription-&gt;save();\n                    $parent_order-&gt;delete_meta_data(\n                        self::BAMBORA_ONLINE_CLASSIC_SUBSCRIPTION_ID_LEGACY\n                    );\n                    $parent_order-&gt;save();\n                }\n            }\n        }\n\n        return $bambora_subscription_id;\n    }<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-17187876","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/17187876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/17187876\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=17187876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}