Advertisement
palsushobhan

Product bundles v6.4.0 Min/Max Items compatibility

Dec 12th, 2023
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1. add_filter('wcfm_product_bundles_general_fields', function($fields) {
  2.     global $WCFMph, $wp;
  3.     if( version_compare( $WCFMph->version, '1.0.8', '<=' ) && version_compare( WC_PB()->plugin_version(), '6.4.0', '>=' )) {
  4.         $min_qty_limit = '';
  5.         $max_qty_limit = '';
  6.         if(!empty( $wp->query_vars['wcfm-products-manage'] )) {
  7.             $product_id = $wp->query_vars['wcfm-products-manage'];
  8.             $min_qty_limit = get_post_meta( $product_id, '_wcpb_min_qty_limit', true );
  9.             $max_qty_limit = get_post_meta( $product_id, '_wcpb_max_qty_limit', true );
  10.         }
  11.         $min_max_fields = array(
  12.             "_wcpb_min_qty_limit" => array( 'label' => __( 'Min Bundle Size', 'woocommerce-product-bundles-min-max-items' ), 'type' => 'number', 'value' => $min_qty_limit, 'class' => 'wcfm-text', 'label_class' => 'wcfm_title', 'hints' => __( 'Minimum required quantity of bundled items.', 'woocommerce-product-bundles' ) ),
  13.             "_wcpb_max_qty_limit" => array( 'label' => __( 'Max Bundle Size', 'woocommerce-product-bundles-min-max-items' ), 'type' => 'number', 'value' => $max_qty_limit, 'class' => 'wcfm-text', 'label_class' => 'wcfm_title', 'hints' => __( 'Maximum allowed quantity of bundled items.', 'woocommerce-product-bundles-min-max-items' ) ),
  14.         );
  15.         $index = array_search( "_wc_pb_group_mode", array_keys( $fields ) ) + 1;
  16.         return array_slice( $fields, 0, $index, true ) +
  17.             $min_max_fields +
  18.             array_slice( $fields, $index, count( $fields ) - 1, true );
  19.    
  20.     }
  21.     return $fields;
  22. });
  23. add_action( 'after_wcfm_products_manage_meta_save', function($new_product_id, $wcfm_products_manage_form_data) {
  24.     global $WCFMph;
  25.     $product_type = empty( $wcfm_products_manage_form_data['product_type'] ) ? WC_Product_Factory::get_product_type( $new_product_id ) : sanitize_title( stripslashes( $wcfm_products_manage_form_data['product_type'] ) );
  26.     if ( $product_type != 'bundle' ) {
  27.         return;
  28.     }
  29.     $product = wc_get_product( $new_product_id );
  30.     if( version_compare( $WCFMph->version, '1.0.8', '<=' ) && version_compare( WC_PB()->plugin_version(), '6.4.0', '>=' )) {
  31.         if ( ! empty( $wcfm_products_manage_form_data[ '_wcpb_min_qty_limit' ] ) && is_numeric( $wcfm_products_manage_form_data[ '_wcpb_min_qty_limit' ] ) ) {
  32.             $product->add_meta_data( '_wcpb_min_qty_limit', stripslashes( $wcfm_products_manage_form_data[ '_wcpb_min_qty_limit' ] ), true );
  33.         } else {
  34.             $product->delete_meta_data( '_wcpb_min_qty_limit' );
  35.         }
  36.    
  37.         if ( ! empty( $wcfm_products_manage_form_data[ '_wcpb_max_qty_limit' ] ) && is_numeric( $wcfm_products_manage_form_data[ '_wcpb_max_qty_limit' ] ) ) {
  38.             $product->add_meta_data( '_wcpb_max_qty_limit', stripslashes( $wcfm_products_manage_form_data[ '_wcpb_max_qty_limit' ] ), true );
  39.         } else {
  40.             $product->delete_meta_data( '_wcpb_max_qty_limit' );
  41.         }
  42.         $product->save();
  43.     }
  44. }, 91, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement