如何将 if(is_page()) 用于 WooCommerce 产品?

How to use if(is_page()) for WooCommerce products?

我在 functions.php 中使用代码,允许用户发送他们的徽标以获取可定制的产品。我只想为这个页面使用这个功能,因为其他产品是不可定制的。起初我试过这样的:

if(is_page('1089')){
   \function here
}

然而,每当我使用这个或 is_post 等时,图像输入按钮从两个产品中完全消失。你能帮我吗?我使用的函数是:

add_action( 'woocommerce_before_add_to_cart_button', 'display_additional_product_fields', 9 );

function display_additional_product_fields(){
     ?>
  <p class="form-row validate-required" id="image" >
    <label for="file_field"><?php echo __("Logonuzu Yukleyin") . ' '; ?>
      <br>
      <input type='file' name='image' accept='image/*'>
    </br>
    </label>
  </p>
  <br>
  <p>Ya da</p>
  </br>
  <?php
}
}
}

// Add custom fields data as the cart item custom data
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_fields_data_as_custom_cart_item_data', 10, 2 );
function add_custom_fields_data_as_custom_cart_item_data( $cart_item, $product_id ){
  if( isset($_FILES['image']) && ! empty($_FILES['image']) ) {
    $upload   = wp_upload_bits( $_FILES['image']['name'], null, file_get_contents( $_FILES['image']['tmp_name'] ) );
    $filetype  = wp_check_filetype( basename( $upload['file'] ), null );
    $upload_dir = wp_upload_dir();
        $upl_base_url = is_ssl() ? str_replace('http://', 'https://', $upload_dir['baseurl']) : $upload_dir['baseurl'];
    $base_name  = basename( $upload['file'] );

    $cart_item['file_upload'] = array(
      'guid'   => $upl_base_url .'/'. _wp_relative_upload_path( $upload['file'] ), // Url
      'file_type' => $filetype['type'], // File type
      'file_name' => $base_name, // File name
      'title'  => ucfirst( preg_replace('/\.[^.]+$/', '', $base_name ) ), // Title
    );
    $cart_item['unique_key'] = md5( microtime().rand() ); // Avoid merging items
  }
  return $cart_item;
}
/*
// Display custom cart item data in cart (optional)
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
  if ( isset( $cart_item['file_upload']['title'] ) ){
    $cart_item_data[] = array(
      'name' => __( 'Image uploaded', 'woocommerce' ),
      'value' => str_pad($cart_item['file_upload']['title'], 16, 'X', STR_PAD_LEFT) . '…',
    );
  }
  return $cart_item_data;
}
*/
// Save Image data as order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_field_update_order_item_meta', 20, 4 );
function custom_field_update_order_item_meta( $item, $cart_item_key, $values, $order ) {
  if ( isset( $values['file_upload'] ) ){
    $item->update_meta_data( '_img_file', $values['file_upload'] );
  }
}

// Admin orders: Display a linked button + the link of the image file
add_action( 'woocommerce_after_order_itemmeta', 'backend_image_link_after_order_itemmeta', 10, 3 );
function backend_image_link_after_order_itemmeta( $item_id, $item, $product ) {
  // Only in backend for order line items (avoiding errors)
  if( is_admin() && $item->is_type('line_item') && $file_data = $item->get_meta( '_img_file' ) ){
    echo '<p><a href="'.$file_data['guid'].'" target="_blank" class="button">'.__("Open Image") . '</a></p>'; // Optional
    echo '<p><code>'.$file_data['guid'].'</code></p>'; // Optional
  }
}

// Admin new order email: Display a linked button + the link of the image file
add_action( 'woocommerce_email_after_order_table', 'wc_email_new_order_custom_meta_data', 10, 4);
function wc_email_new_order_custom_meta_data( $order, $sent_to_admin, $plain_text, $email ){
  // On "new order" email notifications
  if ( 'new_order' === $email->id ) {
    foreach ($order->get_items() as $item ) {
      if ( $file_data = $item->get_meta( '_img_file' ) ) {
        echo '<p>
          <a href="'.$file_data['guid'].'" target="_blank" class="button">'.__("Download Image") . '</a><br>
          <pre><code style="font-size:12px; background-color:#eee; padding:5px;">'.$file_data['guid'].'</code></pre>
        </p><br>';
      }
    }

您可以像这样检查产品 ID:

if(get_the_id() == 1089){
   //Your logic here
}

但是,我建议您以不同的方式执行此操作,例如为这些产品创建一个专用类别,这样您以后可以添加更多产品以具有相同的功能,而且如果您删除该产品并创建一个新的,您不需要再次触摸代码来添加 id,而只需将产品分配给专用类别。那么你的条件将是这样的:

if(has_term('special_category_slug', 'product_cat', get_the_id())){
  //Your logic here
}