get_sale_price( 'edit' ) && $product->is_on_sale( 'edit' ); } public static function is_marked( \WC_Product $product ): bool { if ( 'yes' === $product->get_meta( self::META, true, 'edit' ) ) { return true; } $parent = $product->get_parent_id() ? wc_get_product( $product->get_parent_id() ) : null; return $parent instanceof \WC_Product && 'yes' === $parent->get_meta( self::META, true, 'edit' ); } public function render_field(): void { global $product_object; woocommerce_wp_checkbox( array( 'id' => self::META, 'label' => __( 'Perishable', 'strikewise' ), 'value' => $product_object instanceof \WC_Product ? $product_object->get_meta( self::META, true, 'edit' ) : '', 'cbvalue' => 'yes', 'description' => __( 'The sale price is reduced because the goods are about to spoil or reach their best-before date (§ 11 Abs. 4 PAngV). No lowest price is required; the shop shows the reason instead.', 'strikewise' ), ) ); } /** * @param \WC_Product $product */ public function save_product( $product ): void { // phpcs:ignore WordPress.Security.NonceVerification -- WooCommerce verified the product form nonce. self::mark( $product, isset( $_POST[ self::META ] ) ); } /** * @param int $loop * @param array $variation_data * @param \WP_Post $variation */ public function render_variation_field( $loop, $variation_data, $variation ): void { $product = wc_get_product( $variation->ID ); woocommerce_wp_checkbox( array( 'id' => self::META . '_' . $loop, 'name' => self::META . '[' . $loop . ']', 'label' => __( 'Perishable: reduced because it is about to spoil or expire', 'strikewise' ), 'value' => $product ? $product->get_meta( self::META, true, 'edit' ) : '', 'cbvalue' => 'yes', 'wrapper_class' => 'form-row form-row-full', ) ); } /** * @param int $variation_id * @param int $loop */ public function save_variation( $variation_id, $loop ): void { $variation = wc_get_product( $variation_id ); if ( ! $variation ) { return; } // phpcs:ignore WordPress.Security.NonceVerification -- WooCommerce verified the variation nonce. $marked = isset( $_POST[ self::META ][ $loop ] ); $was = 'yes' === $variation->get_meta( self::META, true, 'edit' ); if ( $was !== $marked ) { self::mark( $variation, $marked ); $variation->save(); } } private static function mark( \WC_Product $product, bool $marked ): void { if ( $marked ) { $product->update_meta_data( self::META, 'yes' ); } else { $product->delete_meta_data( self::META ); } } public static function text(): string { $text = trim( (string) get_option( 'strikewise_perishable_text', '' ) ); return '' === $text ? __( 'Reduced because of a short remaining shelf life', 'strikewise' ) : $text; } /** * Settings row for the notice text, added to the display group. * * @return array */ public static function setting(): array { return array( 'title' => __( 'Perishable goods', 'strikewise' ), 'desc' => __( 'Shown instead of the lowest price for reduced products marked as perishable.', 'strikewise' ), 'id' => 'strikewise_perishable_text', 'type' => 'text', 'default' => '', 'placeholder' => __( 'Reduced because of a short remaining shelf life', 'strikewise' ), 'css' => 'min-width: 400px;', ); } }