css
1 year ago
js
1 year ago
class-dc-skroutz-feed-admin.php
8 months ago
class-dc-skroutz-feed-creator.php
8 months ago
class-dc-skroutz-feed-data-helper.php
8 months ago
index.php
1 year ago
mod_simplexml.php
1 year ago
class-dc-skroutz-feed-data-helper.php
765 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * A helper class with useful functions for getting product data. |
| 5 | */ |
| 6 | class Dicha_Skroutz_Feed_Data_Helper { |
| 7 | |
| 8 | private array $options; |
| 9 | private string $feed_type; |
| 10 | private int $default_qty_for_instock; |
| 11 | private array $categories_to_exclude; |
| 12 | private string $default_vat; |
| 13 | private string $availability_meta_key; |
| 14 | private string $weight_unit_for_export; |
| 15 | |
| 16 | /** |
| 17 | * Initializes the helper class. |
| 18 | * |
| 19 | * @param $options array The plugin options (user settings). |
| 20 | * @param $feed_type string The feed type. |
| 21 | */ |
| 22 | public function __construct( array $options, string $feed_type ) { |
| 23 | |
| 24 | $this->options = $options; |
| 25 | $this->feed_type = $feed_type; |
| 26 | $this->default_qty_for_instock = (int) apply_filters( 'dicha_skroutz_feed_default_qty_for_products_instock', 10, $this->feed_type ); |
| 27 | $this->categories_to_exclude = apply_filters( 'dicha_skroutz_feed_excluded_cats_from_category_tree', [], $this->feed_type ); |
| 28 | $this->default_vat = apply_filters( 'dicha_skroutz_feed_default_vat', '24', $this->feed_type ); |
| 29 | $this->availability_meta_key = apply_filters( 'dicha_skroutz_feed_availability_meta_key', 'dicha_skroutz_feed_custom_availability', $this->feed_type ); |
| 30 | $this->weight_unit_for_export = strtolower( trim( apply_filters( 'dicha_skroutz_feed_weight_unit_for_export', 'g', $this->feed_type ) ) ); |
| 31 | $this->weight_unit_for_export = in_array( $this->weight_unit_for_export, [ 'g', 'kg' ] ) ? $this->weight_unit_for_export : 'g'; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Checks if a product should be excluded from XML. |
| 37 | * |
| 38 | * @param $product WC_Product |
| 39 | * |
| 40 | * @return bool True to exclude. |
| 41 | */ |
| 42 | public function skroutz_exclude_product_from_xml( WC_Product $product ): bool { |
| 43 | |
| 44 | return (bool) apply_filters( 'dicha_skroutz_feed_exclude_product_from_xml', false, $product, $this->feed_type ); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Checks if a variation should be excluded from XML. |
| 50 | * |
| 51 | * @param $variation WC_Product_Variation |
| 52 | * @param $parent_product WC_Product_Variable |
| 53 | * |
| 54 | * @return bool True to exclude. |
| 55 | */ |
| 56 | public function skroutz_exclude_variation_from_xml( $variation, WC_Product_Variable $parent_product ): bool { |
| 57 | |
| 58 | if ( ! $variation instanceof WC_Product_Variation ) return true; |
| 59 | |
| 60 | return (bool) apply_filters( 'dicha_skroutz_feed_exclude_variation_from_xml', false, $variation, $parent_product, $this->feed_type ); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Getter for product name. |
| 66 | * |
| 67 | * @param $product WC_Product |
| 68 | * @param $name_base string|NULL |
| 69 | * @param $variation WC_Product_Variation|NULL |
| 70 | * |
| 71 | * @return string |
| 72 | */ |
| 73 | public function skroutz_get_name( WC_Product $product, ?string $name_base = NULL, $variation = NULL ): string { |
| 74 | |
| 75 | // Find default name |
| 76 | $name_base = is_string( $name_base ) ? $name_base : $product->get_name(); |
| 77 | |
| 78 | /* |
| 79 | * Filter to short-circuit product name. |
| 80 | * This is useful for building an entirely new name from scratch. |
| 81 | * If you want to make minor changes to the final title, use the filter in the end of this function. |
| 82 | * |
| 83 | * If this filter is used and anything else than NULL is returned, then this value will be returned immediately. |
| 84 | * This saves time because the rest function code will not be executed at all. |
| 85 | */ |
| 86 | $pre_product_name = apply_filters( 'dicha_skroutz_feed_custom_product_name_pre', NULL, $product, $name_base, $variation, $this->feed_type ); |
| 87 | |
| 88 | if ( NULL !== $pre_product_name ) { |
| 89 | return $pre_product_name; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | // Find attributes or custom taxonomies to add to product name |
| 94 | $term_names_to_append = []; |
| 95 | $allowed_taxonomies_in_title = array_filter( array_map( 'sanitize_title', $this->options['title_attributes'] ) ); // checked ok with greek slugs |
| 96 | $attributes = $product->get_attributes(); |
| 97 | |
| 98 | if ( ! empty( $allowed_taxonomies_in_title ) ) { |
| 99 | |
| 100 | // Get allowed attributes that are not used for variations (Variation attributes are included in $name_base already) |
| 101 | $attributes_to_add = array_filter( |
| 102 | $attributes, |
| 103 | function( $val, $key ) use( $allowed_taxonomies_in_title ) { |
| 104 | return in_array( $key, $allowed_taxonomies_in_title ) && ! $val->get_variation(); |
| 105 | }, ARRAY_FILTER_USE_BOTH ); |
| 106 | |
| 107 | // transform WC_Product_Attribute objects to WP_Term objects |
| 108 | $terms_to_add = array_map( function( $wc_attribute ) { return $wc_attribute->get_terms(); }, $attributes_to_add ); |
| 109 | |
| 110 | // Add support for custom taxonomies |
| 111 | if ( in_array( 'pa_woo__product_brand', $allowed_taxonomies_in_title ) ) { |
| 112 | // Support for native WooCommerce Brands |
| 113 | $terms_to_add = array_merge( $terms_to_add, [ 'woo__product_brand' => $this->get_wc_brands_native( $product ) ] ); |
| 114 | } |
| 115 | if ( in_array( 'pa_pwb-brand', $allowed_taxonomies_in_title ) ) { |
| 116 | // Support for Perfect Brands for WooCommerce |
| 117 | $terms_to_add = array_merge( $terms_to_add, [ 'pwb-brand' => $this->get_perfect_brands_terms( $product ) ] ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if ( ! empty( $terms_to_add ) ) { |
| 122 | |
| 123 | // Split name to meaningful words to detect attribute names. |
| 124 | // Better than comparing whole string with strpos, |
| 125 | // because some attrs have small names like "S" (Small size) and won't be added if letter S exists in some random word. |
| 126 | // Search with strpos only when attribute name has more than one word. |
| 127 | $name_base_parts = preg_split( '/[\s,()]+/', mb_strtolower( $name_base, 'UTF-8' ), -1, PREG_SPLIT_NO_EMPTY ); |
| 128 | |
| 129 | foreach ( $terms_to_add as $term_objects ) { |
| 130 | |
| 131 | foreach ( $term_objects as $term ) { |
| 132 | |
| 133 | if ( count( explode( ' ', $term->name ) ) > 1 ) { // for multi-word names -> direct search in title |
| 134 | if ( mb_stripos( $name_base, $term->name, 0, 'UTF-8' ) === false ) { |
| 135 | $term_names_to_append[] = $term->name; |
| 136 | } |
| 137 | } |
| 138 | elseif ( ! in_array( mb_strtolower( $term->name, 'UTF-8' ), $name_base_parts ) ) { |
| 139 | $term_names_to_append[] = $term->name; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | $product_name = ! empty( $term_names_to_append ) ? $name_base . ' ' . implode( ' ', $term_names_to_append ) : $name_base; |
| 146 | |
| 147 | return apply_filters( 'dicha_skroutz_feed_custom_product_name', $product_name, $product, $name_base, $allowed_taxonomies_in_title, $variation, $this->feed_type ); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * Getter for product link. |
| 153 | * |
| 154 | * @param $product WC_Product |
| 155 | * @param $remove_size_params bool |
| 156 | * |
| 157 | * @return string |
| 158 | */ |
| 159 | public function skroutz_get_url( WC_Product $product, bool $remove_size_params = false ): string { |
| 160 | |
| 161 | $link = $product->get_permalink(); |
| 162 | |
| 163 | if ( $remove_size_params && ! empty( $this->options['size'] ) ) { |
| 164 | $size_params = array_map( function( $size_slug ) { return "attribute_$size_slug"; }, $this->options['size'] ); |
| 165 | $link = remove_query_arg( $size_params, $link ); |
| 166 | } |
| 167 | |
| 168 | return apply_filters( 'dicha_skroutz_feed_custom_link', $link, $product, $this->feed_type ); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Getter for product main image URL. |
| 174 | * |
| 175 | * @param $product WC_Product |
| 176 | * |
| 177 | * @return string|WP_Error |
| 178 | */ |
| 179 | public function skroutz_get_main_image_url( WC_Product $product ) { |
| 180 | |
| 181 | $main_image = wp_get_attachment_url( $product->get_image_id() ); |
| 182 | $main_image = apply_filters( 'dicha_skroutz_feed_custom_image', $main_image, $product, $this->feed_type ); |
| 183 | |
| 184 | if ( ! $main_image ) { |
| 185 | return new WP_Error( '40', 'Προϊόν χωρίς εικόνα.' ); |
| 186 | } |
| 187 | |
| 188 | return $main_image; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * Getter for product gallery images URLs. |
| 194 | * |
| 195 | * @param $product WC_Product |
| 196 | * |
| 197 | * @return array |
| 198 | */ |
| 199 | public function skroutz_get_additional_images( WC_Product $product ): array { |
| 200 | |
| 201 | $additional_images = []; |
| 202 | $gallery_images = $product->get_gallery_image_ids(); |
| 203 | |
| 204 | if ( sizeof( $gallery_images ) > 0 ) { |
| 205 | foreach ( $gallery_images as $gallery_image_id ) { |
| 206 | $additional_images[] = wp_get_attachment_url( $gallery_image_id ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return apply_filters( 'dicha_skroutz_feed_custom_additional_images', $additional_images, $product, $this->feed_type ); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * Getter for product category tree. |
| 216 | * |
| 217 | * @param $product WC_Product |
| 218 | * |
| 219 | * @return string|WP_Error |
| 220 | */ |
| 221 | public function skroutz_get_category( WC_Product $product ) { |
| 222 | |
| 223 | /* |
| 224 | * Filter to short-circuit product category. |
| 225 | * This is useful for building an entirely new category tree from scratch. |
| 226 | * If you want to make minor changes to the final title, use the filter in the end of this function. |
| 227 | * |
| 228 | * If this filter is used and anything else than NULL is returned, then this value will be returned immediately. |
| 229 | * This saves time because the rest function code will not be executed at all. |
| 230 | */ |
| 231 | $pre_main_product_cat = apply_filters( 'dicha_skroutz_feed_custom_category_pre', NULL, $product, $this->categories_to_exclude, $this->feed_type ); |
| 232 | |
| 233 | if ( NULL !== $pre_main_product_cat ) { |
| 234 | return $pre_main_product_cat; |
| 235 | } |
| 236 | |
| 237 | $categories_trees = []; |
| 238 | |
| 239 | $parent_id = $product->get_parent_id(); |
| 240 | $id_to_search = $parent_id > 0 ? $parent_id : $product->get_id(); |
| 241 | |
| 242 | $product_cats = wp_get_post_terms( |
| 243 | $id_to_search, |
| 244 | 'product_cat', |
| 245 | [ |
| 246 | 'fields' => 'ids', |
| 247 | 'orderby' => 'none', |
| 248 | 'exclude_tree' => $this->categories_to_exclude |
| 249 | ] |
| 250 | ); |
| 251 | |
| 252 | $separator = ' > '; |
| 253 | |
| 254 | foreach ( $product_cats as $category_id ) { |
| 255 | |
| 256 | $category_parent_list = get_term_parents_list( |
| 257 | $category_id, |
| 258 | 'product_cat', |
| 259 | [ |
| 260 | 'separator' => $separator, |
| 261 | 'link' => false, |
| 262 | ] |
| 263 | ); |
| 264 | |
| 265 | if ( ! empty( $category_parent_list ) && is_string( $category_parent_list ) ) { |
| 266 | $categories_trees[ $category_id ] = trim( $category_parent_list, $separator ); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | $main_product_cat = ''; |
| 271 | $depth = 0; |
| 272 | |
| 273 | if ( ! empty( $categories_trees ) ) { |
| 274 | |
| 275 | foreach ( $categories_trees as $cat_tree ) { |
| 276 | |
| 277 | $cur_depth = count( explode( $separator, $cat_tree ) ); |
| 278 | |
| 279 | if ( $cur_depth > $depth ) { |
| 280 | $main_product_cat = $cat_tree; |
| 281 | $depth = $cur_depth; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | if ( empty( $main_product_cat ) ) { |
| 287 | return new WP_Error( '50', 'Προϊόν χωρίς κατηγορία.' ); |
| 288 | } |
| 289 | |
| 290 | return apply_filters( 'dicha_skroutz_feed_custom_category', $main_product_cat, $product, $this->categories_to_exclude, $this->feed_type ); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | /** |
| 295 | * Getter for product description. |
| 296 | * |
| 297 | * @param $product WC_Product |
| 298 | * |
| 299 | * @return string |
| 300 | */ |
| 301 | public function skroutz_get_description( WC_Product $product ): string { |
| 302 | |
| 303 | $description = $this->options['description'] === 'short' ? $product->get_short_description() : $product->get_description(); |
| 304 | $description = apply_filters( 'dicha_skroutz_feed_custom_description', $description, $product, $this->options['description'], $this->feed_type ); |
| 305 | |
| 306 | return wp_filter_nohtml_kses( $description ); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | /** |
| 311 | * Getter for product EAN code. |
| 312 | * |
| 313 | * @param $product WC_Product |
| 314 | * |
| 315 | * @return string |
| 316 | */ |
| 317 | public function skroutz_get_ean( WC_Product $product ): string { |
| 318 | |
| 319 | $ean = ''; |
| 320 | $dicha_skroutz_feed_enable_ean_field = get_option( 'dicha_skroutz_feed_enable_ean_field' ); |
| 321 | |
| 322 | if ( wc_string_to_bool( $dicha_skroutz_feed_enable_ean_field ) ) { |
| 323 | $ean = $product->get_meta( 'dicha_skroutz_feed_ean_barcode' ); |
| 324 | } |
| 325 | |
| 326 | if ( empty( $ean ) && method_exists( $product, 'get_global_unique_id' ) ) { |
| 327 | $ean = $product->get_global_unique_id(); |
| 328 | } |
| 329 | |
| 330 | return apply_filters( 'dicha_skroutz_feed_custom_ean', $ean, $product, $this->feed_type ); |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /** |
| 335 | * Getter for product MPN (SKU) code. |
| 336 | * |
| 337 | * @param $product WC_Product |
| 338 | * @param $context string Options: 'view' or 'not_inherit_from_parent'. |
| 339 | * |
| 340 | * @return string |
| 341 | */ |
| 342 | public function skroutz_get_mpn( WC_Product $product, string $context = 'view' ): string { |
| 343 | |
| 344 | $mpn = $product->get_sku( $context ); |
| 345 | |
| 346 | return apply_filters( 'dicha_skroutz_feed_custom_mpn', $mpn, $product, $context, $this->feed_type ); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | /** |
| 351 | * Getter for product price. |
| 352 | * |
| 353 | * @param $product WC_Product |
| 354 | * |
| 355 | * @return string|WP_Error |
| 356 | */ |
| 357 | public function skroutz_get_price( WC_Product $product ) { |
| 358 | |
| 359 | $global_markup = $this->options['global_markup']; |
| 360 | $product_skroutz_price = max( 0.0, $product->get_meta( 'dicha_skroutz_feed_price' ) ); |
| 361 | |
| 362 | if ( ! empty( $product_skroutz_price ) ) { |
| 363 | $price_incl_tax = wc_get_price_including_tax( $product, [ 'price' => $product_skroutz_price ] ); |
| 364 | } |
| 365 | elseif ( ! empty( $global_markup ) ) { |
| 366 | $price_incl_tax = wc_get_price_including_tax( $product ) * ( 1 + $global_markup / 100 ); |
| 367 | } |
| 368 | else { |
| 369 | $price_incl_tax = wc_get_price_including_tax( $product ); |
| 370 | } |
| 371 | |
| 372 | $price = apply_filters( 'dicha_skroutz_feed_custom_price', $price_incl_tax, $product, $this->feed_type, $global_markup ); |
| 373 | |
| 374 | if ( empty( $price ) || $price < 0 ) { |
| 375 | return new WP_Error( '30', 'Η τιμή προϊόντος πρέπει να είναι μεγαλύτερη το� |
| 376 | 0.' ); |
| 377 | } |
| 378 | |
| 379 | return wc_format_decimal( $price, 2, true ); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | /** |
| 384 | * Getter for product VAT. |
| 385 | * |
| 386 | * @param $product WC_Product |
| 387 | * |
| 388 | * @return float|string |
| 389 | */ |
| 390 | public function skroutz_get_vat( WC_Product $product ) { |
| 391 | |
| 392 | $vat = $this->default_vat; |
| 393 | |
| 394 | if ( wc_tax_enabled() ) { |
| 395 | |
| 396 | if ( $product->is_taxable() ) { |
| 397 | $tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); |
| 398 | |
| 399 | if ( $tax_rates && is_array( $tax_rates ) ) { |
| 400 | $vat = end( $tax_rates )['rate']; |
| 401 | } |
| 402 | } |
| 403 | else { |
| 404 | $vat = 0; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | return apply_filters( 'dicha_skroutz_feed_custom_vat', $vat, $product, $this->default_vat, $this->feed_type ); |
| 409 | } |
| 410 | |
| 411 | |
| 412 | /** |
| 413 | * Getter for product manufacturer. |
| 414 | * |
| 415 | * @param $product WC_Product |
| 416 | * |
| 417 | * @return string |
| 418 | */ |
| 419 | public function skroutz_get_manufacturer( WC_Product $product ): string { |
| 420 | |
| 421 | $manufacturer = 'OEM'; |
| 422 | |
| 423 | if ( ! empty( $this->options['manufacturer'] ) && is_array( $this->options['manufacturer'] ) ) { |
| 424 | |
| 425 | foreach ( $this->options['manufacturer'] as $manufacturer_attribute ) { |
| 426 | |
| 427 | if ( 'pa_woo__product_brand' === $manufacturer_attribute ) { |
| 428 | // Support for native WooCommerce Brands taxonomy |
| 429 | $manufacturer_name = implode( ', ', wp_list_pluck( $this->get_wc_brands_native( $product ), 'name' ) ); |
| 430 | } |
| 431 | elseif ( 'pa_pwb-brand' === $manufacturer_attribute ) { |
| 432 | // Support for Perfect Brands for WooCommerce |
| 433 | $manufacturer_name = implode( ', ', wp_list_pluck( $this->get_perfect_brands_terms( $product ), 'name' ) ); |
| 434 | } |
| 435 | else { |
| 436 | // Get regular attribute value |
| 437 | $manufacturer_name = $product->get_attribute( $manufacturer_attribute ); |
| 438 | } |
| 439 | |
| 440 | // keep only the value from the first non-empty attribute/custom taxonomy |
| 441 | if ( ! empty( $manufacturer_name ) ) { |
| 442 | $manufacturer = $manufacturer_name; |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | return apply_filters( 'dicha_skroutz_feed_custom_manufacturer', $manufacturer, $product, $this->options['manufacturer'], $this->feed_type ); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | /** |
| 453 | * Retrieves the native WooCommerce product brands associated with a given product. |
| 454 | * |
| 455 | * @param WC_Product $product |
| 456 | * |
| 457 | * @return array A WP_Term[] array with product brand term objects or an empty array if no brands are found or an error occurs. |
| 458 | */ |
| 459 | private function get_wc_brands_native( WC_Product $product ): array { |
| 460 | |
| 461 | $wc_brands = get_the_terms( $product->get_id(), 'product_brand' ); |
| 462 | |
| 463 | if ( empty( $wc_brands ) || is_wp_error( $wc_brands ) ) { |
| 464 | return []; |
| 465 | } |
| 466 | |
| 467 | return $wc_brands; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | /** |
| 472 | * Retrieves the terms associated with the Perfect Brands plugin 'pwb-brand' taxonomy for a given product. |
| 473 | * |
| 474 | * @param WC_Product $product The WooCommerce product object. |
| 475 | * |
| 476 | * @return array A WP_Term[] array representing the brands associated with the product, |
| 477 | * or an empty array if no brands are found or there is an error. |
| 478 | */ |
| 479 | private function get_perfect_brands_terms( WC_Product $product ): array { |
| 480 | |
| 481 | $brands = get_the_terms( $product->get_id(), 'pwb-brand' ); |
| 482 | |
| 483 | if ( empty( $brands ) || is_wp_error( $brands ) ) { |
| 484 | return []; |
| 485 | } |
| 486 | |
| 487 | return $brands; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | /** |
| 492 | * Getter for product weight. |
| 493 | * |
| 494 | * @param $product WC_Product |
| 495 | * |
| 496 | * @return float|int|string |
| 497 | */ |
| 498 | public function skroutz_get_weight( WC_Product $product ) { |
| 499 | |
| 500 | $weight = wc_get_weight( $product->get_weight(), $this->weight_unit_for_export ); |
| 501 | $weight = apply_filters( 'dicha_skroutz_feed_custom_weight', $weight, $product, $this->weight_unit_for_export, $this->feed_type ); |
| 502 | |
| 503 | if ( $weight == 0 ) return ''; |
| 504 | |
| 505 | return 'kg' === $this->weight_unit_for_export ? $weight . ' kg' : $weight; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | /** |
| 510 | * Getter for product weight. |
| 511 | * |
| 512 | * @param $product WC_Product |
| 513 | * |
| 514 | * @return float|int|string |
| 515 | */ |
| 516 | public function skroutz_get_shipping( WC_Product $product ) { |
| 517 | |
| 518 | $shipping = ''; |
| 519 | |
| 520 | if ( isset( $this->options['flat_rate'] ) && $this->options['flat_rate'] != '' ) { |
| 521 | |
| 522 | if ( ! empty( $this->options['flat_rate_free'] ) && floatval( $product->get_price() ) >= floatval( $this->options['flat_rate_free'] ) ) { |
| 523 | $shipping = 0; |
| 524 | } |
| 525 | else { |
| 526 | $shipping = wc_format_decimal( (float) $this->options['flat_rate'], 2, true ); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | return apply_filters( 'dicha_skroutz_feed_custom_shipping', $shipping, $product, $this->options['flat_rate'], $this->options['flat_rate_free'], $this->feed_type ); |
| 531 | } |
| 532 | |
| 533 | |
| 534 | /** |
| 535 | * Getter for product quantity. |
| 536 | * |
| 537 | * @param $product WC_Product |
| 538 | * |
| 539 | * @return int|WP_Error |
| 540 | */ |
| 541 | public function skroutz_get_quantity( WC_Product $product ) { |
| 542 | |
| 543 | $product_type = $product->get_type(); |
| 544 | |
| 545 | if ( 'simple' === $product_type || 'variation' === $product_type ) { |
| 546 | |
| 547 | if ( $product->is_on_backorder( 1 ) ) { |
| 548 | |
| 549 | // skip if backorders should be excluded (from settings) |
| 550 | // this really only applies to variations, because simple products are already excluded from the initial products query |
| 551 | if ( ! wc_string_to_bool( $this->options['include_backorders'] ) ) { |
| 552 | return new WP_Error( '10-2', 'Τα προϊόντα σε λίστα αναμονής (backorder) εξαιρούνται από το XML βάσει ρ� |
| 553 | θμίσεων' ); |
| 554 | } |
| 555 | |
| 556 | $quantity = apply_filters( 'dicha_skroutz_feed_default_qty_for_products_on_backorder', 5, $product, $this->feed_type ); |
| 557 | } |
| 558 | elseif ( $product->managing_stock() ) { |
| 559 | $quantity = max( $product->get_stock_quantity(), 0 ); |
| 560 | } |
| 561 | else { |
| 562 | $quantity = $product->is_in_stock() ? $this->default_qty_for_instock : 0; |
| 563 | } |
| 564 | } |
| 565 | else { |
| 566 | return new WP_Error( '90-1', 'Η μέθοδος `skroutz_get_quantity` δεν πρέπει να καλείται σε WC_Product_Variable objects' ); |
| 567 | } |
| 568 | |
| 569 | $quantity = (int) apply_filters( 'dicha_skroutz_feed_custom_quantity', $quantity, $product, $this->default_qty_for_instock, $this->feed_type ); |
| 570 | |
| 571 | return min( $quantity, 10000000 ); |
| 572 | } |
| 573 | |
| 574 | |
| 575 | /** |
| 576 | * Getter for product color. |
| 577 | * |
| 578 | * @param $product WC_Product |
| 579 | * |
| 580 | * @return string |
| 581 | */ |
| 582 | public function skroutz_get_color( WC_Product $product ): string { |
| 583 | |
| 584 | $color = ''; |
| 585 | |
| 586 | if ( ! empty( $this->options['color'] ) && is_array( $this->options['color'] ) ) { |
| 587 | |
| 588 | $product_type = $product->get_type(); |
| 589 | |
| 590 | if ( 'simple' === $product_type || 'variable' === $product_type ) { |
| 591 | |
| 592 | foreach ( $this->options['color'] as $color_attribute ) { |
| 593 | |
| 594 | $color_names = $product->get_attribute( $color_attribute ); |
| 595 | |
| 596 | if ( ! empty( $color_names ) ) { |
| 597 | // if multiple color options exist, then keep only first for skroutz |
| 598 | $color = implode( '/', array_map( 'trim', explode( ',', $color_names, 2 ) ) ); |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | elseif ( 'variation' === $product_type ) { |
| 604 | |
| 605 | $variation_colors = []; |
| 606 | |
| 607 | // if a variation has multiple size attributes, keep all of them and separate with "/" |
| 608 | foreach ( $this->options['color'] as $color_attribute ) { |
| 609 | |
| 610 | $color_value = $product->get_attribute( $color_attribute ); |
| 611 | |
| 612 | /** |
| 613 | * Mod for greek (non-english) slugs. |
| 614 | * |
| 615 | * WC_Product_Variation::get_attribute() works differently from WC_Product::get_attribute() |
| 616 | * because it returns the option slug for attributes with greek slug, instead of name. |
| 617 | * So we need to fetch the term name for display ourselves. |
| 618 | */ |
| 619 | if ( ! taxonomy_exists( sanitize_title( $color_attribute ) ) && taxonomy_exists( $color_attribute ) ) { |
| 620 | |
| 621 | $term = get_term_by( 'slug', $color_value, $color_attribute ); |
| 622 | $color_value = ! is_wp_error( $term ) && $term ? $term->name : $color_value; |
| 623 | } |
| 624 | |
| 625 | if ( ! empty( $color_value ) ) { |
| 626 | $variation_colors[] = $color_value; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | $color = implode( '/', $variation_colors ); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | return apply_filters( 'dicha_skroutz_feed_custom_color', $color, $product, $this->options['color'], $this->feed_type ); |
| 635 | } |
| 636 | |
| 637 | |
| 638 | /** |
| 639 | * Getter for product size. |
| 640 | * |
| 641 | * @param $product WC_Product |
| 642 | * |
| 643 | * @return string |
| 644 | */ |
| 645 | public function skroutz_get_size( WC_Product $product ): string { |
| 646 | |
| 647 | $size = ''; |
| 648 | |
| 649 | if ( ! empty( $this->options['size'] ) && is_array( $this->options['size'] ) ) { |
| 650 | |
| 651 | $product_type = $product->get_type(); |
| 652 | |
| 653 | if ( 'simple' === $product_type || 'variable' === $product_type ) { |
| 654 | |
| 655 | foreach ( $this->options['size'] as $size_attribute ) { |
| 656 | |
| 657 | $size_values = $product->get_attribute( $size_attribute ); |
| 658 | |
| 659 | if ( ! empty( $size_values ) ) { |
| 660 | // if multiple size options exist in simple products, then keep only first for skroutz |
| 661 | $size = explode( ',', $size_values, 2 )[0]; |
| 662 | break; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // only for simple or variable products (not variations), replace empty size with "One Size" |
| 667 | // If variations have size info, this will be replaced anyway. If they haven't, they will inherit this one, which is ideal. |
| 668 | if ( empty( $size ) && wc_string_to_bool( get_option( 'dicha_skroutz_feed_size_default_one_size' ) ) ) { |
| 669 | $size = 'One Size'; |
| 670 | } |
| 671 | } |
| 672 | elseif ( 'variation' === $product_type ) { |
| 673 | |
| 674 | $variation_sizes = []; |
| 675 | |
| 676 | // if a variation has multiple size attributes, keep all of them and separate with "/" |
| 677 | foreach ( $this->options['size'] as $size_attribute ) { |
| 678 | |
| 679 | $size_value = $product->get_attribute( $size_attribute ); |
| 680 | |
| 681 | /** |
| 682 | * Mod for greek (non-english) slugs. |
| 683 | * |
| 684 | * WC_Product_Variation::get_attribute() works differently from WC_Product::get_attribute() |
| 685 | * because it returns the option slug for attributes with greek slug, instead of name. |
| 686 | * So we need to fetch the term name for display ourselves. |
| 687 | */ |
| 688 | if ( ! taxonomy_exists( sanitize_title( $size_attribute ) ) && taxonomy_exists( $size_attribute ) ) { |
| 689 | |
| 690 | $term = get_term_by( 'slug', $size_value, $size_attribute ); |
| 691 | $size_value = ! is_wp_error( $term ) && $term ? $term->name : $size_value; |
| 692 | } |
| 693 | |
| 694 | if ( ! empty( $size_value ) ) { |
| 695 | $variation_sizes[] = $size_value; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | $size = implode( '/', $variation_sizes ); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | return apply_filters( 'dicha_skroutz_feed_custom_size', $size, $product, $this->options['size'], $this->feed_type ); |
| 704 | } |
| 705 | |
| 706 | |
| 707 | /** |
| 708 | * Getter for product availability. |
| 709 | * |
| 710 | * @param $product WC_Product |
| 711 | * |
| 712 | * @return string|WP_Error |
| 713 | */ |
| 714 | public function skroutz_get_availability( WC_Product $product ) { |
| 715 | |
| 716 | $availability_value = $product->get_meta( $this->availability_meta_key ); |
| 717 | |
| 718 | // if empty, try to get parent product's value |
| 719 | if ( empty( $availability_value ) ) { |
| 720 | |
| 721 | $parent_id = $product->get_parent_id(); |
| 722 | |
| 723 | if ( $parent_id > 0 ) { |
| 724 | $availability_value = get_post_meta( $parent_id, $this->availability_meta_key, true ); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | // if still empty, get default value from settings |
| 729 | if ( empty( $availability_value ) ) { |
| 730 | $availability_value = $this->options['xml_availability']; |
| 731 | } |
| 732 | |
| 733 | $availability_text = $this->skroutz_get_availability_text( $availability_value ); |
| 734 | |
| 735 | if ( is_wp_error( $availability_text ) ) return $availability_text; |
| 736 | |
| 737 | return apply_filters( 'dicha_skroutz_feed_custom_availability', $availability_text, $product, $availability_value, $this->options['xml_availability'], $this->feed_type ); |
| 738 | } |
| 739 | |
| 740 | |
| 741 | /** |
| 742 | * Get availability display text based on availability value. |
| 743 | * |
| 744 | * @param $availability_value string |
| 745 | * |
| 746 | * @return string|WP_Error |
| 747 | */ |
| 748 | public function skroutz_get_availability_text( string $availability_value ) { |
| 749 | |
| 750 | $availability_options = Dicha_Skroutz_Feed_Admin::skroutz_get_availability_options(); |
| 751 | $availability_text = $availability_options[ $availability_value ] ?? ''; |
| 752 | |
| 753 | if ( empty( $availability_text ) ) { |
| 754 | return new WP_Error( '20', 'Μη έγκ� |
| 755 | ρη τιμή διαθεσιμότητας' ); |
| 756 | } |
| 757 | elseif( '5' === $availability_value ) { |
| 758 | return new WP_Error( '60', 'Απόκρ� |
| 759 | ψη από το XML λόγω ρύθμισης' ); |
| 760 | } |
| 761 | |
| 762 | return $availability_text; |
| 763 | } |
| 764 | |
| 765 | } |