Created
September 24, 2023 05:39
-
-
Save codersaiful/9b9b66e11b338f625fbb27c940e4662d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if( !function_exists( 'wpt_show_variation_table' ) ){ | |
/** | |
* Show variation table on each and every variable product page | |
* which will replaced the default variation dropdown select options. | |
* | |
* | |
* @since 7.0.8.1 | |
*/ | |
function wpt_show_variation_table(){ | |
global $product; | |
//var_dump($product->get_type()); | |
if( $product->get_type() !== 'variable' ) return; | |
$config = get_option( 'wpt_configure_options' ); | |
$enable = isset( $config['table_for_variable_product'] ) && $config['table_for_variable_product'] == 'on' ? true : false; | |
//if( isset( $config['table_on_archive'] ) && $config['table_on_archive'] == 'on' ) | |
$table_id = isset( $config['variation_table_id'] ) ? $config['variation_table_id'] : false; | |
$table_position = isset( $config['variation_table_position'] ) ? $config['variation_table_position'] : false; | |
$table_id = apply_filters( 'wpto_variation_table_id', $table_id ); | |
$table_id = is_numeric( $table_id ) ? (int) $table_id : false; | |
if( $table_id && $enable ){ | |
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 ); | |
if ($table_position == 'woocommerce_single_product_summary') { | |
add_action($table_position, 'wpt_woo_product_table_as_variation', 4); | |
}else{ | |
add_action($table_position, 'wpt_woo_product_table_as_variation', -999); | |
} | |
} | |
} | |
} | |
add_action( 'woocommerce_single_product_summary', 'wpt_show_variation_table', 3 ); | |
if( !function_exists( 'wpt_woo_product_table_as_variation' ) ){ | |
function wpt_woo_product_table_as_variation(){ | |
$config = get_option( 'wpt_configure_options' ); | |
$table_id = isset( $config['variation_table_id'] ) ? $config['variation_table_id'] : false; | |
$table_id = apply_filters( 'wpto_variation_table_id', $table_id ); | |
$table_id = is_numeric( $table_id ) ? (int) $table_id : false; | |
//var_dump($table_id); | |
if( $table_id ){ | |
add_filter( 'wpto_table_query_args', 'wpt_variation_table_args' ); | |
echo do_shortcode( "[Product_Table id='{$table_id}']" ); | |
} | |
} | |
} | |
if( !function_exists( 'wpt_variation_table_args' ) ){ | |
/** | |
* Args manipulation for variable product | |
* | |
* @param array $args WPT default args array | |
* @return array | |
*/ | |
function wpt_variation_table_args( $args ){ | |
$_ID = get_the_ID(); | |
if( empty( $_ID ) || ! is_product() ) return $args; | |
$product_includes = []; | |
$this_post_variable = new WC_Product_Variable( $_ID ); | |
$available_post_includes = $this_post_variable->get_children(); | |
if( isset( $available_post_includes ) && is_array($available_post_includes) && count( $available_post_includes ) > 0 ){ | |
foreach ($available_post_includes as $pperItem){ | |
$product_includes[$pperItem] = $pperItem; | |
} | |
} | |
$args['post_type'] = array( 'product', 'product_variation' ); | |
$args['post__in'] = $product_includes; | |
unset( $args['tax_query'] ); | |
// unset( $args['meta_query'] ); //stock kaj korchilona bole off korechi. notun somossay paile abar dekhote hobe: 8.0.8.1 | |
return $args; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment