get_parent_id() ? $product->get_parent_id() : $product->get_id(); foreach ( self::BRAND_TAXONOMIES as $tax ) { if ( taxonomy_exists( $tax ) ) { $terms = get_the_terms( $id, $tax ); if ( $terms && ! is_wp_error( $terms ) ) { return $terms[0]->name; } } } $meta = (string) get_post_meta( $id, '_prodlyra_brand', true ); if ( '' !== $meta ) { return $meta; } foreach ( array( 'pa_brand', 'pa_marque', 'brand', 'marque' ) as $attribute ) { $value = $product->get_attribute( $attribute ); if ( '' !== $value ) { $parts = explode( ',', $value ); return trim( $parts[0] ); } } return $fallback ? Prodlyra_Settings::get()['default_brand'] : ''; } /** * Valid GTIN (8, 12, 13 or 14 digits) or an empty string. * * @param WC_Product $product Product. * @return string */ public static function gtin( $product ) { $value = ''; if ( self::has_native_gtin() ) { $value = (string) $product->get_global_unique_id(); } if ( '' === $value ) { $value = (string) get_post_meta( $product->get_id(), '_prodlyra_gtin', true ); } $value = preg_replace( '/\D/', '', $value ); return in_array( strlen( $value ), array( 8, 12, 13, 14 ), true ) ? $value : ''; } /** * Plain-text description (long, or short when the long one is empty). * * @param WC_Product $product Product. * @return string */ public static function description( $product ) { $text = $product->get_description(); if ( '' === trim( wp_strip_all_tags( $text ) ) ) { $text = $product->get_short_description(); } return trim( preg_replace( '/\s+/', ' ', wp_strip_all_tags( strip_shortcodes( $text ) ) ) ); } /** * schema.org OfferShippingDetails built from the settings, or null when not configured. * * @return array|null */ public static function shipping_details() { if ( ! Prodlyra_Settings::shipping_configured() ) { return null; } $s = Prodlyra_Settings::get(); $currency = get_woocommerce_currency(); $details = array(); foreach ( Prodlyra_Settings::countries() as $country ) { $details[] = array( '@type' => 'OfferShippingDetails', 'shippingRate' => array( '@type' => 'MonetaryAmount', 'value' => (float) $s['ship_cost'], 'currency' => $currency, ), 'shippingDestination' => array( '@type' => 'DefinedRegion', 'addressCountry' => $country, ), 'deliveryTime' => array( '@type' => 'ShippingDeliveryTime', 'handlingTime' => self::days( $s['handling_min'], $s['handling_max'] ), 'transitTime' => self::days( $s['transit_min'], $s['transit_max'] ), ), ); } if ( ! $details ) { return null; } return 1 === count( $details ) ? $details[0] : $details; } /** * schema.org MerchantReturnPolicy built from the settings, or null when not configured. * * @return array|null */ public static function return_policy() { if ( ! Prodlyra_Settings::returns_configured() ) { return null; } $s = Prodlyra_Settings::get(); $countries = Prodlyra_Settings::countries(); $policy = array( '@type' => 'MerchantReturnPolicy', 'applicableCountry' => 1 === count( $countries ) ? $countries[0] : $countries, ); $days = (int) $s['return_days']; if ( $days <= 0 ) { $policy['returnPolicyCategory'] = 'https://schema.org/MerchantReturnNotPermitted'; return $policy; } $policy['returnPolicyCategory'] = 'https://schema.org/MerchantReturnFiniteReturnWindow'; $policy['merchantReturnDays'] = $days; $policy['returnMethod'] = 'https://schema.org/ReturnByMail'; $policy['returnFees'] = 'free' === $s['return_fees'] ? 'https://schema.org/FreeReturn' : 'https://schema.org/ReturnFeesCustomerResponsibility'; return $policy; } /** * A range of days as schema.org QuantitativeValue. * * @param int $min Minimum days. * @param int $max Maximum days. * @return array */ private static function days( $min, $max ) { return array( '@type' => 'QuantitativeValue', 'minValue' => (int) $min, 'maxValue' => (int) $max, 'unitCode' => 'DAY', ); } }