Skip to content

Instantly share code, notes, and snippets.

@renrax
Last active January 14, 2022 10:17
Show Gist options
  • Save renrax/9abd5d9ef263bf491446b46ab48637d4 to your computer and use it in GitHub Desktop.
Save renrax/9abd5d9ef263bf491446b46ab48637d4 to your computer and use it in GitHub Desktop.
Dokan - Adding checkbox for custom settings on New product popup/without popup form
This code adds the Checkbox field to Dokan and to WooCommerce.
But
If the administrator on the Wordpress side edits, the box is saved.
If the seller edits via Dokan, the box will remain unchecked after he saves the page.
<?php
/**
* 1. Add checkbox
*/
//This field Adds a checkbox to the edit product page by Wordpress and Display him
add_action( 'woocommerce_product_options_general_product_data', 'add_related_products_checkbox' );
function add_related_products_checkbox() {
woocommerce_wp_checkbox( array(
'id' => 'hide_related',
'class' => '',
'label' => 'Remove related products'
) );
}
// This field Adds a checkbox to the edit product page by Dokan
add_action( 'dokan_new_product_after_product_tags','dokan_add_related_products_checkbox', 10 );
function dokan_add_related_products_checkbox(){ ?>
<div class="dokan-form-group">
<label for="hide_related" class="form-label">
<input name="hide_related" id="hide_related" value="yes" type="checkbox"> Remove related products <i class="fa fa-question-circle tips" aria-hidden="true" data-bs-original-title="Check this box if you want to remove the related products on product page."></i></label>
</div>
<?php
}
/*-----------------------*/
/**
* 2. Save checkbox
*/
//Save checkbox field by Wordpress
add_action( 'save_post_product', 'save_related_products_checkbox' );
function save_related_products_checkbox( $product_id ) {
global $pagenow, $typenow;
if ( 'post.php' !== $pagenow || 'product' !== $typenow ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( isset( $_POST['hide_related'] ) ) {
update_post_meta( $product_id, 'hide_related', $_POST['hide_related'] ); }
else delete_post_meta( $product_id, 'hide_related' );
}
// Save checkbox field by Dokan
add_action( 'dokan_new_product_added','save_add_product_meta', 10, 2 );
add_action( 'dokan_product_updated', 'save_add_product_meta', 10, 2 );
function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( ! empty( $postdata['hide_related'] ) ) {
update_post_meta( $product_id, 'hide_related', $postdata['hide_related'] );
}
}
/*-----------------------*/
/**
* 3. Display checkbox
*/
//Showing field data on Dokan product edit page
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,2);
function show_on_edit_page($post, $post_id){
$hide_related = get_post_meta( $post_id, 'hide_related', true );
?>
<div class="dokan_product_my dokan-edit-row ">
<div class="dokan-section-heading" data-togglehandler="dokan_product_my">
<h2><i class="fa fa-cubes" aria-hidden="true"></i> Моя таба</h2>
<p>Управление моей табой.</p>
<a href="#" class="dokan-section-toggle">
<i class="fa fa-sort-desc fa-flip-vertical" aria-hidden="true" style="margin-top: 9px;"></i>
</a>
<div class="dokan-clearfix"></div>
</div>
<div class="dokan-section-content">
<div class="dokan-form-group">
<label for="hide_related" class="form-label">
<input name="hide_related" id="hide_related" value="yes" type="checkbox"> Remove related products <i class="fa fa-question-circle tips" aria-hidden="true" data-bs-original-title="Check this box if you want to remove the related products on product page."></i></label>
</div> </div> </div><?php
}
/*-----------------------*/
// 4. Hide related products in single product page
add_action( 'woocommerce_after_single_product_summary', 'hide_related_products_checkbox', 1 );
function hide_related_products_checkbox() {
global $product;
if ( ! empty ( get_post_meta( $product->get_id(), 'hide_related', true ) ) ) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 ); }
}
<?php
/**
* Change the number of related products
*/
function woo_related_products_limit() {
global $product;
$args['posts_per_page'] = 6;
return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'related_products_args', 20 );
function related_products_args( $args ) {
$args['posts_per_page'] = 3; // 3 related products
$args['columns'] = 1; // arranged in 1 columns
return $args;
}
?>
<?php
/**
* Try the following that will remove related products from specific defined product ID(s) page(s):
*/
add_action( 'woocommerce_after_single_product_summary', 'remove_related_products_conditionally', 1 );
function remove_related_products_conditionally(){
global $product;
// HERE Define your targeted product Id(s) (in the array)
$targeted_products = array( 37 );
if( in_array( $product->get_id(), $targeted_products ) )
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment