Analytics.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmails.php
3 years ago
Captcha.php
1 year ago
Coupons.php
2 years ago
CustomFields.php
2 months ago
DynamicProducts.php
1 year ago
DynamicSegments.php
2 months ago
FeatureFlags.php
3 years ago
Forms.php
2 months ago
Help.php
1 year ago
ImportExport.php
2 months ago
Mailer.php
1 year ago
NewsletterLinks.php
2 months ago
NewsletterTemplates.php
2 months ago
Newsletters.php
2 months ago
Premium.php
10 months ago
RedirectResponse.php
1 year ago
Segments.php
2 months ago
SendingQueue.php
2 months ago
Services.php
6 months ago
Settings.php
2 months ago
Setup.php
1 year ago
StatisticsExport.php
3 months ago
SubscriberStats.php
1 month ago
Subscribers.php
2 months ago
Tags.php
3 years ago
UserFlags.php
2 years ago
WoocommerceProductVariations.php
2 months ago
WoocommerceSettings.php
3 years ago
index.php
3 years ago
WoocommerceProductVariations.php
77 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\API\JSON\v1; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\Endpoint as APIEndpoint; |
| 9 | use MailPoet\API\JSON\SuccessResponse; |
| 10 | use MailPoet\Config\AccessControl; |
| 11 | use MailPoet\WooCommerce\Helper; |
| 12 | use WC_Product_Variable; |
| 13 | use WC_Product_Variation; |
| 14 | |
| 15 | class WoocommerceProductVariations extends APIEndpoint { |
| 16 | /** @var Helper */ |
| 17 | private $wooHelper; |
| 18 | |
| 19 | public $permissions = [ |
| 20 | 'global' => AccessControl::PERMISSION_MANAGE_SEGMENTS, |
| 21 | ]; |
| 22 | |
| 23 | public function __construct( |
| 24 | Helper $wooHelper |
| 25 | ) { |
| 26 | $this->wooHelper = $wooHelper; |
| 27 | } |
| 28 | |
| 29 | public function getVariations(array $data = []): SuccessResponse { |
| 30 | $emptyResponse = ['product' => null, 'variations' => []]; |
| 31 | if (!$this->wooHelper->isWooCommerceActive()) { |
| 32 | return $this->successResponse($emptyResponse); |
| 33 | } |
| 34 | |
| 35 | $product = null; |
| 36 | if (isset($data['product_id'])) { |
| 37 | $candidate = $this->wooHelper->wcGetProduct((int)$data['product_id']); |
| 38 | if ($candidate instanceof WC_Product_Variable) { |
| 39 | $product = $candidate; |
| 40 | } |
| 41 | } elseif (isset($data['variation_id'])) { |
| 42 | $variation = $this->wooHelper->wcGetProduct((int)$data['variation_id']); |
| 43 | if ($variation instanceof WC_Product_Variation) { |
| 44 | $parent = $this->wooHelper->wcGetProduct($variation->get_parent_id()); |
| 45 | if ($parent instanceof WC_Product_Variable) { |
| 46 | $product = $parent; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (!$product instanceof WC_Product_Variable) { |
| 52 | return $this->successResponse($emptyResponse); |
| 53 | } |
| 54 | |
| 55 | $variations = []; |
| 56 | foreach ($product->get_children() as $variationId) { |
| 57 | $variation = $this->wooHelper->wcGetProduct($variationId); |
| 58 | if (!$variation instanceof WC_Product_Variation) { |
| 59 | continue; |
| 60 | } |
| 61 | $attributesSummary = $this->wooHelper->wcGetFormattedVariation($variation, true); |
| 62 | $variations[] = [ |
| 63 | 'id' => (string)$variation->get_id(), |
| 64 | 'name' => $attributesSummary !== '' ? $attributesSummary : $variation->get_name(), |
| 65 | ]; |
| 66 | } |
| 67 | |
| 68 | return $this->successResponse([ |
| 69 | 'product' => [ |
| 70 | 'id' => (string)$product->get_id(), |
| 71 | 'name' => $product->get_name(), |
| 72 | ], |
| 73 | 'variations' => $variations, |
| 74 | ]); |
| 75 | } |
| 76 | } |
| 77 |