Làm chức năng up image ảnh cho sản phẩm vào đơn hàng, đặt hàng order demoart.net5s.vn (ok)

media

C:\xampp\htdocs\demoart\wp-content\themes\consulting-child\functions.php

add_action('woocommerce_before_add_to_cart_button','fc_woocommerce_before_add_to_cart_button_img');
function fc_woocommerce_before_add_to_cart_button_img() {
    $html = "";
    $html .= '<label class="title" for="imgct">Chọn ảnh:</label>';
    $html .= '<input type="file" id="imgct" name="imgct" style="margin-bottom: 10px;" accept="image/*">';
    echo $html;
}
add_action('woocommerce_add_to_cart', 'product_option_add_to_cart',10,6);
function product_option_add_to_cart($cart_id, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
  global $woocommerce;
  echo '<pre>';
    var_export($product_id);
  echo '</pre>';
  // echo '<pre>';
  //   var_export($cart_id); // 0c63677bdc3522bfc97eab3de157cf6f
  //   echo "<br/>";
  //   var_export($product_id); // 4037
  //   echo "<br/>";
  //   var_export($quantity); // 1
  //   echo "<br/>";
  //   var_export($variation_id); // 6319
  //   echo "<br/>";
  //   var_export($variation); // array ( 'attribute_pa_kich-thuoc' => '20cm-x-50cm', )
  //   echo "<br/>";
  //   var_export($cart_item_data); // array ( )
  // echo '</pre>';
  // echo '<pre>';
  //   var_export($_POST); // array ( 'attribute_pa_kich-thuoc' => '20cm-x-50cm', 'quantity' => '1', 'add-to-cart' => '4037', 'product_id' => '4037', 'variation_id' => '6319', )
  // echo '</pre>';
  if(isset($_POST['add-to-cart']) && $_FILES['imgct']['error'] == 0) {
  require( dirname(__FILE__) . '/../../../wp-load.php' );
  $wordpress_upload_dir = wp_upload_dir();
  $i = 1; 
  $imgct = $_FILES['imgct'];
  $items = $woocommerce->cart->get_cart();
  $arrproducts = array();
  foreach($items as $item => $values) { 
    $arrproducts[] = $values['product_id'];
  } 
  if(($_FILES['imgct']['error'] == 0) && in_array($product_id, $arrproducts)) {
      $imgctdata = "_imgct_data_user";
      $new_file_path = $wordpress_upload_dir['path'] . '/' . $imgct['name'];
      $new_file_mime = mime_content_type( $imgct['tmp_name'] );
      if( empty( $imgct ) )
          die( 'File is not selected.' );
      if( $imgct['error'] )
          die( $imgct['error'] );
      if( $imgct['size'] > wp_max_upload_size() )
          die( 'It is too large than expected.' );
      if( !in_array( $new_file_mime, get_allowed_mime_types() ) )
          die( 'WordPress doesn\'t allow this type of uploads.' );
      while( file_exists( $new_file_path ) ) {
          $i++;
          $new_file_path = $wordpress_upload_dir['path'] . '/' . $i . '_' . $imgct['name'];
      }
      if( move_uploaded_file( $imgct['tmp_name'], $new_file_path ) ) {
          $upload_id = wp_insert_attachment( array(
            'guid'           => $new_file_path, 
            'post_mime_type' => $new_file_mime,
            'post_title'     => preg_replace( '/\.[^.]+$/', '', $imgct['name'] ),
            'post_content'   => '',
            'post_status'    => 'inherit'
          ), $new_file_path );
          require_once( ABSPATH . 'wp-admin/includes/image.php' );
          wp_update_attachment_metadata( $upload_id, wp_generate_attachment_metadata( $upload_id, $new_file_path ) );
          $arrays = array(
            "linkimg" => $wordpress_upload_dir['url'] . '/' . basename( $new_file_path ),
            "hask" => $cart_id,
            "add-to-cart" =>$_POST['add-to-cart']
          );
          $serialize = maybe_serialize($arrays);
          update_post_meta($_POST['add-to-cart'], $imgctdata, $serialize);
        }
      }
  }
}
add_action("woocommerce_before_order_item_line_item_html","fc_woocommerce_order_item_line_item_html_ct",999,3);
function fc_woocommerce_order_item_line_item_html_ct($item_id, $item, $order) {
  $idpage = $item->get_product_id();
  $orderid = $item->get_order_id();
  $test = get_post_meta(2488,"_imgct_data_order_id");
  foreach ($test as $key => $value) {
    if(is_array($value) && isset($value['order_id'])) {
      if($orderid == $value['order_id']) {
        echo '<td><img style=" width: 150px; height: 150px; " src="'.$value['linkimg'].'" alt="image">';
      }
    }
  }
}
function wpdocs_selectively_enqueue_admin_script( $hook ) {
  wp_enqueue_style('my_custom_script', get_stylesheet_directory_uri() . '/inc/my_custom_script.css',array(), '0.1.0', 'all');
}
add_action( 'admin_enqueue_scripts', 'wpdocs_selectively_enqueue_admin_script' );

C:\xampp\htdocs\demoart\wp-content\themes\consulting-child\inc\my_custom_script.css

#order_line_items tr:not(.item){display:none}#order_line_items tr:not(.item):nth-child(1){display:block}

Last updated