Woocommerce – A Weblog of Priyank Maniar https://priyank.rocks Articles on web development & mobile app development Fri, 23 Jun 2023 07:17:20 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 WooCommerce custom code to hide some theme elements https://priyank.rocks/woocommerce-custom-code-to-hide-some-theme-elements/ Fri, 23 Jun 2023 07:16:51 +0000 https://priyank.rocks/?p=9489
1
2
3
4
5
6
7
8
9
10
11
12
13
// Woocommerce, hide the customer reviews ui

jQuery(document).ready(function(){

    jQuery('.woocommerce .woocommerce-product-rating').hide();
    jQuery('.woocommerce .edgtf-social-share-holder').hide();
    jQuery('.woocommerce-tabs .edgtf-tabs-nav').find('li').eq(1).hide();
    jQuery('.woocommerce-tabs .edgtf-tabs-nav').find('li').eq(2).hide();

    // hide related products
    jQuery('.woocommerce .related.products').hide();

});

]]>
WooCommerce: Show variation Price at a different place https://priyank.rocks/woocommerce-show-variation-price-at-a-different-place/ Tue, 07 Jul 2020 11:37:19 +0000 https://priyank.rocks/?p=6889 I have a form on the product single page. The price was showing in between of the attributes and the custom form fields. I wanted the price to show at a different place of my choice. The following code is a complete solution to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
 *
 * This entire block is a mixture of 3 solutions. 2 found from web and a bit of custom css styling making it third.
 * Please read the code carefully for more comments.
 *
 * So for the first one (https://www.businessbloomer.com/woocommerce-get-currently-selected-variation-id/). There's a block of code that we want to run when a variation is changed for that first we must have an event trigger for a variation change. This just does that.
 *
 * @source          https://www.businessbloomer.com/woocommerce-get-currently-selected-variation-id/
 *
 * */

add_action( 'woocommerce_before_add_to_cart_quantity', 'bbloomer_display_dropdown_variation_add_cart' );

function bbloomer_display_dropdown_variation_add_cart() {

    global $product;

    if ( $product->is_type( 'variable' ) ) {

        ?>

        <style type="text/css">
            /* Don't do display none because we are accessing the value inside this dom node but the user don't need to see the price here. */
            .woocommerce-variation {
                visibility: hidden;
                height: 1px;
            }
        </style>

        <script>
            jQuery(document).ready(function ($) {

                $('input.variation_id').change(function () {
                    if ('' != $('input.variation_id').val()) {

                        var variationID = $('input.variation_id').val();

                        if (variationID) {
                            jQuery("#wccf_product_field_master_container").hide();


                            // Source: https://stackoverflow.com/questions/43617537/move-woocommerce-variation-price
                            // Update price according to variable price
                            if (jQuery('form.variations_form').length !== 0) {
                                var form = jQuery('form.variations_form');
                                var variable_product_price = '';
                                setInterval(function () {
                                    if (jQuery('.single_variation_wrap span.price span.amount').length !== 0) {
                                        if (jQuery('.single_variation_wrap span.price span.amount').text() !== variable_product_price) {
                                            variable_product_price = jQuery('.single_variation_wrap span.price span.amount').text();

                                            // jQuery('.single-product-summary p.price span.amount').text(variable_product_price);
                                            jQuery('#custom_variation_price').text(variable_product_price);
                                        }
                                    }
                                }, 500);
                            }

                        }

                    }
                });

            });
        </script>
        <?php

    }
}
]]>
WooCommerce – Min/Max Quantities https://priyank.rocks/woocommerce-min-max-quantities/ Sun, 01 Mar 2020 16:34:45 +0000 http://priyank.rocks/?p=6243 The plugin BeRocket can be used for this.

]]>
WooCommerce: Buy in multiples of 5 https://priyank.rocks/woocommerce-buy-in-multiples-of-5/ Sun, 01 Mar 2020 16:32:48 +0000 http://priyank.rocks/?p=6241
1
2
3
4
5
6
7
8
9
10
11
12
13
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' ); 

function woocommerce_check_cart_quantities() {     
    $multiples = 5;
    $total_products = 0;
   
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $total_products += $values['quantity'];    
    }      
   
    if ( ( $total_products % $multiples ) > 0 )        
        wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' );
}
]]>