Guest User

Untitled

a guest
Dec 15th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. // Add weight limit into cart page for some product category or categories
  2. add_action( 'woocommerce_check_cart_items', 'cldws_set_weight_requirements');
  3. function cldws_set_weight_requirements() {
  4. if( is_cart() || is_checkout() ) {
  5. global $woocommerce;
  6.  
  7. $product_in_cart = false;
  8.  
  9. foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
  10. $_product = $values['data'];
  11. $terms = get_the_terms( $_product->id, 'product_cat' );
  12.  
  13. foreach ($terms as $term) {
  14. $_categoryid = $term->term_id;
  15. if ( ($_categoryid === 132) || ($_categoryid === 129 ) || ($_categoryid === 135) ) {
  16. $product_in_cart = true;
  17. $minimum_weight = 2000;
  18. $cart_contents_weight = WC()->cart->cart_contents_weight;
  19. if( $cart_contents_weight < $minimum_weight ) {
  20. wc_add_notice( sprintf('<strong>The error message. Minimal weight is %s%s.</strong>'
  21. . '<br />Current weight of order: %s%s',
  22. $minimum_weight,
  23. get_option( 'woocommerce_weight_unit' ),
  24. $cart_contents_weight,
  25. get_option( 'woocommerce_weight_unit' ),
  26. get_permalink( wc_get_page_id( 'shop' ) )
  27. ),
  28. 'error' );
  29. } break 2;
  30. }
  31. }
  32. }
  33. return $product_in_cart;
  34. }
  35. }
  36.  
  37. if (has_term('mrazene','product_shipping_class',$_product->id)) {
  38. $product_in_cart = true;
  39. $minimum_weight = 2000;
  40. $cart_contents_weight = WC()->cart->cart_contents_weight;
  41. if( $cart_contents_weight < $minimum_weight ) {
  42. wc_add_notice( sprintf('<strong>The error message. Minimal weight is %s%s.</strong>'
  43. . '<br />Current weight: %s%s',
  44. $minimum_weight,
  45. get_option( 'woocommerce_weight_unit' ),
  46. $cart_contents_weight,
  47. get_option( 'woocommerce_weight_unit' ),
  48. get_permalink( wc_get_page_id( 'shop' ) )
  49. ),
  50. 'error' );
  51. } break 2;
  52.  
  53. $chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );
  54.  
  55. if ( ! in_array( 'local_pickup', $chosen_shipping_rates ) ) {
  56. if (has_term('mrazene','product_shipping_class',$_product->id)) {
  57. $product_in_cart = true;
  58. $minimum_weight = 2000;
  59. $cart_contents_weight = WC()->cart->cart_contents_weight;
  60. if( $cart_contents_weight < $minimum_weight ) {
  61. wc_add_notice( sprintf('<strong>The error message. Minimal weight is %s%s.</strong>'
  62. . '<br />Current weight: %s%s',
  63. $minimum_weight,
  64. get_option( 'woocommerce_weight_unit' ),
  65. $cart_contents_weight,
  66. get_option( 'woocommerce_weight_unit' ),
  67. get_permalink( wc_get_page_id( 'shop' ) )
  68. ),
  69. 'error' );
  70. } break 2;
  71. }
  72. endif;
Add Comment
Please, Sign In to add comment