y_times( $context = 'view' ) { if ( is_null( $this->delivery_times ) ) { $slugs = $this->get_delivery_time_slugs( $context ); $cached_terms = array(); foreach ( $slugs as $slug ) { $term = WC_germanized()->delivery_times->get_delivery_time_term( $slug ); if ( ! $term ) { continue; } $cached_terms[ $term->slug ] = $term; } $this->delivery_times = apply_filters( 'woocommerce_gzd_product_delivery_times', $cached_terms, $this, $this->child, $context ); } return $this->delivery_times; } public function get_delivery_time_slugs( $context = 'view' ) { /** * Normally (view context) we are using the term relationship model to retrieve * the delivery times mapped to the product. While saving we are using the props model * to enable saving the current object state. */ if ( 'save' === $context || $this->delivery_times_need_update() ) { $slugs = false; if ( $this->delivery_times_need_update() ) { $slugs = array(); $default_slug = $this->get_default_delivery_time_slug( 'save' ); $country_specific = array_values( array_unique( $this->get_country_specific_delivery_times( 'save' ) ) ); if ( ! empty( $default_slug ) ) { $slugs = array_merge( array( $default_slug ), $slugs ); } if ( ! empty( $country_specific ) ) { $slugs = array_merge( $country_specific, $slugs ); } $slugs = array_unique( $slugs ); } return $slugs; } else { $object_id = $this->get_wc_product()->get_id(); $terms = get_the_terms( $object_id, 'product_delivery_time' ); if ( false === $terms || is_wp_error( $terms ) ) { return array(); } return wp_list_pluck( $terms, 'slug' ); } } protected function set_delivery_time_slugs( $slugs ) { $slugs = wc_gzd_get_valid_product_delivery_time_slugs( $slugs ); $this->set_prop( 'delivery_time_slugs', array_unique( array_map( 'sanitize_title', $slugs ) ) ); $this->delivery_times = null; } public function delivery_times_need_update() { return $this->delivery_times_need_update; } public function set_delivery_times_need_update( $need_update = true ) { $this->delivery_times_need_update = $need_update; } public function set_default_delivery_time_slug( $slug ) { $slug = wc_gzd_get_valid_product_delivery_time_slugs( $slug ); $current = $this->get_default_delivery_time_slug(); $this->set_prop( 'default_delivery_time', $slug ); if ( $current !== $slug ) { $this->set_delivery_times_need_update(); } } protected function get_current_customer_shipping_country() { $country = false; if ( ( is_cart() || is_checkout() ) && WC()->cart && WC()->cart->get_customer() ) { $country = '' === WC()->cart->get_customer()->get_shipping_country() ? WC()->cart->get_customer()->get_billing_country() : WC()->cart->get_customer()->get_shipping_country(); } elseif ( wc_gzd_is_admin_order_request() ) { if ( isset( $_POST['order_id'] ) && ( $order = wc_get_order( absint( $_POST['order_id'] ) ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing if ( is_callable( array( $order, 'get_shipping_country' ) ) ) { $country = '' === $order->get_shipping_country() ? $order->get_billing_country() : $order->get_shipping_country(); } } } elseif ( WC()->customer ) { $country = '' === WC()->customer->get_shipping_country() ? WC()->customer->get_billing_country() : WC()->customer->get_shipping_country(); } elseif ( 'base' === get_option( 'woocommerce_default_customer_address' ) ) { $country = wc_gzd_get_base_country(); } return empty( $country ) ? false : $country; } /** * Returns the current products delivery time term without falling back to default term * * @return false|WP_Term false returns false if term does not exist otherwise returns term object */ public function get_delivery_time( $context = 'view' ) { /** * Use the edit context to disable global setting fallback */ $delivery_time = $this->get_default_delivery_time( $context ); if ( 'view' === $context ) { if ( $country = $this->get_current_customer_shipping_country() ) { $delivery_time_country = $this->get_delivery_time_by_country( $country ); if ( $delivery_time_country ) { $delivery_time = $delivery_time_country; } } } return $delivery_time; } public function get_default_delivery_time_slug( $context = 'view' ) { return $this->get_prop( 'default_delivery_time', $context ); } public function get_gzd_version( $context = 'view' ) { return $this->get_prop( 'gzd_version', $context ); } /** * @param string $context * * @return false|WP_Term */ public function get_default_delivery_time( $context = 'view' ) { $default_slug = $this->get_default_delivery_time_slug( $context ); $times = $this->get_delivery_times( $context ); $delivery_time = false; /** * In case of older Germanized version which did not support multiple delivery times per product (e.g. per country) * the default delivery time matches the first (only) delivery time set for the product. * * Newer versions include a separate meta field (_default_delivery_time) to indicate the default delivery time. */ if ( ! empty( $default_slug ) && array_key_exists( $default_slug, $times ) ) { $delivery_time = $times[ $default_slug ]; } elseif ( ( empty( $this->get_gzd_version() ) || version_compare( $this->get_gzd_version(), '3.7.0', '<' ) ) && ! empty( $times ) ) { $delivery_time = array_values( $times )[0]; } /** * Use a global default delivery time from settings as a fallback in case no default delivery time was selected for this product. */ if ( 'view' === $context && ( empty( $delivery_time ) && ! $this->is_downloadable() ) ) { $eu_countries = WC()->countries->get_european_union_countries(); $base_country = wc_gzd_get_base_country(); $delivery_time = false; $default_option = false; if ( ( $country = $this->get_current_customer_shipping_country() ) && $base_country !== $country ) { if ( in_array( $country, $eu_countries, true ) ) { $default_option = get_option( 'woocommerce_gzd_default_delivery_time_eu' ); } elseif ( ! in_array( $country, $eu_countries, true ) ) { $default_option = get_option( 'woocommerce_gzd_default_delivery_time_third_countries' ); } if ( $default_option ) { $delivery_time = WC_germanized()->delivery_times->get_delivery_time_term( $default_option, 'slug_fallback' ); } } if ( ! $delivery_time && get_option( 'woocommerce_gzd_default_delivery_time' ) ) { $default_option = get_option( 'woocommerce_gzd_default_delivery_time' ); $delivery_time = WC_germanized()->delivery_times->get_delivery_time_term( $default_option, 'slug_fallback' ); } } return $delivery_time; } public function get_country_specific_delivery_times( $context = 'view' ) { $countries = $this->get_prop( 'delivery_time_countries', $context ); $countries = ( ! is_array( $countries ) || empty( $countries ) ) ? array() : $countries; ksort( $countries ); return $countries; } public function set_gzd_version( $version ) { $this->set_prop( 'gzd_version', $version ); } protected function is_valid_country_specific_delivery_time( $slug, $country ) { $default_slug = $this->get_default_delivery_time_slug( 'edit' ); if ( $slug === $default_slug || wc_gzd_get_base_country() === $country ) { return false; } return true; } public function set_country_specific_delivery_times( $terms ) { $current = $this->get_country_specific_delivery_times(); $terms = wc_gzd_get_valid_product_delivery_time_slugs( $terms ); foreach ( $terms as $country => $slug ) { if ( ! $this->is_valid_country_specific_delivery_time( $slug, $country ) ) { unset( $terms[ $country ] ); } } ksort( $terms ); $this->set_prop( 'delivery_time_countries', $terms ); $this->delivery_times = null; if ( $current !== $terms ) { $this->set_delivery_times_need_update(); } } public function get_delivery_time_by_country( $country = '', $context = 'view' ) { $countries = $this->get_country_specific_delivery_times( $context ); $times = $this->get_delivery_times( $context ); $delivery_time = false; $eu_countries = WC()->countries->get_european_union_countries(); $base_country = wc_gzd_get_base_country(); $delivery_time_slug = false; /** * EU-wide delivery times in case target country does not match base country */ if ( in_array( $country, $eu_countries, true ) && $base_country !== $country && array_key_exists( 'EU-wide', $countries ) ) { $delivery_time_slug = $countries['EU-wide']; } /** * Non-EU-wide delivery times in case target country does not match base country */ if ( ! in_array( $country, $eu_countries, true ) && $base_country !== $country && array_key_exists( 'Non-EU-wide', $countries ) ) { $delivery_time_slug = $countries['Non-EU-wide']; } /** * Allow overriding by custom country rules */ if ( array_key_exists( $country, $countries ) ) { $delivery_time_slug = $countries[ $country ]; } /** * Make sure delivery time is related to product */ if ( $delivery_time_slug && array_key_exists( $delivery_time_slug, $times ) ) { $delivery_time = $times[ $delivery_time_slug ]; } if ( 'view' === $context && ! $delivery_time ) { $delivery_time = $this->get_default_delivery_time( $context ); } return $delivery_time; } /** * Returns current product's delivery time term. If none has been set and a default delivery time has been set, returns that instead. * * @return WP_Term|false */ public function get_delivery_time_term( $context = 'view' ) { $delivery_time = $this->get_delivery_time( $context ); return ( ! is_wp_error( $delivery_time ) && ! empty( $delivery_time ) ) ? $delivery_time : false; } public function get_delivery_time_name( $context = 'view' ) { if ( $term = $this->get_delivery_time( $context ) ) { return $term->name; } return ''; } /** * Returns the delivery time html output * * @return string */ public function get_delivery_time_html( $context = 'view' ) { $html = ''; /** * Filter that allows hiding the delivery time for a specific product. * * @param bool $hide Whether to hide delivery time or not. * @param WC_GZD_Product $product The product object. * * @since 1.0.0 * */ if ( apply_filters( 'woocommerce_germanized_hide_delivery_time_text', false, $this ) ) { /** * Filter to adjust disabled product delivery time output. * * @param string $output The output. * @param WC_GZD_Product $product The product object. * * @since 1.0.0 * */ return apply_filters( 'woocommerce_germanized_disabled_delivery_time_text', '', $this ); } elseif ( $this->hide_shopmarks_due_to_missing_price() ) { return ''; } if ( $this->get_delivery_time( $context ) ) { $html = $this->get_delivery_time_name( $context ); } else { /** * Filter to adjust empty delivery time text. * * @param string $text The delivery time text. * @param WC_GZD_Product $product The product object. * * @since 1.0.0 * */ $html = apply_filters( 'woocommerce_germanized_empty_delivery_time_text', '', $this ); } if ( ! empty( $html ) ) { $delivery_time_str = get_option( 'woocommerce_gzd_delivery_time_text' ); $replacements = array( '{delivery_time}' => $html, ); if ( strstr( $delivery_time_str, '{stock_status}' ) ) { $replacements['{stock_status}'] = str_replace( array( '

' ), array( '' ), wc_get_stock_html( $this->child ) ); } /** * Filter to adjust product delivery time HTML. * * @param string $html The notice. * @param string $option The placeholder option. * @param string $html_org The HTML before replacement. * @param WC_GZD_Product $product The product object. * * @since 1.0.0 * */ $html = apply_filters( 'woocommerce_germanized_delivery_time_html', wc_gzd_replace_label_shortcodes( $delivery_time_str, $replacements ), $delivery_time_str, $html, $this ); } else { $html = ''; } // Hide delivery time if product is not in stock if ( 'yes' === get_option( 'woocommerce_gzd_delivery_time_disable_not_in_stock' ) && ! $this->child->is_in_stock() ) { /** * Filter to adjust product delivery time in case of a product is out of stock. * * @param string $output The new delivery time text. * @param WC_GZD_Product $product The product object. * @param string $html The original HTML output. * * @since 2.0.0 * */ $html = apply_filters( 'woocommerce_germanized_delivery_time_out_of_stock_html', '', $this, $html ); } elseif ( 'yes' === get_option( 'woocommerce_gzd_delivery_time_disable_backorder' ) && $this->child->is_on_backorder() ) { /** * Filter to adjust product delivery time in case of a product is on backorder. * * @param string $output The new delivery time text. * @param WC_GZD_Product $product The product object. * @param string $html The original HTML output. * * @since 2.0.0 * */ $html = apply_filters( 'woocommerce_germanized_delivery_time_backorder_html', '', $this, $html ); } /** * Filter to adjust product delivery time html output. * * @param string $html The delivery time html. * @param WC_GZD_Product $product The product object. * * @since 3.1.12 */ return apply_filters( 'woocommerce_gzd_product_delivery_time_html', $html, $this ); } /** * Returns the defect description html output * * @return string */ public function get_formatted_defect_description( $context = 'view' ) { if ( $this->is_defective_copy( $context ) ) { return apply_filters( 'woocommerce_gzd_defect_description', wpautop( do_shortcode( wp_kses_post( htmlspecialchars_decode( $this->get_defect_description( $context ) ) ) ) ) ); } return ''; } /** * Returns the shipping costs notice html output * * @return string */ public function get_shipping_costs_html() { /** * Filter to optionally disable shipping costs info for a certain product. * * @param bool $disable Whether to disable the shipping costs notice or not. * @param WC_GZD_Product $product The product object. * * @since 1.0.0 */ if ( apply_filters( 'woocommerce_germanized_hide_shipping_costs_text', false, $this ) ) { /** * Filter to adjust a product's disabled shipping costs notice. * * @param string $output The output. * @param WC_GZD_Product $product The product object. * * @since 1.0.0 * */ return apply_filters( 'woocommerce_germanized_disabled_shipping_text', '', $this ); } if ( $this->hide_shopmarks_due_to_missing_price() ) { return ''; } return wc_gzd_get_shipping_costs_text( $this ); } public function save() { /** * Update delivery time term slugs if they have been explicitly set during the * save request. */ $slugs = $this->get_delivery_time_slugs( 'save' ); $id = false; if ( false !== $slugs ) { $this->set_delivery_times_need_update( false ); $id = $this->child->save(); } if ( false !== $slugs && $id ) { $slugs = array_unique( array_map( 'sanitize_title', $slugs ) ); if ( empty( $slugs ) ) { wp_delete_object_term_relationships( $id, 'product_delivery_time' ); } else { wp_set_post_terms( $id, $slugs, 'product_delivery_time', false ); } $this->delivery_times = null; } /** * Update deposit type term relationships */ if ( $deposit_type = $this->get_deposit_type_term( 'edit' ) ) { wp_set_post_terms( $this->get_wc_product()->get_id(), array( $deposit_type->slug ), 'product_deposit_type', false ); } else { wp_delete_object_term_relationships( $this->get_wc_product()->get_id(), 'product_deposit_type' ); } } } Angebote - Seite 2 von 2 - felicitasProtect SHOP