Category: Woocommerce Hooks

  • Customize WooCommerce Checkout form into Accordion Multi Steps

    Customize WooCommerce Checkout form into Accordion Multi Steps

    Hello guys how are you? Welcome back to my channel. Today in this post I am going to show you Customize WooCommerce Checkout form into Accordion Multi Steps.

    Guys I have used both latest versions WordPress 6.3 and WooCommerce 8.1.

    Live Demo

    Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

    1. WooCommerce Hooks
    2. WordPress Tricks
    3. WordPress Hooks
    Customize WooCommerce Checkout form into Accordion Multi Steps
    Customize WooCommerce Checkout form into Accordion Multi Steps
    MultiStep Checkout for WooCommerce

    This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

    Jassa

    Developer’s King

    Thanks

  • WooCommerce Single Product Page Change Add To Cart Button Text According to Variation Stock Quantity

    WooCommerce Single Product Page Change Add To Cart Button Text According to Variation Stock Quantity

    Hello guys how are you? Welcome back to my channel. Today in this post I am going to show you WooCommerce Single Product Page Change Add To Cart Button Text According to Variation Stock Quantity

    Guys I have used both latest versions WordPress 6.3 and WooCommerce 8.1.

    Live Demo

    Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

    1. WooCommerce Hooks
    2. WordPress Tricks
    3. WordPress Hooks
    WooCommerce Single Product Page Change Add To Cart Button Text According to Variation Stock Quantity
    WooCommerce Single Product Page Change Add To Cart Button Text According to Variation Stock Quantity

    Guys here is the working code snippet and please use it carefully:

    1. Guys here is the code snippet and you need to add your theme’s functions.php file:

    guys also I will make it with ajax and share that as well

    // For all product variations (on a variable product)
    add_action( 'woocommerce_after_add_to_cart_button', 'after_add_to_cart_button_action_callback', 0 );
    function after_add_to_cart_button_action_callback() {
        global $product;
    
        if( $product->is_type('variable') ) :
    
        $data = [];
    
        // Loop through variation Ids
        foreach( $product->get_visible_children() as $variation_id ){
            $variation = wc_get_product( $variation_id );
            $data[$variation_id] = $variation->is_in_stock();
            
        }
        
    
        $outofstock_text = __("Sold Out", "woocommerce");
        ?>
        <script type="text/javascript">
        jQuery(function($){
            var b = 'button.single_add_to_cart_button',
                t = $(b).text();
    
            $('form.variations_form').on('show_variation hide_variation found_variation', function(){
                $.each(<?php echo json_encode($data); ?>, function(j, r){
                    var i = $('input[name="variation_id"]').val();
                    if(j == i && i != 0 && !r ) {
                        $(b).html('<?php echo $outofstock_text; ?>');
                        return false;
                    } else {
                        $(b).html(t);
                    }
                });
            });
        });
        </script>
        <?php
        endif;
    }

    This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

    Jassa

    Developer’s King

    Thanks

  • How to add quantity input with add to button on WooCommerce shop page?

    How to add quantity input with add to button on WooCommerce shop page?

    Hello guys how are you? Welcome back to my channel. Today in this post I am going to show you How to add quantity input with add to button on WooCommerce shop page?

    Guys I have used both latest versions WordPress 6.3 and WooCommerce 8.1.

    Live Demo

    Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

    1. WooCommerce Hooks
    2. WordPress Tricks
    3. WordPress Hooks

    Guys here is the working code snippet and please use it carefully:

    1. Guys here is the code snippet and you need to add your theme’s functions.php file:

    guys also I will make it with ajax and share that as well

    add_filter( 'woocommerce_loop_add_to_cart_link', 'change_product_button', 10, 2 );
    function change_product_button($html, $product) {
        $values = array(190, 91);
        $id = $product->id;
        if(in_array($id, $values)){
            $html= '<a href="'.$product->get_permalink( $product->id ).'" class="button">' . __('Choose an option', 'woocommerce') . '</a>';
        } else {
            $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
            $html .= woocommerce_quantity_input( array(), $product, false );
            $html .= '<button type="submit" class="button">' . esc_html( $product->add_to_cart_text() ) . '</button>';
            $html .= '</form>';
        }
        return $html;
    }

    This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

    Jassa

    Developer’s King

    Thanks

  • Solved – WooCommerce checkout ‘internal server error’ fail to load ?wc-ajax=update_order_review

    Solved – WooCommerce checkout ‘internal server error’ fail to load ?wc-ajax=update_order_review

    Hello guys how are you? Welcome back on my blog therichpost.com. Today in this post I am going to tell you how to solve WooCommerce checkout ‘internal server error’ fail to load ?wc-ajax=update_order_review

    WooCommerce checkout 'internal server error' fail to load wc-ajax=update_order_review
    WooCommerce checkout ‘internal server error’ fail to load wc-ajax=update_order_review

    Guys last week I updated my php version to 8 and wordpress and woocommerce as well with latest version and I was checking my site and on checkout page I am getting (Failed to load resource: the server responded with a status of 500 () ?wc-ajax=update_order_review 500) error for my all ajax call and I worried for that but I managed to solved this issue.

    Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

    1. WooCommerce Hooks
    2. WordPress Tricks
    3. WordPress Hooks

    Guys here are the following steps to solve wc-ajax issue:

    1. Update your all the plugins including payment as well.
    2. Update your all the WooCommerce templates inside your theme’s folder.
    3. Don’t forget to update all the hooks related to latest version inside your functions.php file.
    4. Please check at wp-admin -> WooCommerce -> Status -> Logs -> fatal_erros log -> is any error generated?
    5. Cleared cache properly in the end.

    This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

    Jassa

    thanks

  • WooCommerce add multiple products to cart via hook

    WooCommerce add multiple products to cart via hook

    Hello guys how are you? Welcome back to my channel. Today in this post I am going to show you WooCommerce add multiple products to cart via hook.

    Live Demo

    Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

    1. WooCommerce Hooks
    2. WordPress Tricks
    3. WordPress Hooks

    Guys your single URL for adding multiple products to cart will look like you can add your link button:

    https://therichpost.com/woocommerce_tricks/cart/?add-to-cart=602,522

    Add guys the following string to add more quantity: &quantity=3, so the URL will become

    https://therichpost.com/woocommerce_tricks/cart/?add-to-cart=602&quantity=3

    Also added multiple products add to cart on checkout page and URL will be like:

    https://therichpost.com/woocommerce_tricks/checkout/?add-to-cart=602,522

    Guys here is the working code snippet for Display coupon usage count and limit in WooCommerce thank you page and please use carefully:

    1. Guys here is the code snippet and you need to add your theme’s functions.php file

    function woo_add_multiple_products_to_cart( $url = false ) {
      // Make sure WC is installed, and add-to-cart qauery arg exists, and contains at least one comma.
      if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || false === strpos( $_REQUEST['add-to-cart'], ',' ) ) {
        return;
      }
    
      // Remove WooCommerce's hook, as it's useless (doesn't handle multiple products).
      remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
    
      $product_ids = explode( ',', $_REQUEST['add-to-cart'] );
      $count       = count( $product_ids );
      $number      = 0;
    
      foreach ( $product_ids as $id_and_quantity ) {
        // Check for quantities defined in curie notation (<product_id>:<product_quantity>)
        
        $id_and_quantity = explode( ':', $id_and_quantity );
        $product_id = $id_and_quantity[0];
    
        $_REQUEST['quantity'] = ! empty( $id_and_quantity[1] ) ? absint( $id_and_quantity[1] ) : 1;
    
        if ( ++$number === $count ) {
          // Ok, final item, let's send it back to woocommerce's add_to_cart_action method for handling.
          $_REQUEST['add-to-cart'] = $product_id;
    
          return WC_Form_Handler::add_to_cart_action( $url );
        }
    
        $product_id        = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );
        $was_added_to_cart = false;
        $adding_to_cart    = wc_get_product( $product_id );
    
        if ( ! $adding_to_cart ) {
          continue;
        }
    
        $add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->get_type(), $adding_to_cart );
    
        // Variable product handling
        if ( 'variable' === $add_to_cart_handler ) {
          woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_variable', $product_id );
    
        // Grouped Products
        } elseif ( 'grouped' === $add_to_cart_handler ) {
          woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_grouped', $product_id );
    
        // Custom Handler
        } elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ){
          do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url );
    
        // Simple Products
        } else {
          woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_simple', $product_id );
        }
      }
    }
    
    // Fire before the WC_Form_Handler::add_to_cart_action callback.
    add_action( 'wp_loaded', 'woo_add_multiple_products_to_cart', 15 );
    
    
    /**
     * Invoke class private method
     *
     * @since   0.1.0
     *
     * @param   string $class_name
     * @param   string $methodName
     *
     * @return  mixed
     */
    function woo_hack_invoke_private_method( $class_name, $methodName ) {
      if ( version_compare( phpversion(), '5.3', '<' ) ) {
        throw new Exception( 'PHP version does not support ReflectionClass::setAccessible()', __LINE__ );
      }
    
      $args = func_get_args();
      unset( $args[0], $args[1] );
      $reflection = new ReflectionClass( $class_name );
      $method = $reflection->getMethod( $methodName );
      $method->setAccessible( true );
    
      //$args = array_merge( array( $class_name ), $args );
      $args = array_merge( array( $reflection ), $args );
      return call_user_func_array( array( $method, 'invoke' ), $args );
    }

    This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

    Jassa

    Developer’s King

    Thanks

  • Dokan Multi vendor add custom badges to a vendor/users profile working demo

    Dokan Multi vendor add custom badges to a vendor/users profile working demo

    Hello guys, welcome to my blog therichpost.com. Today I am going to show you Dokan Multi vendor add custom badges to a vendor/users profile working demo.

    Live working demo

    Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

    1. WooCommerce Hooks
    2. WordPress Tricks
    3. WordPress Hooks

    This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

    Jassa

    Developer’s King

    Thanks

  • Making the Commission Fee to be Added to the WooCommerce Product | Dokan Multivendor | WordPress

    Making the Commission Fee to be Added to the WooCommerce Product | Dokan Multivendor | WordPress

    Hello guys how are you? Welcome back to my channel. Today in this post I am going to show you Making the Commission Fee to be Added to the WooCommerce Product | Dokan Multivendor | WordPress.

    Live demo

    Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

    1. WooCommerce Hooks
    2. WordPress Tricks
    3. WordPress Hooks

    Guys here is the working code snippet for Woocommerce – Hide a shipping method if cart total is higher than an amount and please use carefully:

    1. Guys here is the code snippet and you need to add your theme’s functions.php file:

    add_filter( 'woocommerce_get_price_html', 'therich_alter_price_display', 9999, 2 );
     
    function therich_alter_price_display( $price_html, $product ) {
        
        // ONLY ON FRONTEND
        if ( is_admin() ) return $price_html;
        
        // ONLY IF PRICE NOT NULL
        if ( '' === $product->get_price() ) return $price_html;
        
       
      //  if ( wc_current_user_has_role( 'customer' ) ) {
            $orig_price = wc_get_price_to_display( $product );
            $price_html = wc_price( $orig_price * 1.80 );
     //   }
        
        return $price_html;
     
    }

    This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

    Jassa

    Developer’s King

    Thanks

  • Display coupon usage count and limit in WooCommerce thank you page

    Display coupon usage count and limit in WooCommerce thank you page

    Hello guys how are you? Welcome back to my channel. Today in this post I am going to show you Display coupon usage count and limit in WooCommerce thank you page.

    Live Demo

    Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

    1. WooCommerce Hooks
    2. WordPress Tricks
    3. WordPress Hooks

    Guys here is the working code snippet for Display coupon usage count and limit in WooCommerce thank you page and please use carefully:

    1. Guys here is the code snippet and you need to add your theme’s functions.php file

    function action_woocommerce_thankyou( $order_id ) {
        // Get $order object
        $order = wc_get_order( $order_id );
    
        foreach( $order->get_coupon_codes() as $coupon_code ) {
            // Get the WC_Coupon object
            $coupon = new WC_Coupon( $coupon_code );
    
            // Get usage count
            $count = $coupon->get_usage_count();
            
            // Get coupon usage limit per customer
            $limit = $coupon->get_usage_limit_per_user();
            
            // OR use this instead, to get coupon usage limit.
            // $limit = $coupon->get_usage_limit();
            
            // NOT empty
            if ( ! empty ( $count ) && ! empty ( $limit ) ) {
                // Calculate remaining
                $remaining = $limit - $count;
            
                // Output
                echo sprintf( '<span class="coupon-class">You used the <strong>%s</strong> coupon <strong>%d</strong> times and there are <strong>%d</strong> more left</span>', $coupon_code, $count, $remaining );
            }
        }
    }
    add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

    This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

    Jassa

    Developer’s King

    Thanks

  • Woocommerce – Hide a shipping method if cart total is higher than an amount

    Woocommerce – Hide a shipping method if cart total is higher than an amount

    Hello guys how are you? Welcome back to my channel. Today in this post I am going to show you Woocommerce – Hide a shipping method if cart total is higher than an amount.

    Working live demo

    Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

    1. WooCommerce Hooks
    2. WordPress Tricks
    3. WordPress Hooks

    Guys here is the working code snippet for Woocommerce – Hide a shipping method if cart total is higher than an amount and please use carefully:

    1. Guys here is the code snippet and you need to add your theme’s functions.php file:

    add_filter( 'woocommerce_package_rates', 'shipping_based_on_country_subtotal', 100, 2 );
    function shipping_based_on_country_subtotal( $rates, $package ) {
        $country   = WC()->customer->get_shipping_country();
        $subtotal  = WC()->cart->subtotal; // subtotal incl taxes
        $condition = $country == "US" && $subtotal >= 99; // <== HERE Set your condition (country and minimal subtotal amount)
        $free      = array(); // Initializing
    
        // Loop through shipping rates for current shipping package
        foreach ( $rates as $rate_key => $rate ) {
            if ( $condition ){
                $targeted_rate_id = 'flat_rate:2';
                
                if( $targeted_rate_id === $rate_key ) {
                    unset($rates[$targeted_rate_id]);
                }
            }
        }
        return $rates;
    }

    This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

    Jassa

    Developer’s King

    Thanks

  • How to add custom field in WooCommerce variable product?

    How to add custom field in WooCommerce variable product?

    Hello guys, how are you? Welcome back to my blog therichpost.com. In this post I will tell you, How to add custom field in WooCommerce variable product?

    Live working video

    Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

    1. WooCommerce Hooks
    2. WordPress Tricks
    3. WordPress Hooks

    Guys here is the working code snippet and please use carefully:

    1. Guys here is the working code snippet for create custom field, save custom field value and get and show that custom field value inside single product page

    and guys you need to add this code inside your theme’s functions.php file:

    /**
     * Display the custom text field
     * @since 1.0.0
     */
    function cfwc_create_custom_field() {
     $args = array(
     'id' => 'custom_text_field_1',
     'label' => __( 'Custom Field 1', 'cfwc' ),
     'class' => 'custom_text_field_1',
     'desc_tip' => true,
     'description' => __( 'Custom Field 1.', 'ctwc' ),
     );
     woocommerce_wp_text_input( $args );
    }
    add_action( 'woocommerce_product_options_inventory_product_data', 'cfwc_create_custom_field' );
    
    
    /**
     * Save custom text field
     * @since 1.0.0
     */
    add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields' );
    function wc_custom_save_custom_fields( $post_id ) {
        if ( ! empty( $_POST['custom_text_field_1'] ) ) {
            update_post_meta( $post_id, 'custom_text_field_1', esc_attr( $_POST['custom_text_field_1'] ) );
        }
    }
    
    
    
    /**
     * Get and show the value of custom text field in single product page
     * @since 1.0.0
     */
    
    function cfwc_display_custom_field() {
     global $post;
     // Check for the custom field value
     $product = wc_get_product( $post->ID );
     $title = $product->get_meta( 'custom_text_field_1' );
     if( $title ) {
     // Only display our field if we've got a value for the field title
     printf(
     '<div class="cfwc-custom-field-wrapper"><label for="cfwc-title-field">%s</label></div>',
     esc_html( $title )
     );
     }
    }
    add_action( 'woocommerce_before_add_to_cart_button', 'cfwc_display_custom_field' );

    • How to add custom fields to WooCommerce products?
    • WooCommerce add custom field to product programmatically
    • Add custom field in WooCommerce settings page
    • How to add custom field in WooCommerce product without plugin?
    • WooCommerce display custom fields value on product page?
    • WooCommerce filter products by custom field
    • Add custom field in WooCommerce single product page plugin?

    This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

    Jassa

    Developer’s King

    Thanks