I am trying to add a notification to the shipping totals row on the WooCommerce shopping cart page as soon as two conditions are met.
AND
I've been playing around with the code uderneath, which already hides certain shipping methods (succesfully) but I'm unable to add a simple notification when the conditions are met.
add_filter( 'woocommerce_package_rates', 'woocommerce_shipping_notification', 9999, 2 );
function woocommerce_shipping_notification( $rates, $package ) {
if ( WC()->cart->get_cart_contents_weight() > 14999 ) {
if ( isset( $rates['local_pickup:1'] ) )
unset( $rates['flat_rate:2'], $rates['local_pickup:7'] );
echo "My notification";
}
return $rates;
}
if ( !class_exists( 'WooCommerce' ) ) return;
add_action('wp', function() {
if ( is_cart() && !WC()->cart->is_empty() ) {
$cart = WC()->cart;
$products = $cart->get_cart_contents();
$cat_ids = array('20', '22');
$product_categories = array();
$product_categories_ids = array();
foreach($cat_ids as $key => $cat_id) {
$product_categories = array_merge(
$product_categories,
get_categories(array(
'taxonomy' => 'product_cat',
'child_of' => $cat_id,
))
);
}
foreach($product_categories as $key => $category) {
$product_categories_ids[] = $category->term_id;
}
$categories_included = function() use ( $products, $product_categories_ids ) {
foreach( $products as $key => $product ) {
$terms = wp_get_object_terms(array($product['product_id']), 'product_cat');
foreach( $terms as $_key => $term ) {
if ( in_array( $term->term_id, $product_categories_ids) )
return true;
}
}
return false;
};
$only_pick_up = $categories_included() || $cart->get_cart_contents_weight() > 15000;
if ( $only_pick_up ) {
add_action( 'woocommerce_before_cart', function() {
echo
'<div>
<h3>Please contact us to discuss the delivery options</h3>
</div>';
});
add_action('woocommerce_cart_totals_before_shipping', function() {
foreach(WC()->shipping()->packages[0]['rates'] as $key => $rate) {
if ( $rate->__get('instance_id') != 18 ) {
unset(WC()->shipping()->packages[0]['rates'][$key]);
}
}
});
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.