create_product( $base_name, true ); update_option( 'aisp_otools_custom_item_taxable_id', $new_id ); } if ( ! $notax_id || ! get_post_status( $notax_id ) ) { $new_id = $this->create_product( $base_name, false ); update_option( 'aisp_otools_custom_item_notax_id', $new_id ); } } public function toggle_handler( $old, $new, $option ) { $taxable_id = (int) get_option( 'aisp_otools_custom_item_taxable_id', 0 ); $notax_id = (int) get_option( 'aisp_otools_custom_item_notax_id', 0 ); if ( $new === 'no' ) { $GLOBALS['aisp_otools_allow_delete'] = true; if ( $taxable_id ) { wp_delete_post( $taxable_id, true ); } if ( $notax_id ) { wp_delete_post( $notax_id, true ); } delete_option( 'aisp_otools_custom_item_taxable_id' ); delete_option( 'aisp_otools_custom_item_notax_id' ); unset( $GLOBALS['aisp_otools_allow_delete'] ); return; } if ( $new === 'yes' ) { $this->maybe_setup_products(); } } public function rename_products( $old, $new, $option ) { $taxable_id = (int) get_option( 'aisp_otools_custom_item_taxable_id', 0 ); $notax_id = (int) get_option( 'aisp_otools_custom_item_notax_id', 0 ); if ( $taxable_id && get_post_status( $taxable_id ) ) { wp_update_post( [ 'ID' => $taxable_id, 'post_title' => sprintf( // translators: %s = base name of the custom item. esc_html__( '%s (Taxable)', 'aisp-order-tools-for-woocommerce' ), esc_html( $new ) ), ] ); } if ( $notax_id && get_post_status( $notax_id ) ) { wp_update_post( [ 'ID' => $notax_id, 'post_title' => sprintf( // translators: %s = base name of the custom item. esc_html__( '%s (No tax)', 'aisp-order-tools-for-woocommerce' ), esc_html( $new ) ), ] ); } } private function create_product( $base_name, $is_taxable ) { $name = $is_taxable ? sprintf( // translators: %s = base name of the custom item. esc_html__( '%s (Taxable)', 'aisp-order-tools-for-woocommerce' ), esc_html( $base_name ) ) : sprintf( // translators: %s = base name of the custom item. esc_html__( '%s (No tax)', 'aisp-order-tools-for-woocommerce' ), esc_html( $base_name ) ); $product_id = wp_insert_post( [ 'post_title' => $name, 'post_status' => 'publish', 'post_type' => 'product', ] ); if ( is_wp_error( $product_id ) ) { return 0; } $product = wc_get_product( $product_id ); if ( $product ) { $product->set_regular_price( 0 ); $product->set_price( 0 ); $product->set_virtual( true ); $product->set_sold_individually( true ); $product->set_catalog_visibility( 'hidden' ); $product->set_manage_stock( false ); $product->set_tax_status( $is_taxable ? 'taxable' : 'none' ); $product->save(); } return $product_id; } public function frontend_fields() { global $product; $taxable_id = (int) get_option( 'aisp_otools_custom_item_taxable_id', 0 ); $notax_id = (int) get_option( 'aisp_otools_custom_item_notax_id', 0 ); if ( ! in_array( $product->get_id(), [ $taxable_id, $notax_id ], true ) ) { return; } ?>



get_cart() as $item ) { if ( isset( $item['aisp_custom_price'] ) ) { $item['data']->set_price( $item['aisp_custom_price'] ); } } } public function change_cart_item_name( $name, $cart_item, $cart_item_key ) { return isset( $cart_item['aisp_custom_label'] ) ? esc_html( $cart_item['aisp_custom_label'] ) : $name; } public function add_to_order( $item, $cart_item_key, $values, $order ) { if ( isset( $values['aisp_custom_label'] ) ) { $item->set_name( sanitize_text_field( $values['aisp_custom_label'] ) ); } if ( isset( $values['aisp_custom_price'] ) ) { $item->add_meta_data( esc_html__( 'Custom price', 'aisp-order-tools-for-woocommerce' ), wc_price( $values['aisp_custom_price'] ) ); } } public function block_direct_access() { if ( is_admin() || ! is_singular( 'product' ) ) { return; } $taxable_id = (int) get_option( 'aisp_otools_custom_item_taxable_id', 0 ); $notax_id = (int) get_option( 'aisp_otools_custom_item_notax_id', 0 ); if ( ! in_array( get_the_ID(), [ $taxable_id, $notax_id ], true ) ) { return; } if ( current_user_can( 'manage_woocommerce' ) ) { return; } wp_safe_redirect( home_url() ); exit; } public function block_trash( $trash, $post ) { if ( isset( $GLOBALS['aisp_otools_allow_delete'] ) && $GLOBALS['aisp_otools_allow_delete'] === true ) { return $trash; } if ( ! $post instanceof \WP_Post ) { return $trash; } $taxable_id = (int) get_option( 'aisp_otools_custom_item_taxable_id', 0 ); $notax_id = (int) get_option( 'aisp_otools_custom_item_notax_id', 0 ); if ( in_array( $post->ID, [ $taxable_id, $notax_id ], true ) ) { wp_die( esc_html__( 'This product is managed by Order Tools and cannot be deleted manually.', 'aisp-order-tools-for-woocommerce' ), esc_html__( 'Action blocked', 'aisp-order-tools-for-woocommerce' ), [ 'back_link' => true ] ); } return $trash; } public function block_delete( $post_id ) { if ( isset( $GLOBALS['aisp_otools_allow_delete'] ) && $GLOBALS['aisp_otools_allow_delete'] === true ) { return; } $taxable_id = (int) get_option( 'aisp_otools_custom_item_taxable_id', 0 ); $notax_id = (int) get_option( 'aisp_otools_custom_item_notax_id', 0 ); if ( in_array( $post_id, [ $taxable_id, $notax_id ], true ) ) { wp_die( esc_html__( 'This product is required by Order Tools and cannot be deleted manually.', 'aisp-order-tools-for-woocommerce' ), esc_html__( 'Action blocked', 'aisp-order-tools-for-woocommerce' ), [ 'back_link' => true ] ); } } }