Category: WooCommerce

Remove WooCommerce styles and scripts

Remove WooCommerce default css and js of lightbox then implement any plugins like:-

  1. Twist – Woocommerce Product Gallery Slider
  2. Responsive Lightbox

 

/* Remove WooCommerce styles and scripts. */
function woo_remove_lightboxes() {

// Styles
wp_dequeue_style( ‘woocommerce_prettyPhoto_css’ );

// Scripts
wp_dequeue_script( ‘prettyPhoto’ );
wp_dequeue_script( ‘prettyPhoto-init’ );
wp_dequeue_script( ‘fancybox’ );
wp_dequeue_script( ‘enable-lightbox’ );
}

add_action( ‘wp_enqueue_scripts’, ‘woo_remove_lightboxes’, 99 );

Remove All products or attributes with SQL commands WooCommerce

Remove all attributes from WooCommerce

DELETE FROM wp_terms WHERE term_id IN 
(SELECT term_id FROM wp_term_taxonomy WHERE taxonomy LIKE 'pa_%');
DELETE FROM wp_term_taxonomy WHERE taxonomy LIKE 'pa_%';
DELETE FROM wp_term_relationships WHERE term_taxonomy_id not IN 
(SELECT term_taxonomy_id FROM wp_term_taxonomy);

Delete all WooCommerce products

DELETE FROM wp_term_relationships WHERE object_id IN 
(SELECT ID FROM wp_posts WHERE post_type IN ('product','product_variation'));
DELETE FROM wp_postmeta WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type IN 
('product','product_variation'));
DELETE FROM wp_posts WHERE post_type IN ('product','product_variation');

Delete orphaned postmeta

DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL

Add remove shipping method when free shipping class is available WooCommerce

add_filter( ‘woocommerce_package_rates’,’hide_shipping_when_free_is_available’);

function hide_shipping_when_free_is_available( $rates, $package ) {
$freeship = array();
$flatship = array();
// Only modify rates if free_shipping is present
$total = WC()->cart->cart_contents_total;
if( $total >= 150 ) {
foreach ( $rates as $rate_id => $rate ) {
if ( ‘free_shipping’ === $rate->method_id ) {
$freeship[ $rate_id ] = $rate;
break;
}
}
foreach ( $rates as $rate_id => $rate ) {

if ( ‘flat_rate’ === $rate->method_id ) {
continue;

}
$flatship[ $rate_id ] = $rate;
}
return ! empty( $freeship ) ? $flatship : $rates;
}else{
return $rates;
}

}

woocommerce add js in payment gateway option

jQuery(function(){
    jQuery( 'body' )
    .on( 'updated_checkout', function() {
          usingGateway();

        jQuery('input[name="payment_method"]').change(function(){
            console.log("payment method changed");
              usingGateway();

        });
    });
});


function usingGateway(){
    console.log(jQuery("input[name='payment_method']:checked").val());
    if(jQuery('form[name="checkout"] input[name="payment_method"]:checked').val() == 'my_gateway'){
        console.log("Using my gateway");
        //Etc etc
    }else{
         console.log("Not using my gateway. Proceed as usual");
    }
}