【问题标题】:How to include multiple product ids in a Woocommerce action如何在 Woocommerce 操作中包含多个产品 ID
【发布时间】:2020-10-18 14:36:20
【问题描述】:

我们如何在if ($product->get_id()=="15757"){中包含多个产品ID

add_filter( 'woocommerce_quantity_input_args', 'ts_woocommerce_quantity_selected_number', 10, 2 );
  
function ts_woocommerce_quantity_selected_number( $args, $product ) {
 // global $product;
   if ( ! is_cart() ) {
 if ($product->get_id()=="15757"){
      $args['input_value'] = 5; // Start from this value (default = 1) 
      $args['max_value'] = 15; // Maximum quantity (default = -1)
      $args['min_value'] = 5; // Minimum quantity (default = 0)
      $args['step'] = 5; // Increment or decrement by this value (default = 1)
 }
   } else {
 if ($product->get_id()=="15757"){
      // Cart's 'min_value' is 0
      $args['max_value'] = 15; 
      $args['step'] = 5; 
      $args['min_value'] = 5;
 }
   }
  
   return $args; 
}

我们试过了,但它不起作用if ($product->get_id()=="15757,15758,15759"){

【问题讨论】:

标签: php arrays wordpress if-statement woocommerce


【解决方案1】:

对于多个产品 ID,您可以将 in_array() 用于产品 ID 数组...现在在您的代码中,您需要首先添加“产品 ID”条件,例如:

add_filter( 'woocommerce_quantity_input_args', 'ts_woocommerce_quantity_selected_number', 10, 2 );
function ts_woocommerce_quantity_selected_number( $args, $product ) {
    $product_ids = array("15757", "15758"); // Here the array of product Ids

    if ( in_array( $product->get_id(), $product_ids ) ) {
        // In cart
        if ( ! is_cart() ) {
            $args['input_value'] = 5; // Start from this value (default = 1) 
            $args['max_value'] = 15; // Maximum quantity (default = -1)
            $args['min_value'] = 5; // Minimum quantity (default = 0)
            $args['step'] = 5; // Increment or decrement by this value (default = 1)
        } 
        // Not in cart
        else {
            // Cart's 'min_value' is 0
            $args['max_value'] = 15; 
            $args['step'] = 5; 
            $args['min_value'] = 5;
        }
    }
    return $args; 
}

代码进入活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

相关:Hide quantity fields in cart for specific products in Woocommerce 3

【讨论】:

  • 这非常感谢您并且工作正常,但是您为什么需要先添加产品 ID 条件,这与我们首先检查其购物车的代码之间有什么区别? ,,有什么区别?
  • 在您的情况下,您在 IF ELSE 语句的两个位置使用相同的产品 ID……因此在这种情况下,您将仅在 IF 语句中将订单 ID 放在首位。要定位多个产品 ID(ID 数组),您将使用 in_array()...
  • 我们明白了你的意思,这不会影响先检查其购物车或先检查其匹配产品 ID 的功能
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多