set_defaults(); // if ( (! $product->get_id() || ! ( $post_object = get_post( $product->get_id() ) ) || 'product' !== $post_object->post_type) && 'exchange_request' !== $post_object->post_type ) { // throw new Exception( __( 'Invalid product.', 'astra' ) ); // } // $id = $product->get_id(); // $product->set_props( array( // 'name' => 'Fee for '.$post_object->post_title, // 'slug' => $post_object->post_name, // 'date_created' => 0 < $post_object->post_date_gmt ? wc_string_to_timestamp( $post_object->post_date_gmt ) : null, // 'date_modified' => 0 < $post_object->post_modified_gmt ? wc_string_to_timestamp( $post_object->post_modified_gmt ) : null, // 'status' => $post_object->post_status, // 'description' => $post_object->post_content, // 'short_description' => $post_object->post_excerpt, // 'parent_id' => $post_object->post_parent, // 'menu_order' => $post_object->menu_order, // 'reviews_allowed' => 'open' === $post_object->comment_status, // ) ); // $this->read_attributes( $product ); // $this->read_downloads( $product ); // $this->read_visibility( $product ); // $this->read_product_data( $product ); // $this->read_extra_data( $product ); // $product->set_object_read( true ); // } // /** // * Get the product type based on product ID. // * // * @since 3.0.0 // * @param int $product_id // * @return bool|string // */ // public function get_product_type( $product_id ) { // $post_type = get_post_type( $product_id ); // if ( 'product_variation' === $post_type ) { // return 'variation'; // } elseif ( in_array( $post_type, array( 'exchange_request', 'product' ) ) ) { // change exchange_request with your post type // $terms = get_the_terms( $product_id, 'product_type' ); // return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple'; // } else { // return false; // } // } // } class WCCPT_Product_Data_Store_CPT extends WC_Product_Data_Store_CPT { /** * Method to read a product from the database. * @param WC_Product */ public function read( &$product ) { $product->set_defaults(); if ( ! $product->get_id() || ! ( $post_object = get_post( $product->get_id() ) ) || ! in_array( $post_object->post_type, array( 'exchange_request', 'product' ) ) ) { // change birds with your post type throw new Exception( __( 'Invalid product.', 'astra' ) ); } $id = $product->get_id(); $product->set_props( array( 'name' => $post_object->post_title. ' fee', 'slug' => $post_object->post_name, 'date_created' => 0 < $post_object->post_date_gmt ? wc_string_to_timestamp( $post_object->post_date_gmt ) : null, 'date_modified' => 0 < $post_object->post_modified_gmt ? wc_string_to_timestamp( $post_object->post_modified_gmt ) : null, 'status' => 'publish', 'description' => $post_object->post_content, 'short_description' => $post_object->post_excerpt, 'parent_id' => $post_object->post_parent, 'menu_order' => $post_object->menu_order, 'reviews_allowed' => 'open' === $post_object->comment_status, ) ); $this->read_attributes( $product ); $this->read_downloads( $product ); $this->read_visibility( $product ); $this->read_product_data( $product ); $this->read_extra_data( $product ); $product->set_object_read( true ); } /** * Get the product type based on product ID. * * @since 3.0.0 * @param int $product_id * @return bool|string */ public function get_product_type( $product_id ) { $post_type = get_post_type( $product_id ); if ( 'product_variation' === $post_type ) { return 'variation'; } elseif ( in_array( $post_type, array( 'exchange_request', 'product' ) ) ) { // change birds with your post type $terms = get_the_terms( $product_id, 'product_type' ); return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple'; } else { return false; } } } // Make the custom post type 'exchange_request' purchasable function softoba_make_exchange_request_purchasable($purchasable, $product) { if ($product->get_type() === 'exchange_request') { $purchasable = true; } return $purchasable; } add_filter('woocommerce_is_purchasable', 'softoba_make_exchange_request_purchasable', 10, 2); // function softoba_custom_post_type_in_product_query( $query ) { // if ( is_admin() || ! $query->is_main_query() ) { // return; // } // $query->set( 'post_type', array( 'product', 'exchange_request' ) ); // } // add_action( 'pre_get_posts', 'softoba_custom_post_type_in_product_query' ); // add_filter('woocommerce_get_price','softoba_woocommerce_get_price',20,2); // function softoba_woocommerce_get_price($price, $post){ // if ($post->post->post_type === 'exchange_request') { // $price = get_post_meta($post->id, "exchange_price", true); // } // return $price; // } add_filter('woocommerce_product_get_price', 'softoba_woocommerce_product_get_price', 10, 2); function softoba_woocommerce_product_get_price($price, $product) { if ( $product->get_type() == 'exchange_request' ){ $price = 3; } return $price; } // function softoba_woocommerce_data_stores( $stores ) { // require_once WP_PLUGIN_DIR . '/woocommerce/includes/class-wc-data-store.php'; // $stores['product'] = 'SOFTOBA_Product_Data_Store_EXR'; // return $stores; // } // add_filter( 'woocommerce_data_stores', 'softoba_woocommerce_data_stores' ); add_filter( 'woocommerce_data_stores', 'woocommerce_data_stores' ); function woocommerce_data_stores ( $stores ) { $stores['product'] = 'WCCPT_Product_Data_Store_CPT'; return $stores; } /** * Add _price post_meta to exchange_request upon publish * * _pirce field is required for WooCommerce add to cart */ function softoba_add_price_field_to_post($post_id, $post) { if ( 'exchange_request' === $post->post_type ) { // Add or update custom meta key with an underscore prefix if (get_post_meta($post_id, '_price', true) === '') { add_post_meta($post_id, '_price', '3', true); add_post_meta($post_id, '_product_type', 'simple', true); } else { update_post_meta($post_id, '_price', '3'); update_post_meta($post_id, '_product_type', 'simple'); } } } add_action( 'save_post', 'softoba_add_price_field_to_post', 10, 2 ); /** * Change cart item permalink */ // function softoba_cart_item_permalink( $permalink, $cart_item, $cart_item_key ) { // $type = ! empty( $cart_item['softoba_cart_data']['order_type'] ) ? $cart_item['softoba_cart_data']['order_type'] : ''; // if ( is_cart() && $type == 'exchange_request' ) { // $permalink = $cart_item['softoba_cart_data']['post_permalink']; // } // return $permalink; // } // add_filter( 'woocommerce_cart_item_permalink', 'softoba_cart_item_permalink', 10, 3 ); // Display custom cart item meta data (in cart and checkout) function softoba_display_cart_item_custom_meta_data( $item_data, $cart_item ) { if ( isset( $cart_item['softoba_cart_data']['order_type'] ) ) { $item_data[] = array( 'key' => __( 'Type', 'astra' ), 'value' => 'Exchange Request', ); } if ( isset( $cart_item['softoba_cart_data']['post_id'] ) && $cart_item['softoba_cart_data']['post_id'] > 0 ) { $item_data[] = array( 'key' => __( 'Request ID', 'astra' ), 'value' => $cart_item['softoba_cart_data']['post_id'], ); } if ( isset( $cart_item['softoba_cart_data']['post_author'] ) && $cart_item['softoba_cart_data']['post_author'] > 0 ) { $item_data[] = array( 'key' => __( 'Customer ID', 'astra' ), 'value' => $cart_item['softoba_cart_data']['post_author'], ); } if ( isset( $cart_item['softoba_cart_data']['unique_id'] ) && $cart_item['softoba_cart_data']['unique_id'] > 0 ) { $item_data[] = array( 'key' => __( 'Unique ID', 'astra' ), 'value' => $cart_item['softoba_cart_data']['unique_id'], ); } return $item_data; } add_filter( 'woocommerce_get_item_data', 'softoba_display_cart_item_custom_meta_data', 10, 2 ); /** * Show custom data in order details */ function softoba_fee_custom_order_data( $item, $cart_item_key, $values, $order ) { // Assigning data into variables $order_type = !empty($values['softoba_cart_data']['order_type']) ? $values['softoba_cart_data']['order_type'] : ''; $post_author = !empty($values['softoba_cart_data']['post_author']) ? $values['softoba_cart_data']['post_author'] : ''; $post_id = !empty($values['softoba_cart_data']['post_id']) ? $values['softoba_cart_data']['post_id'] : ''; $unique_id = !empty($values['softoba_cart_data']['unique_id']) ? $values['softoba_cart_data']['unique_id'] : ''; /** * Show data in order meta & email * */ if ( $order_type ) { $item->update_meta_data( '_order_type', $order_type ); } if ( $post_author ) { $item->update_meta_data( '_post_author', $post_author ); } if ( $post_id ) { $item->update_meta_data( '_post_id', $post_id ); } if ( $unique_id ) { $item->update_meta_data( '_unique_id', $unique_id ); } } add_action( 'woocommerce_checkout_create_order_line_item', 'softoba_fee_custom_order_data', 10, 4 ); /** * Add order id to the exchange request meta field * * runs during WooCommerce checkout process * * @author fida */ function softoba_add_order_id_exchange_checkout_order_processed( $order_id, $posted_data, $order ) { $softoba_integration_order_data = array( 'order_id' => $order_id ); $softoba_integration_order_status = []; # Get and Loop Over Order Line Items foreach ( $order->get_items() as $item_id => $item ) { $order_type = $item->get_meta( '_order_type', true ); if("exchange_request" == $order_type){ $post_id = $item->get_meta( '_post_id', true ); // Product Exchange id $unique_id = $item->get_meta( '_unique_id', true ); // Unique id of rooms $exchange_fee = get_post_meta( $post_id, 'softoba_exchange_fee', true ); // Exchange Request meta update_post_meta( $post_id, 'softoba_exchange_fee', $order->get_status() ); update_post_meta( $post_id, 'softoba_exchange_fee_order_id', $order_id ); update_post_meta( $post_id, 'softoba_exchange_payment_method', $order->get_payment_method() ); } } } add_action( 'woocommerce_checkout_order_processed', 'softoba_add_order_id_exchange_checkout_order_processed', 10, 4 ); /* * Admin order status change * @author Jahid */ add_action('woocommerce_order_status_changed', 'softoba_order_status_changed', 10, 4); function softoba_order_status_changed($order_id, $old_status, $new_status, $order) { foreach ( $order->get_items() as $item_id => $item ) { $order_type = $item->get_meta( '_order_type', true ); if("exchange_request" == $order_type){ $post_id = $item->get_meta( '_post_id', true ); // Product Exchange id update_post_meta( $post_id, 'softoba_exchange_fee', $order->get_status() ); update_post_meta( $post_id, 'softoba_exchange_fee_order_id', $order_id ); update_post_meta( $post_id, 'softoba_exchange_payment_method', $order->get_payment_method() ); } } } /** * Add custom fields to checkout page before phone field */ function softoba_add_custom_checkout_fields( $fields ) { // Display fields only when the selected country is Kuwait $kuwait_fields = array( 'softoba_governorate_options' => array( 'type' => 'select', 'label' => __('Governorate', 'astra'), 'required' => true, 'class' => array('form-row-wide'), 'clear' => true, 'options' => array( 'Al Asimah Governate' => __('Al Asimah Governate', 'astra'), 'Ahmadi Governorate' => __('Ahmadi Governorate', 'astra'), 'Mubarak Al-Kabeer Governorate' => __('Mubarak Al-Kabeer Governorate', 'astra'), 'Farwaniya Governorate' => __('Farwaniya Governorate', 'astra'), 'Jahra Governorate' => __('Jahra Governorate', 'astra'), 'Hawalli Governorate' => __('Hawalli Governorate', 'astra'), ), ), 'softoba_area_field' => array( 'type' => 'text', 'label' => __('Area', 'astra'), 'required' => true, 'class' => array('form-row-wide'), 'clear' => true, ), 'softoba_block_field' => array( 'type' => 'text', 'label' => __('Block', 'astra'), 'required' => true, 'class' => array('form-row-wide'), 'clear' => true, ), 'softoba_avenue' => array( 'type' => 'text', 'label' => __('Avenue', 'astra'), 'required' => false, // Set to false for optional 'class' => array('form-row-wide'), 'clear' => true, ), 'softoba_house_number' => array( 'type' => 'text', 'label' => __('House Number', 'astra'), 'required' => true, 'class' => array('form-row-wide'), 'clear' => true, ), ); $fields['billing'] = array_merge( array_slice($fields['billing'], 0, 4, true), $kuwait_fields, array_slice($fields['billing'], 4, null, true) ); if( WC()->customer && WC()->customer->get_billing_country() !== 'KW' ) { // Set the "required" attribute to false for specified fields $fields['billing']['softoba_governorate_options']['required'] = false; $fields['billing']['softoba_area_field']['required'] = false; $fields['billing']['softoba_block_field']['required'] = false; $fields['billing']['softoba_avenue']['required'] = false; $fields['billing']['softoba_house_number']['required'] = false; } $fields['billing']['billing_address_1']['label'] = __('Street Name / Number', 'astra'); return $fields; } add_filter('woocommerce_checkout_fields', 'softoba_add_custom_checkout_fields'); function custom_checkout_fields( $fields ) { // Check if the selected country is Kuwait if ( WC()->customer && WC()->customer->get_billing_country() === 'KW' ) { // Set fields as optional for Kuwait // $fields['billing']['billing_address_1']['required'] = false; $fields['billing']['billing_city']['required'] = false; $fields['billing']['billing_postcode']['required'] = false; } $fields['billing']['billing_address_1']['label'] = __('Street Name / Number', 'astra'); return $fields; } add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields' ); /** * Save custom field data to the order */ function softoba_save_custom_checkout_fields( $order_id ) { if ( isset( $_POST['softoba_governorate_options'] ) ) { update_post_meta( $order_id, 'oont_governorate', sanitize_text_field( $_POST['softoba_governorate_options'] ) ); } if ( isset( $_POST['softoba_area_field'] ) ) { update_post_meta( $order_id, 'oont_area', sanitize_text_field( $_POST['softoba_area_field'] ) ); } if ( isset( $_POST['softoba_block_field'] ) ) { update_post_meta( $order_id, 'oont_block', sanitize_text_field( $_POST['softoba_block_field'] ) ); } if ( isset( $_POST['softoba_avenue'] ) ) { update_post_meta( $order_id, 'oont_avenue', sanitize_text_field( $_POST['softoba_avenue'] ) ); } if ( isset( $_POST['softoba_house_number'] ) ) { update_post_meta( $order_id, 'oont_house_number', sanitize_text_field( $_POST['softoba_house_number'] ) ); } } add_action('woocommerce_checkout_update_order_meta', 'softoba_save_custom_checkout_fields'); /** * Display custom field data on the order page */ function softoba_display_order_data( $order ) { echo '