Skip to content

Instantly share code, notes, and snippets.

View kartikparmar's full-sized avatar

Kartik Parmar kartikparmar

  • Mumbai
View GitHub Profile
@kartikparmar
kartikparmar / functions.php
Last active February 5, 2019 16:37
Function to display content of WooCommerce orders dashboard widget.
<?php
/**
* Create the function to output the contents of our Dashboard Widget.
*/
function wc_orders_dashboard_widget_function() {
$args = array(
'post_type' => 'shop_order',
@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 July 24, 2019 17:35
hide_wc_category_from_shop
<?php
/**
* Show products only of selected category.
*/
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
$hide_category = array( 126 ); // Ids of the category you don't want to display on the shop page
@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'];
@kartikparmar
kartikparmar / functions.php
Created November 6, 2018 05:30
Automatically adding the product to the cart for only specific user roles.
<?php
/*
* Automatically adding the product to the cart for only specific user roles.
*/
function aaptc_add_product_to_cart( $item_key, $product_id ) {
$product_category_id = 123; // cricket bat category id
$product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' );
$user = wp_get_current_user(); // This will fetch current user's role
$allowed_roles = array( 'booking_agent', 'perfecth' ); // These are the list of allowed user role.