File tree Expand file tree Collapse file tree 3 files changed +44
-5
lines changed
src/BigCommerce/ResourceModels/Redirect
tests/BigCommerce/Api/Redirects Expand file tree Collapse file tree 3 files changed +44
-5
lines changed Original file line number Diff line number Diff line change 55use BigCommerce \ApiV3 \ResourceModels \ResourceModel ;
66use stdClass ;
77
8+ /**
9+ * Redirect Resource
10+ *
11+ * Represents the request object for redirect objects.
12+ *
13+ * Contains a convenience method for simply redirecting to a product or category
14+ *
15+ * ```php
16+ * $api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']);
17+ *
18+ * $productRedirect = new Redirect();
19+ * $productRedirect->site_id = 1000;
20+ * $productRedirect->from_path = '/cool-product.html';
21+ * $productRedirect->toProduct(123);
22+ *
23+ * $api->redirects()->upsert([$productRedirect]);
24+ * ```
25+ *
26+ */
827class Redirect extends ResourceModel
928{
1029 public string $ from_path ;
@@ -20,4 +39,14 @@ public function __construct(?stdClass $optionObject = null)
2039
2140 parent ::__construct ($ optionObject );
2241 }
42+
43+ public function toProduct (int $ productId ): void
44+ {
45+ $ this ->to = RedirectTo::buildRedirectTo (RedirectTo::TYPE__PRODUCT , $ productId );
46+ }
47+
48+ public function toCategory (int $ categoryId ): void
49+ {
50+ $ this ->to = RedirectTo::buildRedirectTo (RedirectTo::TYPE__CATEGORY , $ categoryId );
51+ }
2352}
Original file line number Diff line number Diff line change @@ -15,5 +15,15 @@ class RedirectTo extends ResourceModel
1515
1616 public string $ type ;
1717 public ?int $ entity_id ;
18- public string $ url ;
18+ public ?string $ url ;
19+
20+ public static function buildRedirectTo (string $ type , ?int $ entityId = null , ?string $ url = null ): RedirectTo
21+ {
22+ $ redirectTo = new self ();
23+ $ redirectTo ->type = $ type ;
24+ $ redirectTo ->entity_id = $ entityId ;
25+ $ redirectTo ->url = $ url ;
26+
27+ return $ redirectTo ;
28+ }
1929}
Original file line number Diff line number Diff line change @@ -14,10 +14,10 @@ public function testCanUpsertRedirects()
1414 $ redirect = new Redirect ();
1515 $ redirect ->from_path = '/old-url ' ;
1616 $ redirect ->site_id = 0 ;
17- $ redirect ->to = new RedirectTo ( );
18- $ redirect -> to -> type = RedirectTo:: TYPE__PRODUCT ;
19- $ redirect ->to ->entity_id = 0 ;
20- $ redirect ->to ->url = ' /new-url ' ;
17+ $ redirect ->toProduct ( 1 );
18+
19+ $ this -> assertEquals (RedirectTo:: TYPE__PRODUCT , $ redirect ->to ->type ) ;
20+ $ this -> assertEquals ( 1 , $ redirect ->to ->entity_id ) ;
2121
2222 $ this ->getApi ()->redirects ()->upsert ([$ redirect ]);
2323 $ this ->assertEquals ('storefront/redirects ' , $ this ->getLastRequestPath ());
You can’t perform that action at this time.
0 commit comments