offers??null; } public function setOffers(Demand|Offer|array $offers):void { if (is_array($offers)) { $offers = $this->mixedArray('offers', $offers,[ 'JVBase\managers\SEO\render\Thing\Intangible\Demand', 'JVBase\managers\SEO\render\Thing\Intangible\Offer' ]); } $this->offers = $offers; } public function setOffersField(Fields $fields):void { $fields->addField('generate_offers_from_content', [ 'type' => 'true_false', 'label' => __('Generate Offers from Content', 'jvb'), ]); $fields->addField('seo_offers', [ 'type' => 'repeater', 'label' => __('Offers', 'jvb'), 'condition' => [ 'field' => 'generate_offers_from_content', 'value' => true, 'operator' => '==' ], 'fields' => [ 'type' => [ 'type' => 'radio', 'label' => __('Select Type', 'jvb'), 'options' => [ 'product' => 'Product', 'service' => 'Service' ], 'required' => true, ], 'name' => [ 'type' => 'text', 'label' => __('Name of Product or Service', 'jvb'), 'required' => true, ], 'description' => [ 'type' => 'textarea', 'label' => __('Description (optional)', 'jvb'), ], 'price' => [ 'type' => 'text', 'subtype' => 'number', 'label' => __('Price', 'jvb'), ], 'unitText' => [ 'type' => 'radio', 'label' => 'Price per unit', 'options' => [ 'hour' => 'Hour', 'unit' => 'Unit', ], 'default' => 'unit', ] ], ]); } public function formatOffersField(Meta $meta):void { $generate = $meta->get('generate_offers_from_content'); if (!empty($generate) && $generate === true) { $this->generateOffersFromContent($meta); return; } $catalog = $meta->get('seo_offers'); if (!empty($catalog)) { $offerCatalog = []; $name = ''; $services = array_filter($catalog, function ($item) { return $item['type'] === 'service'; }); $products =array_filter($catalog, function ($item) { return $item['type'] === 'product'; }); if (count($products) > 0) { $name = 'Products'; if (count($services) > 0) { $name .= ' & '; } } if (count($services) > 0) { $name .= 'Services'; } foreach ($catalog as $row) { $offer = new Offer(); $item = match($row['type']) { 'product' => new Product(), 'service' => new Service(), }; $item->setName($row['name']); if (!empty($row['description'])) { $item->setDescription($row['description']); } if (!empty($row['price'])) { $price = new PriceSpecification(); $price->setPrice($row['price']); $price->setPriceCurrency('CAD'); $price->setUnitText($row['unitText']); $offer->setPriceSpecification($price); } $offer->setItemOffered($item); $offerCatalog[] = $offer; } if (!empty(!$offerCatalog)) { $this->setOffers($offerCatalog); } } } /** * This presumes that the Meta instance being passed is for a profile content type. * @param Meta $meta * @return void */ public function generateOffersFromContent(Meta $meta):void { $userID = (int)get_post_meta($meta->id(), BASE.'profile_link', true); if (empty($userID)) { return; } $user = get_userdata($userID); if (!$user) { return; } $registrar = Registrar::getInstance(jvbUserRole($userID)); if (!$registrar) { return; } $types = $registrar->getCreatable(); if (empty($types)) { return; } $offers = []; $cities = $meta->get('city'); $rate = $meta->get('rate'); $reviews = $meta->get('reviews'); $rating = $meta->get('average_rating'); $theCities = $theRate = $theReviews = $theRating = null; foreach($types as $type) { $based = jvbCheckBase($type); $hasAny = new WP_Query([ 'post_type' => $based, 'posts_per_page'=> 1, 'fields' => 'ids', 'post_author' => $userID ]); if ($hasAny->have_posts()) { $offer = new Offer(); $typeRegistrar = Registrar::getInstance($type); } } } }