Show Woocommerce shipping based on product category

function cart_has_product_with_new_cats() {
//Check to see if user has product in cart
global $woocommerce;
//assigns a default negative value
// categories targeted 17, 18, 19

$product_in_cart = false;

// start of the loop that fetches the cart items

foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values[‘data’];
$terms = get_the_terms( $_product->id, ‘product_cat’ );

// second level loop search, in case some items have several categories
foreach ($terms as $term) {
$_categoryid = $term->term_id;
}

if ( $_categoryid === 94 ) {
$product_in_cart = true;
}

}

return $product_in_cart;
}

// add filter and function to hide method
add_filter( ‘woocommerce_package_rates’, ‘hide_shipping_if_cat_is_orange’ , 10, 2 );
function hide_shipping_if_cat_is_orange( $rates, $package ){
if ( cart_has_product_with_new_cats() ) {
// remove the rate you want
unset( $rates[‘wf_shipping_canada_post:DOM.RP’] );
}else{
unset( $rates[‘1’] );
}

// return the available methods without the one you unset.
return $rates;
}

Advertisements

Change WooCommce Text

add_filter( ‘gettext’, ‘my_text_strings’, 20, 3 );

function my_text_strings( $translated_text, $text, $domain ) {

switch ( $translated_text ) {

case ‘View Cart’ :

$translated_text = __( ‘View Services’, ‘woocommerce’ );break;

case ‘Update Cart’ :

$translated_text = __( ‘Update Services’, ‘woocommerce’ );break;

case ‘Cart totals’ :

$translated_text = __( ‘Services totals’, ‘woocommerce’ );break;

}

return $translated_text;

}

add_filter ( ‘wc_add_to_cart_message’, ‘wc_add_to_cart_message_filter’, 10, 2 );
function wc_add_to_cart_message_filter($message, $product_id = null) {
$titles[] = get_the_title( $product_id );

$titles = array_filter( $titles );
$added_text = sprintf( _n( ‘%s has been successfully added to your Services.’, ‘%s have been added to your Services.’, sizeof( $titles ), ‘woocommerce’ ), wc_format_list_of_items( $titles ) );

$message = sprintf( ‘%s <a href=”%s” class=”button”>%s</a>&nbsp;<a href=”%s” class=”button”>%s</a>’,
esc_html( $added_text ),
esc_url( wc_get_page_permalink( ‘checkout’ ) ),
esc_html__( ”, ‘woocommerce’ ),
esc_url( wc_get_page_permalink( ‘cart’ ) ),
esc_html__( ‘View Services’, ‘woocommerce’ ));

return $message;
}

After updated cart totals Ajax Woocommerce

jQuery( document.body ).on( ‘updated_cart_totals’, function() {

jQuery( document.body ).on( ‘updated_cart_totals’, function() {

jQuery(“#shipping_method_0”).prepend(“<option value=” selected=’selected’>Select Your Shipping Option</option>”);

console.log( ‘cart updated’ );

} );

How to check featured image exist or not in post

$args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => -1);

$the_query = new WP_Query( $args );
$i = 1;

if ( $the_query->have_posts() ) {

echo ‘<ul>’;

while ( $the_query->have_posts() ) {

$the_query->the_post();

if ( has_post_thumbnail() ) {

$dom_url = get_the_post_thumbnail_url();

$new_url = str_replace(‘site_url()’,’/var/www/html/’,get_the_post_thumbnail_url()); if(!file_exists($new_url))

{

echo ‘<li><b> ‘ .$i. ‘ </b> ‘. get_the_title() . ‘ Page Link:——-‘. get_the_post_thumbnail_url() .'</li>’;

$i++;

}

}

}

echo ‘</ul>’;

wp_reset_postdata();

} else {

// no posts found

}
echo “Number Of Posts: “.$i;

Check root path of image $_SERVER[‘DOCUMENT_ROOT’]