Skip to content

Instantly share code, notes, and snippets.

View kartikparmar's full-sized avatar

Kartik Parmar kartikparmar

  • Mumbai
View GitHub Profile
@kartikparmar
kartikparmar / cmnq_min_max_quantity.php
Last active November 3, 2017 07:13
Saving the value when product is published/updated
<?php
/*
* This function will save the value set to Minimum Quantity and Maximum Quantity options
* into _wc_min_qty_product and _wc_max_qty_product meta keys respectively
*/
function wc_qty_save_product_field( $post_id ) {
$val_min = trim( get_post_meta( $post_id, '_wc_min_qty_product', true ) );
$new_min = sanitize_text_field( $_POST['_wc_min_qty_product'] );
<?php
function wc_qty_add_product_field() {
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_wc_min_qty_product',
'label' => __( 'Minimum Quantity', 'woocommerce-max-quantity' ),
'placeholder' => '',
'desc_tip' => 'true',
@kartikparmar
kartikparmar / functions.php
Created November 30, 2017 07:31
Adding dashboard widget
<?php
/**
* Add a widget to the dashboard.
*
* This function is hooked into the 'wp_dashboard_setup' action below.
*/
function wc_orders_dashboard_widgets() {
wp_add_dashboard_widget(
'wc_order_widget_id', // Widget slug.
@kartikparmar
kartikparmar / wcodw_style.css
Created November 30, 2017 12:03
Applying styling for the Orders table, Action Buttons and View all orders Link.
.complete, .processing, .view {
display: block;
text-indent: -9999px;
position: relative;
height: 1em;
width: 1em;
padding: 0!important;
height: 2em!important;
width: 2em;
}
@kartikparmar
kartikparmar / functions.php
Created November 30, 2017 12:07
Including the wcodw_style.css file on the WordPress dashboard
<?php
/**
* This function include wcodw_style.css file only on dashboard.
*/
function wc_my_enqueue_scripts_css() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( $screen_id == "dashboard" ) {
@kartikparmar
kartikparmar / functions.php
Last active January 20, 2018 06:48
Using this you can remove free product from cart when parent product is removed
<?php
/**
* Remove free product from cart when parent product is removed.
*/
function remove_product_from_cart() {
// Run only in the Cart or Checkout Page
if( is_cart() || is_checkout() ) {
@kartikparmar
kartikparmar / functions.php
Last active May 5, 2018 13:05
Adding button on mini cart.
<?php
/**
* Adding button to mini cart.
*/
function woocommerce_widget_shopping_cart_buttons_callback() {
echo '<a href="' . esc_url( wc_get_checkout_url() ) . '" class="button wc-forward x-anchor">'. esc_html__( 'Submit Order', 'woocommerce' ) .'</a>';
}
add_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_buttons_callback', 20 );
@kartikparmar
kartikparmar / functions.php
Created May 31, 2018 07:46
Show product's extra information inline on Order Received page and Email Notification
<?php
function woocommerce_order_item_get_formatted_meta_data_callback( $formatted_meta ){
foreach ( $formatted_meta as $key => $value ) {
$formatted_meta[$key]->display_value = wp_strip_all_tags( $value->display_value );
}
return $formatted_meta;
}
add_filter( "woocommerce_order_item_get_formatted_meta_data", "woocommerce_order_item_get_formatted_meta_data_callback", 10, 1 );
@kartikparmar
kartikparmar / functions.php
Created August 24, 2018 12:45
Get all WooCommerce product ids
<?php
/**
* Function to fetch all WooCommerce product ids.
*/
function get_all_wc_products_ids() {
$args = array(
'post_type' => array( 'product' ),
'posts_per_page' => -1,
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ), // 'all'
@kartikparmar
kartikparmar / functions.php
Created September 30, 2018 10:39
Set price to 0 for a perticular product.
<?php
function add_custom_price( $cart_object ) {
$cart_total = 25; // If cart total is more than this value.
$free_product_id = 6332; // Set price to 0 of this free product.
$carttotal = 0;
foreach ( $cart_object->cart_contents as $key => $value ) {
$_product = $value['data'];