Skip to content

Instantly share code, notes, and snippets.

@luckymancvp
Created June 12, 2020 11:08
Show Gist options
  • Save luckymancvp/ae694a042d5d01637a74d75bec060f6f to your computer and use it in GitHub Desktop.
Save luckymancvp/ae694a042d5d01637a74d75bec060f6f to your computer and use it in GitHub Desktop.
Custom field

Tất cả các đoạn code sau đều cho vào file functions.php

Đoạn 1, thêm cột vào trong admin

// Add a new custom column to admin order list
add_filter( 'manage_edit-shop_order_columns', 'admin_orders_list_add_column', 10, 1 );
function admin_orders_list_add_column( $columns ){
	$columns['custom_column3'] = __( 'Product ID', 'woocommerce' );
	$columns['custom_column4'] = __( 'Design Front', 'woocommerce' );
  $columns['custom_column5'] = __( 'Design Back', 'woocommerce' );
  return $columns;
}

Đoạn 2, thêm dữ liệu cho cột


// The data of the new custom column in admin order list
add_action( 'manage_shop_order_posts_custom_column' , 'admin_orders_list_column_content', 10, 2 );
function admin_orders_list_column_content( $column, $post_id ){
    global $the_order;

    
		if( 'custom_column3' === $column ){
        $count = 0;
        // Loop through order items
        foreach( $the_order->get_items() as $item ) {
            $product = $item->get_product(); // The WC_Product Object
            $id = $product->get_id();
			$producteditURL = get_edit_post_link($id);
		    printf('<a href="%s">%s</a>', $producteditURL, $id);
			printf("\n");
			$count++;
        }
    }
	if( 'custom_column4' === $column ){
        $count = 0;

        // Loop through order items
        foreach( $the_order->get_items() as $item ) {
            $product = $item->get_product(); // The WC_Product Object
            //$style   = $count > 0 ? ' style="padding-left:6px;"' : '';

            // Display product thumbnail
            $designFront = get_post_meta($product->id, 'design_front', true);
            printf('<a href="%s">%s</a>', $designFront, $designFront);
			printf("\n");

            $count++;
        }
    }
    if( 'custom_column5' === $column ){
        $count = 0;

        // Loop through order items
        foreach( $the_order->get_items() as $item ) {
            $product = $item->get_product(); // The WC_Product Object
            //$style   = $count > 0 ? ' style="padding-left:6px;"' : '';

            // Display product thumbnail
            $designBack = get_post_meta($product->id, 'design_back', true);
            printf('<a href="%s">%s</a>', $designBack, $designBack);
			printf("\n");

            $count++;
        }
    }
}

Để hoạt động được thì trong product phải thêm custom fields với tên là : design_frontdesign_black . Ko có thì ko hiện chứ ko có lỗi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment