Advertisement
palsushobhan

wcfm-min-order-qty-and-steps

Oct 25th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. add_filter( 'woocommerce_quantity_input_args', function ( $args, $product ) {
  2.     $min_order_qty = wcfm_get_post_meta( $product->get_id(), 'wcfm_min_order_qty', true );
  3.     $args['min_value'] = $min_order_qty ? $min_order_qty : 1;
  4.     $order_qty_step = wcfm_get_post_meta( $product->get_id(), 'wcfm_order_qty_step', true );
  5.     $args['step'] = $order_qty_step ? $order_qty_step : 1;    
  6.     return $args;
  7. }, 1, 2 );
  8.  
  9. add_filter('wcfm_product_fields_stock', function($fields, $product_id) {
  10.     $index = array_search( "sku", array_keys( $fields ) ) + 1;
  11.     if ( $index !== false ) {
  12.         $min_order_qty_field = array( "wcfm_min_order_qty" => array(
  13.             'label'       => __( 'Minimum order quantity', 'wc-frontend-manager' ),
  14.             'type'        => 'number',
  15.             'class'       => 'wcfm-text wcfm_non_negative_input',
  16.             'label_class' => 'wcfm_title',
  17.             'value'       => wcfm_get_post_meta( $product_id, 'wcfm_min_order_qty', true ),
  18.             'placeholder' => 'Keep it blank for minimum quantity as 1',
  19.          ),
  20.         );
  21.         $qty_step_field = array( "wcfm_order_qty_step" => array(
  22.             'label'       => __( 'Order quantity step size', 'wc-frontend-manager' ),
  23.             'type'        => 'number',
  24.             'class'       => 'wcfm-text wcfm_non_negative_input',
  25.             'label_class' => 'wcfm_title',
  26.             'value'       => wcfm_get_post_meta( $product_id, 'wcfm_order_qty_step', true ),
  27.             'placeholder' => 'Keep it blank for default step value 1',
  28.          ),
  29.         );
  30.         return array_slice( $fields, 0, $index, true ) +
  31.             $min_order_qty_field + $qty_step_field +
  32.             array_slice( $fields, $index, count( $fields ) - 1, true );
  33.     }
  34.     return $fields;
  35. }, 10, 2);
  36.  
  37. add_action( 'after_wcfm_products_manage_meta_save', function($new_product_id, $wcfm_products_manage_form_data) {
  38.     $min_order_qty = 1;
  39.     if ( !empty( (int) $wcfm_products_manage_form_data[ 'wcfm_min_order_qty' ] ) ) {
  40.         $min_order_qty = (int) $wcfm_products_manage_form_data[ 'wcfm_min_order_qty' ];
  41.     }
  42.     wcfm_update_post_meta( $new_product_id, 'wcfm_min_order_qty', $min_order_qty );
  43.  
  44.     $order_qty_step = 1;
  45.     if ( !empty( (int) $wcfm_products_manage_form_data[ 'wcfm_order_qty_step' ] ) ) {
  46.         $order_qty_step = (int) $wcfm_products_manage_form_data[ 'wcfm_order_qty_step' ];
  47.     }
  48.     wcfm_update_post_meta( $new_product_id, 'wcfm_order_qty_step', $order_qty_step );
  49. }, 91, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement