Custom Backorder option

mail@pastecode.io avatar
unknown
php
3 years ago
2.4 kB
11
Indexable
Never
// Add custom backorder checkbox for product variations.
function action_woocommerce_variation_options( $loop, $variation_data, $variation ) {
    $is_checked = get_post_meta( $variation->ID, 'custom_backorder_checkbox', true );

    if ( $is_checked == 'yes' ) {
        $is_checked = 'checked';
    } else {
        $is_checked = '';     
    }

    ?>
    <label class="tips" data-tip="<?php esc_attr_e( 'Active this to show product is an order item.', 'woocommerce' ); ?>">
        <?php esc_html_e( 'Product is an order item', 'woocommerce' ); ?>
        <input type="checkbox" class="checkbox variable_checkbox custom_backorder_checkbox" name="custom_backorder_checkbox[<?php echo esc_attr( $loop ); ?>]"<?php echo $is_checked; ?>/>
    </label>
    <?php
}
add_action( 'woocommerce_variation_options', 'action_woocommerce_variation_options', 10, 3);

// Save custom backorder checkbox for product variations.
function action_woocommerce_save_product_variation( $variation_id, $i ) {
    if ( ! empty( $_POST['custom_backorder_checkbox'] ) && ! empty( $_POST['custom_backorder_checkbox'][$i] ) ) {
        update_post_meta( $variation_id, 'custom_backorder_checkbox', 'yes' );
    } else {
        update_post_meta( $variation_id, 'custom_backorder_checkbox', 'no' ); 
    }       
}
add_action( 'woocommerce_save_product_variation', 'action_woocommerce_save_product_variation', 10, 2 );

/**
 * Following has been tried with multiple variations
 */
 /**
 * WooCommerce single product page hooks for backorder products.
 */
//add_action('woocommerce_single_product_summary', 'igniters_add_backorder_text', 999);
function igniters_add_backorder_text() {
	global $product;
	$variations = $product->get_available_variations();
	echo '<pre style="border: 1px solid #bbb;background:#ddd;padding:15px;margin:15px;">'.$product.'</pre>';
	echo '<hr/>';
	echo '<pre style="border: 1px solid #bbb;background:#ddd;padding:15px;margin:15px;">'.json_encode($variations).'</pre>';
	echo '<hr/>';
	/* return variations id array */
	$variations_id 		= wp_list_pluck( $variations, 'variation_id' );
	/* return variation attributes array */
	$attributes 		= $product->get_variation_attributes();
	$attributesArray 	= wp_list_pluck( $variations, 'attributes' );
	echo '<pre style="border: 1px solid #bbb;background:#ddd;padding:15px;margin:15px;">'.json_encode($attributesArray).'</pre>';
	echo '<hr/>';
	$children = $variations->get_children();
}